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\)算法。我们发现两个性质:

  1. 若\((l, r)\)可行,那么\((l, r + 1) , (l, r + 2)...(l, x)\)都是可行的。因为在升序序列删东西还是升序的。

  2. 若\((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)\)的时间预处理四个数组:

  1. \(S_i\) 代表数字\(i\)在数组中出现最早的位置
  2. \(T_i\) 代表数字\(i\)在数组中出现最晚的位置
  3. \(Sx_i\) 代表数字\(i\)到\(x\)在数组中出现最早的位置
  4. \(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的更多相关文章

  1. Codeforces Beta Round #65 (Div. 2)

    Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...

  2. Codeforces Beta Round #65 (Div. 2) C. Round Table Knights

    http://codeforces.com/problemset/problem/71/C 题意: 在一个圆桌上有n个人,每个人要么是1,要么就是0,现在要判断是否能由一些1相连构成正多边形. 思路: ...

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

  4. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

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

  6. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. 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]中有多少个数 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. HotSpot源码分析之C++对象的内存布局

    HotSpot采用了OOP-Klass模型来描述Java类和对象.OOP(Ordinary Object Pointer)指的是普通对象指针,而Klass用来描述对象的具体类型.为了更好理解这个模型, ...

  2. 【译】Arc 在 Rust 中是如何工作的

    原文标题:How Arc works in Rust 原文链接:https://medium.com/@DylanKerler1/how-arc-works-in-rust-b06192acd0a6 ...

  3. Cpython的全局解释器锁(GIL)

    # Cpyrhon解释器下有个全局解释器锁-GIL:在同一 # 在同一时刻,多线程中只有一个线程访问CPU # 有了全局解释器锁(GIL)后,在同一时刻只能有一个线程访问CPU. # 全局解释器锁锁的 ...

  4. Spring扩展之二:ApplicationListener

    1.介绍 用于监听应用程序事件的接口. 子接口:GenericApplicationListener,SmartApplicationListener. 通过ApplicationEvent类和App ...

  5. 最全Linux搭建SVN服务端教程

    文章首推 支付宝接口对接 高德地图调用 验证码登录 QQ邮箱登录 今日主题:Linux搭建SVN服务端 简介 相信程序员对SVN还是不陌生的,虽然现在用Git比较多,但是SVN也是用的,SVN可以做代 ...

  6. SpringBoot 之 @ControllerAdvice 拦截异常并统一处理

    在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...

  7. PHP反序列化漏洞-CVE-2016-7124(绕过__wakeup)复现

    前言 最近电脑也不知怎么了时不时断网而且我竟然找不出原因!!!很诡异....  其他设备电脑都OK唯独我的电脑 时好时坏 我仿佛摸清了我电脑断网的时间段所以作息时间都改变了  今天12点多断网刷了会手 ...

  8. Guitar Pro教程之虚拟吉他功能讲解

    上一章节我们讲述了Guitar Pro的组织小节的相关功能,那么本章节我们还是采用图文结合的方式为大家讲解关于{cms_selflink page='index' text='Guitar Pro'} ...

  9. Vegas教程分享,制作古装墨迹笔刷开场效果

    许多酷炫的古装大片,片头曲介绍人物的时候,都有一种墨迹笔刷的开场效果,那么这个特效如何利用Vegas去做呢? 1.导入素材文件 首先呢,导入相关文件素材到视频制作软件Vegas中,点击页面上方如图1箭 ...

  10. C语言讲义——函数

    为实现特定目的而编写的一段可被调用的代码 简单地讲:函数就是一组语句,取了个名字 别名:子例程(routine)/方法(Method,一般面向对象的语言使用这个叫法) 函数的组成部分 以主函数为例: ...