Codeforces Round #789 (Div. 2) A-C
Codeforces Round #789 (Div. 2) A-C
A
题目
https://codeforces.com/problemset/problem/1677/A
题解
思路
知识点:模拟。
(比较显然,不写了)
时间复杂度 \(O(nlogn)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int a[100];
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i = 0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int cnt = 0;
bool ok = false;
for(int i = 0;i<n-1;i++){
if(a[i] == a[i+1] || !a[i]){
ok = true;
break;
}
}
for(int i = 0;i<n;i++){
if(a[i]) cnt++;
}
cout<<(cnt+!ok)<<'\n';
}
return 0;
}
B
题目
https://codeforces.com/problemset/problem/1678/B1
https://codeforces.com/problemset/problem/1678/B2
题解
思路
知识点:贪心。
对于给定偶数长度的0/1串,奇数子串与其他串(不论奇偶的其他)的分界点有且仅有一个位于某一对中间。比如,\(111001100011\) 划分以后变成 \(11,10,01,10,00,11\) ,发现 \(111\) 的末尾 \(1\) 出现在 \(10\) 中,\(000\) 的首部 \(0\) 出现在 \(10\) 中。因此我们可以通过按对(即两个两个不相交)遍历,若遇到一次 \(01\) 或 \(10\) 则操作次数加 \(1\) 。
与此同时,我们发现若修改一处 \(01\) 或 \(10\) 可以修改成 \(00\) 或 \(11\) ,从而被前段或者后段的 \(00\) 或 \(11\) 吸收不改变总段数,因此可以将修改等价认为直接删除这段 \(01\) 或 \(10\) ,即在程序中不考虑这种情况对总数影响,只需要记录 \(00\) 或 \(11\) 的连续成段情况、特别地,如果 \(0/1\) 串本身没有 \(00\) 或 \(11\) 的对,或者说全串由 \(01\) 或 \(10\) 组成,那么它们可以自成唯一一段互相吸收,需要特判。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
int op = 0,cnt = 0;
char pre = '?';
for(int i = 0;i<n;i+=2){///两两配对
if(s[i] != s[i+1]) op++;///操作次数+1
else{
if(pre != s[i]) cnt++;///不同数字,段数+1
pre = s[i];///更新段数字
}
}
cout<<op<<' '<<max(1,cnt)<<'\n';///如果没有00/11吸收01/10,那么01/10可以自成一段
}
return 0;
}
C
题目
https://codeforces.com/problemset/problem/1677/A
题解
思路
知识点:DP,枚举。
注意到 \(a<b<c<d\) ,可以考虑枚举 \(b,c\) 两个点,用 \(a,d\) 分别在 \([1,b-1]\) 和 \([c+1,n]\) 的区间匹配。
匹配条件是 \(p_a < p_c \and p_b >p_d\) ,考虑用数组 \(cnt[i][j]\) 表示满足 \(p_x \leq j , x \in [1,i]\) 的 \(x\) 个数。于是 \(a\) 的匹配个数是 \(cnt[b-1][p[c]]\) ,\(d\) 的匹配个数是 \(cnt[n][p[b]] - cnt[c][p[b]]\) 。因此,对于一组 \(b,c\) 可以得到 \(cnt[b-1][p[c]] \cdot (cnt[n][p[b]] - cnt[c][p[b]])\) 的组数,枚举 \(b,c\) 累加即可。
时间复杂度 \(O(n^2)\)
空间复杂度 \(O(n^2)\)
代码
#include <bits/stdc++.h>
using namespace std;
int p[5007],cnt[5007][5007];//注意初始化
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i = 1;i<=n;i++){
cin>>p[i];
}
for(int i = 1;i<=n;i++){///预处理cnt[i][j] 为 [1,i] 中小于等于 j 的数字个数
for(int j = 1;j<=n;j++) cnt[i][j] = cnt[i-1][j];///传递上个区间
for(int j = p[i];j<=n;j++) cnt[i][j]++;///j从p[i]开始都大于等于i,遍历+1
}
///枚举b,c,以此为准求[1,b-1]和[c+1,d]间合法a,d个数
long long ans = 0;
for(int b = 2;b<=n-2;b++){
for(int c = b+1;c<=n-1;c++){
ans += 1LL * cnt[b-1][p[c]] * (cnt[n][p[b]] - cnt[c][p[b]]);
}
}
cout<<ans<<'\n';
}
return 0;
}
Codeforces Round #789 (Div. 2) A-C的更多相关文章
- Codeforces Round #789 (Div. 2)
题集链接 A. Tokitsukaze and All Zero Sequence 题意 Tokitsukaze 有一个长度为 n 的序列 a. 对于每个操作,她选择两个数字 ai 和 aj (i≠j ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- Java中日期格式化的实现算法
package com.study.test; import java.io.Serializable; import java.text.SimpleDateFormat; import java. ...
- MySql免安装版 Error 2003 Can connect to MySQL server on ...
现象描述:mysql只能本地登录,无法远程登录 解决方案: 1. 查看mysql端口(默认端口3306,命令端口根据需要修改),发现只有本地连接端口开放. netstat -an|findstr 33 ...
- python学习-Day35
目录 今日内容详细 代码创建进程 创建进程的方式 第一种创建进程的方式 创建进程的第二种方式 进程实现并发 join方法 进程间数据默认隔离 进程对象属性和方法 进程号如何查看 查看进程号的方法 杀死 ...
- 1.SSH协议学习笔记
一.SSH介绍 介绍: SSH全称是Secure Shell,安全外壳协议. 端口号:22: 如何查看服务端口号: grep ssh /etc/services netstat -antup | gr ...
- 流量录制回放工具jvm-sandbox-repeater入门篇——服务部署
趋于当前技术不断更新.产品功能多元化之下,流量回放的热度也是越来越高. 在前一段时间,测试团队也提到阿里开源的流量回放工具 jvm-sandbox-repeater 我个人就先尝试一下,期间还是遇到一 ...
- C# 随机给一个全部信息都未知的类类型,如何获取该类的类名、属性个数、属性名、属性的数据类型、属性值?
一.场景假设 假设现在有一个泛型类T的实例对象t,该T类的全部信息都未知. 要求:打印输出实例对象t的类名.属性个数.属性名.属性的数据类型.属性值. 二.解决问题 1.我们根据输出的内容要求定义一个 ...
- 一条Sql的执行过程
一条sql内部是如何执行的: 学习MySQL实战45专栏 sql中的内部执行图: 可以分为两部分:server和存储引擎 server层包含: 连接器.分析器.优化器.执行器,涵盖了MySQL大多数核 ...
- 论文解读《Deep Attention-guided Graph Clustering with Dual Self-supervision》
论文信息 论文标题:Deep Attention-guided Graph Clustering with Dual Self-supervision论文作者:Zhihao Peng, Hui Liu ...
- 评估海外pop点网络质量,批量探测到整个国家运营商ip地址段时延
1 查询当地供应商所有AS号和IP地址段,如下 可以手动复制也可以爬下来,此次测试地址不多,手动复制下来再做下格式话 61.99.128.0/17 61.99.0.0/16 61.98.96.0/20 ...
- 385. Mini Parser - LeetCode
Question 385. Mini Parser Solution 分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下: # ...