Codeforces Edu Round 65 A-E
A. Telephone Number
跟之前有一道必胜策略是一样的,\(n - 10\)位之前的数存在\(8\)即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 110;
int n;
char s[N];
int main(){
int T; scanf("%d", &T);
while(T--){
scanf("%d%s", &n, s + 1);
bool flag = false;
for(int i = 1; i <= n - 10; i++)
if(s[i] == '8') flag = true;
if(flag) puts("YES");
else puts("NO");
}
return 0;
}
B. Lost Numbers
暴力枚举答案即可。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[6] = {4, 8, 15, 16, 23, 42};
int b[4];
bool inline judge(){
for(int i = 0; i < 4; i++)
if(a[i] * a[i + 1] != b[i]) return false;
return true;
}
int main(){
for(int i = 1; i <= 4; i++){
printf("? %d %d\n", i, i + 1);
fflush(stdout);
scanf("%d", b + i - 1);
}
do{
if(judge()){
printf("! ");
for(int i = 0; i < 6; i++)
printf("%d ", a[i]);
puts("");
fflush(stdout);
break;
}
}while(next_permutation(a, a + 6));
return 0;
}
C. News Distribution
这...不是并查集裸题吗?
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 500010;
int n, m, f[N], size[N];
int inline find(int x){
return f[x] == x ? x : f[x] = find(f[x]);
}
void inline merge(int x, int y){
x = find(x), y = find(y);
if(x == y) return ;
if(size[x] > size[y]) swap(x, y);
f[x] = y; size[y] += size[x];
}
int main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
f[i] = i, size[i] = 1;
}
for(int i = 1; i <= m; i++){
int k, x; scanf("%d", &k);
if(!k) continue;
scanf("%d", &x);
for(int j = 1, y; j < k; j++){
scanf("%d", &y);
merge(x, y);
}
}
for(int i = 1; i <= n; i++)
printf("%d ", size[find(i)]);
return 0;
}
D. Bicolored RBS
维护一个变量\(dep\),表示目前在第一层,只要奇偶异置,则符合要求,最大层数为\(\lceil maxDep / 2\rceil\)
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
const int N = 200010;
char str[N];
int n, ans[N], dep = 0;
int main(){
scanf("%d%s", &n, str + 1);
for(int i = 1; i <= n; i++){
if(str[i] == '('){
ans[i] = (++dep) & 1;
}else if(str[i] == ')'){
ans[i] = (dep--) & 1;
}
}
for(int i = 1 ;i <= n; i++)
printf("%d", ans[i]);
return 0;
}
E. Range Deleting
\(Two - Pointer\)算法。我们发现两个性质:
若\((l, r)\)可行,那么\((l, r + 1) , (l, r + 2)...(l, x)\)都是可行的。因为在升序序列删东西还是升序的。
若\((l, r)\)不可行,那么\((l, r - 1) , (l, r - 2)...(l, l)\)都是不可行的。因为在已经不可行的序列加东西显然不想。
那么,对于每一个\(l (1 <= l <= x)\),我们找出一个最小的\(r\)使其满足条件,左端点为\(l\)对答案的贡献就是\(n - r + 1\)。我们称这个最小的\(r\)为\(r_{min_l}\)
还有另外一个性质,在\(l\)增加之后,\(r_{min_l}\)只可能增加或不变,不可能减少。因为多露出数,要么符合条件,要么还需要减掉一些数。
想到这里,我们就可以用双指针算法了。我们还需要用\(O(1)\)的时间判断加入一个数是否可行。
我们可以用\(O(n)\)的时间预处理四个数组:
- \(S_i\) 代表数字\(i\)在数组中出现最早的位置
- \(T_i\) 代表数字\(i\)在数组中出现最晚的位置
- \(Sx_i\) 代表数字\(i\)到\(x\)在数组中出现最早的位置
- \(Tx_i\) 代表数字\(1\)到\(i\)在数组中出现最晚的位置
- 对于左端\(l\)的处理,若我们想知道让\(l - 1\)移动到\(l\)是否可行:
若\(Tx[l - 2] < S[l - 1]\),则小于\(l - 1\)的一切数位置都在第一个\(l - 1\)左边,就可以移动。
- 对于右端点的处理,已确定\([1, l - 1]\)与\([r + 1, x]\)范围内的数均满足条件,判断\((l, r)\),是否满足条件:
\(Tx_{l - 1} < Sx_{r + 1}\)表示所有小于等于\(l - 1\)的数都在大于\(r + 1\)的数的左边则满足条件,否则我们需要让\(r\)增加。
注意,在处理当中我们先找到一个最小的\(r\)使得\((1, r)\)满足条件,然后在扩大\(l\)的1过程当中,被迫增加\(r\),如性质\(2\)。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1000010, INF = 0x3f3f3f3f;
typedef long long LL;
int n, x, S[N], T[N], Sx[N], Tx[N];
int a[N];
LL ans = 0;
int main(){
memset(S, 0x3f, sizeof S);
scanf("%d%d", &n, &x);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
if(S[a[i]] == INF) S[a[i]] = i;
T[a[i]] = i;
}
for(int i = 1; i <= x; i++)
Tx[i] = max(Tx[i - 1], T[i]);
Sx[x + 1] = INF;
for(int i = x; i; i--)
Sx[i] = min(Sx[i + 1], S[i]);
int r = x - 1;
while(r && T[r] < Sx[r + 1]) r--;
for(int l = 1; l <= x; l++){
if(l > 2 && S[l - 1] < Tx[l - 2]) break;
while(r <= x && (r < l || Tx[l - 1] > Sx[r + 1]))
r++;
ans += x - r + 1;
}
printf("%lld\n", ans);
return 0;
}
Codeforces Edu Round 65 A-E的更多相关文章
- Codeforces Beta Round #65 (Div. 2)
Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #65 (Div. 2) C. Round Table Knights
http://codeforces.com/problemset/problem/71/C 题意: 在一个圆桌上有n个人,每个人要么是1,要么就是0,现在要判断是否能由一些1相连构成正多边形. 思路: ...
- codeforces水题100道 第二十一题 Codeforces Beta Round #65 (Div. 2) A. Way Too Long Words (strings)
题目链接:http://www.codeforces.com/problemset/problem/71/A题意:将长字符串改成简写格式.C++代码: #include <string> ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
随机推荐
- linux 信号 ctrl + d z c fg bg 作用
ctrl+c:前台进程终止 后台进程的终止: 方法一:通过jobs命令查看job号(假设为num),然后执行kill %num $ kill %1 方法二:通过ps命令查看job的进程号(PID, ...
- Java中List,Set,Map区别
在Java开发面试中,面试官最常问到的就是Java集合,免不了要让面试者说出之间的区别,下面博主就对其做了总结. 1.集合与数组的区别 长度区别:数组是固定长度,集合长度可变: 内容区别:数组可以是基 ...
- WireShark——ARP 协议包分析
1. 什么是ARP ARP(Address Resolution Protocol)协议,即地址解析协议.该协议的功能就是将 IP 地 址解析成 MAC 地址. ARP(Address Resolu ...
- spring boot和spring cloud版本选择
SpringBoot版本介绍 官网链接:https://spring.io/projects/spring-boot#learn Spring Boot的版本以数字表示.例如:Spring Boot ...
- Hadoop大数据平台之Kafka部署
环境:CentOS 7.4 (1708 DVD) 工具:Xshell+Xftp 1. 使用xftp将kafka上传到/usr/local目录下,将kafka解压并重命名. 2. 编辑kafka/co ...
- 8、Spring Boot任务
1.异步任务 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring ...
- 正则表达式——maltrail工程项目中使用
1. 正则表达所需语法 \ 正则表达式使用反斜杠字符 ('') 来表示特殊形式或是允许在使用特殊字符时不引发它们的特殊含义. 转义特殊字符(允许你匹配 '*', '?', 或者此类其他) \A 只匹配 ...
- MathType中怎么打约化普朗克常数ħ
普朗克常数记为ħ,是一个物理常数,用以描述量子大小.在量子力学中占有重要的角色,马克斯·普朗克在1900年研究物体热辐射的规律时发现的.如果要打出关于约化普朗克常数ħ的公式,就需要用到专业的公式编辑器 ...
- 使用ABBYY FineReader 手动校正文档复杂结构
ABBYY FineReader 15(Windows系统)拥有强大的OCR识别功能,能对扫描仪或者数码相机等光学工具获取的图像进行识别,解析其中的文本.图像.表格.条形码等,方便用户进一步获取图像中 ...
- 如何使用OCR编辑器检查和识别文本
ABBYY FineReader 15(Windows系统)中的OCR编辑器能帮助用户对扫描仪或者数码相机获取的图像文件进行自动文本识别,OCR区域绘制等,使这些图像文件能进一步转换为可编辑的格式.其 ...