Codeforces Round #605 (Div. 3)
地址:http://codeforces.com/contest/1272
A. Three Friends
仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L = min{a, b, c}, R = max{a, b, c})
那么本题的移动条件就只考虑两个端点L, R即可,答案即为 |(L+1)-(R-1)| 即L向右移动1,R向左移动1,在此之前判断一下原L,R之间的距离是否<=2,<=2输出0
#include <bits/stdc++.h>
using namespace std; int q, a[];
int main(){
ios::sync_with_stdio();
cin.tie(); cout.tie();
cin >> q;
while( q-- ){
for( int i=; i<; i++ ) cin >> a[i];
sort(a, a+);
if( a[]-a[]<= ) cout << "" << endl;
else cout << (a[]-a[]-)* << endl;
} return ;
}
A.Three Friends
B.Snow Walking Robot
这题比赛时没做出来??理解题意出了锅。
思路:如果能够回到(0, 0)需满足数量U=D且R=L,只需要上下方向选min(U, D),左右方向选min(L, R)。
如果U=0或D=0那么就要删除所有的另外一个方向,且将左右方向置为1(min(L, R)>0),对于R和L也一样要满足该条件,上下方向置为1(min(U, D)>0)
然后最后组合的时候只需要让路径构成一个环即可,这个组合可以任意组合,做题的时候卡在这了。。。
#include <bits/stdc++.h>
using namespace std; int q, cl, cr, cu, cd;
string s;
int main(){
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio();
cin.tie(); cout.tie();
cin >> q;
while( q-- ){
cin >> s;
cl = cr = cu = cd = ;
for( char i:s ){
cl += i=='L';
cr += i=='R';
cu += i=='U';
cd += i=='D';
}
cu = min(cu, cd);
cl = min(cl, cr);
if( cu== || cl== ){
cl = min(, cl);
cu = min(, cu);
}
string ans;
for( int i=; i<cl; i++ ) ans += 'R';
for( int i=; i<cu; i++ ) ans += 'U';
for( int i=; i<cl; i++ ) ans += 'L';
for( int i=; i<cu; i++ ) ans += 'D';
cout << ans.size() << endl << ans << endl;
} return ;
}
B.Snow Walking Robot
C.Yet Another Broken Keyboard
给k个字母问这k的字母能构成字符串s中多少个子串。
set存一下这k个字母,对s中每一段连续的可以在set中查找到的分别进行统计汇总即可,每一段长度len_i对答案的贡献为(len_i)*(len_i+1)/2.
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
int n, k;
string s;
set<char> a;
ll ans, tmp;
int main(){
ios::sync_with_stdio();
cin.tie(); cout.tie();
cin >> n >> k >> s;
for( int i=; i<k; i++ ){
char ch;
cin >> ch;
a.insert(ch);
}
int len = s.size();
for( int i=; i<len; i++ ){
if( a.find(s[i])!=a.end() )
tmp ++;
else{
ans += tmp*(tmp+)/;
tmp = ;
}
}
if(tmp) ans += tmp*(tmp+)/;
cout << ans << endl; return ;
}
C.Yet Another Broken Keyboard
D. Remove One Element
三遍预处理 + 一遍原序列
第一遍预处理处理出来一个数不删的最大长度。
第二遍预处理从i from 1~n记录l[i]从左到右递增的最长连续序列长度。
第三遍预处理从i from n~1记录r[i]从右到左递减的最长连续序列长度。
最后跑原序列i from 1~n判断删除i之后的结果能否更新第一遍记录的答案
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int a[maxn], n, l[maxn], r[maxn], ans;
int main(){
ios::sync_with_stdio();
cin.tie(); cout.tie();
cin >> n;
for( int i=; i<n; i++ ) cin >> a[i];
int tmp = ;
for( int i=; i<n; i++ ){
if( a[i]>a[i-] ) tmp++;
else tmp = ;
ans = max(ans, tmp);
}
l[] = ;
for( int i=; i<n; i++ ){
if( a[i]>a[i-] ) l[i] = l[i-]+;
else l[i] = ;
}
r[n-] = ;
for( int i=n-; ~i; i-- ){
if( a[i]<a[i+] ) r[i] = r[i+]+;
else r[i] = ;
}
for( int i=; i<n-; i++ )
if( a[i-]<a[i+] ) ans = max(ans, l[i-]+r[i+]);
cout << ans << endl; return ;
}
D. Remove One Element
E.Nearest Opposite Parity
比赛时dfs写MLE,时间不够放弃。
预处理 + bfs通过,使用bfs理由->最短路
预处理出来答案为1的点,然后放进队列,同时将所有点i from 1~n可以跳到的位置i-a[i]和i+a[i]连边然后bfs
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
const int inf = 0x3f3f3f3f;
int n, a[maxn], ans[maxn];
vector<int> p[maxn];
int main(){
memset( ans, -, sizeof(ans) );
scanf("%d", &n);
for( int i=; i<n; i++ ) scanf("%d", &a[i]);
queue<int> q;
for( int i=; i<n; i++ ){
if( i-a[i]>= ){
p[i-a[i]].push_back(i);
if( a[i-a[i]]% != a[i]% ) ans[i] = ;
}
if( i+a[i]<n ){
p[i+a[i]].push_back(i);
if( a[i+a[i]]% != a[i]% ) ans[i] = ;
}
if( ans[i]== ) q.push(i);
}
while( q.size() ){
int x = q.front();
q.pop();
for( auto i:p[x] ){
if( ans[i]==- && a[i]%==a[x]% ){
ans[i] = ans[x]+;
q.push(i);
}
}
}
for( int i=; i<n; i++ ){
printf("%d", ans[i]);
if( i==n- ) puts("");
else putchar(' ');
} return ;
}
E.Nearest Opposite Parity
Codeforces Round #605 (Div. 3)的更多相关文章
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard
链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- Codeforces Round #605 (Div. 3) A. Three Friends(贪心)
链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...
- Codeforces Round #605 (Div. 3) 题解
Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity (超级源点)
随机推荐
- 洛谷P5437/5442 约定(概率期望,拉格朗日插值,自然数幂)
题目大意:$n$ 个点的完全图,点 $i$ 和点 $j$ 的边权为 $(i+j)^k$.随机一个生成树,问这个生成树边权和的期望对 $998244353$ 取模的值. 对于P5437:$1\le n\ ...
- 手把手教你 通过 NuGet.Server 包 搭建nuget服务器,并使用桌面工具上传 nuget 包,免命令行
新建web项目 工具:VS2013 版本:.Net Framework 4.6,低版本也行,不过要找到对应版本的Nuget.Server 装了NuGet客户端(百度如何安装) WebForm或MVC都 ...
- Loj #3045. 「ZJOI2019」开关
Loj #3045. 「ZJOI2019」开关 题目描述 九条可怜是一个贪玩的女孩子. 这天,她和她的好朋友法海哥哥去玩密室逃脱.在他们面前的是 \(n\) 个开关,开始每个开关都是关闭的状态.要通过 ...
- ZYNQ笔记(2):PS端——Hello World !
PL端使用过后,来到了ZYNQ核心的部分:PS端,现在用Vivado软件对ZYNQ-7000开发板的PS端进行第一个程序设计:Hello World. 一.新建Vivado工程 1.打开Vivado, ...
- virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护
virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护 两个方面:对应用端 & 对DOM端(渲染准备的计算) 1.将DOM状态的维护工作由系统维护转交给vi ...
- 【linux】CentOS 查看系统时间,修改时区
===============CentOS 7.6================ 1.查看系统时间 date 查看当前系统时间以及时区结果是: Mon Jul 8 09:23:31 UTC 2019 ...
- 安卓访问https错误,访问http可以,可能是nginx ssl证书配置有问题
开发中遇到react-native生成的android访问UAT和开发环境的http api都可以,但是访问生产环境的https就报错,还有就是第三方webhook调用你https网站的api也可能会 ...
- form.submit()提交后返回数据的处理
form.submit()发送请求一般是单向的,如果需要取返回的数据,一般会发送ajax请求,但是如果form中有附件呢?(以后有时间给大家分享ajax上传附件的功能),确实需要返回数据来知道该功能是 ...
- 兼容 .NET Core3.0, Natasha 框架实现 隔离域与热编译操作
关于 Natasha 动态构建已经成为了封装者们的家常便饭,从现有的开发趋势来看,普通反射性能之低,会迫使开发者转向EMIT/表达式树等构建方式,但是无论是EMIT还是表达式树,都会依赖于反射的 ...
- PIE截图方法的优化
因为我们组的项目要通过截图获取数据,所以要经常使用截图工具,之前截图都是根据教程(https://www.cnblogs.com/PIESat/p/10243308.html)用的地图显示范围截图,而 ...