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中几个文本文件查看命令
Linux中,常用的文本文件查看命令介绍如下: 1. cat 用法: cat [options] filename options: -A: 显示全部. -E: 在每一行的后面加上"$&qu ...
- 深入理解h2和r2dbc-h2
简介 本文将会介绍R2DBC的H2实现r2dbc-h2的使用方法和要注意的事项.一起来看看吧. H2数据库简介 什么是H2数据库呢? H2是一个Java SQL database,它是一个开源的数据库 ...
- 免费|申请谷歌云服务器|msf安装
apt-get install -y wget 参考链接 知乎-免费|申请谷歌云服务器 知乎-免费|申请谷歌云服务器 cnblogs-debian.ubuntu安装metasploit通用方法 谷歌云 ...
- git设置个人信息
git config --global user.name "username" 设置下自己提交的用户名 git config --global user.email " ...
- Perfview 分析进程性能
PerfView 概述: PerfView是一个可以帮助你分析CPU和内存问题的工具软件.它非常轻量级也不会入侵诊断的程序,在诊断过程中对诊断的程序影响甚微. Visual Studio自带的性能分析 ...
- MathType中如何实现上下两行公式“=”号对齐
作为功能强大的数学公式编辑器,MathType可以轻松输入各种复杂的公式和符号,与 Office 文档完美结合,显示效果超好,比 Office 自带的公式编辑器要强大很多,可以为办公文档.网页.桌面出 ...
- 如何用ABBYY解决文档图像存在缺陷,OCR 准确性低的问题
扭曲的文本行.歪斜.噪声及扫描图像和数码照片中常见的其他缺陷可能会降低识别质量.ABBYY FineReader,提供各种 自动和手动工具去除这些缺陷. 如何手动编辑图像 如果您禁用了自动预处理功能或 ...
- Word 2013中如何直接调用MathType
相信有很多用户已经发现在使用Word 2013编辑文档时MathType无法直接调用,但是点击文档中的公式时能够跳出MathType公式编辑窗口,那么这是怎么回事呢?其实,这一问题也不是没有办法解决的 ...
- MySQL优化篇(未完待续)
一.优化SQL语句的一般步骤 1.通过 show status命令了解各种sql的执行频率 mysql客户端连接成功后,通过show[session|global] status命令,可以查看服务器的 ...
- 唯一key的生成规则,可自己视情况改动
<?php function getMillisecond() { $time = explode ( " ", microtime ()); $time = $time[1 ...