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 ...
随机推荐
- selenium之 定位以及切换frame(iframe)(转)
frame标签有frameset.frame.iframe三种,frameset跟其他普通标签没有区别,不会影响到正常的定位,而frame与iframe对selenium定位而言是一样的,seleni ...
- 借助boost bind/function来实现基于对象编程。
boost bind/function库的使用: 替换了stl中mem_fun,bind1st,bin2nd等函数.用户注册回调函数需要利用boost/bind转化成库中boost/function格 ...
- 你只会用 StringBuilder?试试 StringJoiner,真香!
你只会用 StringBuilder/ StringBuffer 拼接字符串? 那你就 OUT 了!! 如果需要拼接分隔符的字符串,建议使用 Java 8 中的这款拼接神器:StringJoiner, ...
- struts2中数据的传输
1.传统的写多个request接受参数方法. 2.struts2中的多个setter方法,getter方法 3.利用实体bean,让strut2 实例bean,少写setter方法,getter方法, ...
- nginx开启目录浏览
使用nginx作为下载站点,开启目录浏览的功能 在/etc/nginx/sites-enabled/default中添加: autoindex on ; autoindex_exact_size of ...
- 廖师兄springboot微信点餐开发中相关注解使用解释
package com.imooc.dataobject;import lombok.Data;import org.hibernate.annotations.DynamicUpdate;impor ...
- 这 5 个开源的能挣钱的 SpringBoot 项目,真TMD香!
不得不佩服 Spring Boot 的生态如此强大,今天我给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统,小伙伴们再也不用从头到尾撸一个项目了,简直就是接私活,挣钱的利器啊. SmartA ...
- 查询SQL Server数据库使用的版本号信息
如何查询当前连接服务器的数据库版本号,使用以下语句执行即可: select @@version
- 01、Spring环境搭建
环境:SpringSource-Tool-3.9.9.Eclipse4.10.0 首先,我们需要解决的是Spring包的问题,我看了百度.CSDN很多都是直接一上来随便丢个包就可以安装了,搞得我弄了一 ...
- Eclipse的环境配置
1.想要配置Eclipse的环境,就要先下载Eclipse,并安装它,不会下载安装的小伙伴可以点击下面给的链接,里面有我写的详细的教程,这里就不重复了 Eclipse下载与安装:https://blo ...