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的更多相关文章

  1. Codeforces Round #789 (Div. 2)

    题集链接 A. Tokitsukaze and All Zero Sequence 题意 Tokitsukaze 有一个长度为 n 的序列 a. 对于每个操作,她选择两个数字 ai 和 aj (i≠j ...

  2. 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 ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. Python图像处理丨OpenCV+Numpy库读取与修改像素

    摘要:本篇文章主要讲解 OpenCV+Numpy 图像处理基础知识,包括读取像素和修改像素. 本文分享自华为云社区<[Python图像处理] 二.OpenCV+Numpy库读取与修改像素> ...

  2. 获取iframe的window对象

    在父窗口中获取iframe中的元素 // JS // 方法1: var iframeWindow = window.frames["iframe的name或id"]; iframe ...

  3. [AcWing 823] 排列

    点击查看代码 #include<iostream> using namespace std; const int N = 10; int n; void dfs(int u, int nu ...

  4. create-react-app的TS支持以及css模块化

    开始: 利用官方脚手架,搭建react工程.参考:https://react.docschina.org/docs/create-a-new-react-app.html. 过程: 1.暴露webpa ...

  5. 【VMware】在移动硬盘或U盘中安装便携linux系统

    背景: 操作系统课需要在Linux环境下进行编程.本来是给了个傻瓜式的Ubuntu虚拟机镜像,但奈何虚拟机这东西我这老本子跑起来巨卡,装双系统又卡,只能选择把系统装进便携设备里了. 前期准备: 一个2 ...

  6. ZABBIX新功能系列1-使用Webhook将告警主动推送至第三方系统

    Zabbix5以来的新版本与以前的版本除UI界面变化较大外,在很多功能上也有许多亮点,我这里计划安排1个系列来和大家交流一些新功能的使用,这是第一篇:使用Webhook将告警主动推送至第三方系统. 首 ...

  7. 『现学现忘』Git基础 — 24、Git中查看历史版本记录

    目录 1.查看详细的历史版本记录 2.简化显示历史版本记录 3.历史版本记录常用操作 (1)指定查看最近几次提交的内容 (2)以简单图形的方式查看分支版本历史 (3)翻页与退出 4.查看分支相关的版本 ...

  8. Linux磁盘和文件系统知识总结

    硬盘操作 为什么要给硬盘分区? 如果你需要在一块硬盘上用到多个文件系统,那么你就需要对硬盘进行分区,以便用不同的分区支持不同的文件系统.(但一个硬盘只能有一个分区表!)反过来说,如果你整块硬盘都用同样 ...

  9. 从0到1使用kubebuiler开发operator

    介绍 假设一个Nginx的QPS(服务器一秒内处理的请求数)上限为500,如果外部访问的QPS达到了600,为了保证服务质量,必须扩容一个Nginx来分摊请求. 在Kubernetes环境中,如果外部 ...

  10. 【多线程】线程礼让 Thread.yield()

    线程礼让 Thread.yield() 礼让线程,让当前正在执行的线程暂停,但不阻塞 : 将线程从运行状态转为就绪状态 : 让cpu重新调度,礼让不一定成功!看CPU心情. 代码示例: /** * @ ...