Codeforces 1272 A-E

A Three Friends

直接枚举所有情况,共\(3\times 3\times 3=27\)种。

code

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define lson l,mid,p<<1
  5. #define rson mid+1,r,p<<1|1
  6. #define pb push_back
  7. #define ll long long
  8. using namespace std;
  9. const int inf=1e9;
  10. const int mod=1e9+7;
  11. const int maxn=1e5+10;
  12. int q;
  13. int main(){
  14. //ios::sync_with_stdio(false);
  15. //freopen("in","r",stdin);
  16. cin>>q;
  17. while(q--){
  18. int a,b,c;
  19. cin>>a>>b>>c;
  20. int ans=abs(a-b)+abs(b-c)+abs(a-c);
  21. for(int i=-1;i<=1;i++){
  22. for(int j=-1;j<=1;j++){
  23. for(int k=-1;k<=1;k++){
  24. int x=a+i,y=b+j,z=c+k;
  25. ans=min(ans,abs(x-y)+abs(y-z)+abs(x-z));
  26. }
  27. }
  28. }
  29. cout<<ans<<endl;
  30. }
  31. return 0;
  32. }

B Snow Walking Robot

因为是无限大的网格,所以最简单的构造方法是走一个矩形框

注意要特判向任意方向走一步再走回起点的情况,样例中有。

code

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define lson l,mid,p<<1
  5. #define rson mid+1,r,p<<1|1
  6. #define pb push_back
  7. #define ll long long
  8. using namespace std;
  9. const int inf=1e9;
  10. const int mod=1e9+7;
  11. const int maxn=1e5+10;
  12. int q;
  13. char s[maxn];
  14. int main(){
  15. ios::sync_with_stdio(false);
  16. //freopen("in","r",stdin);
  17. cin>>q;
  18. while(q--){
  19. cin>>s+1;
  20. int n=strlen(s+1);
  21. int L=0,R=0,U=0,D=0;
  22. for(int i=1;i<=n;i++){
  23. if(s[i]=='L') ++L;
  24. else if(s[i]=='R') ++R;
  25. else if(s[i]=='U') ++U;
  26. else ++D;
  27. }
  28. L=R=min(L,R);
  29. U=D=min(U,D);
  30. if(L&&U==0){
  31. cout<<2<<endl;
  32. cout<<"RL"<<endl;
  33. }else if(U&&L==0){
  34. cout<<2<<endl;
  35. cout<<"DU"<<endl;
  36. }else if(U==0||L==0){
  37. cout<<0<<endl;
  38. }else{
  39. cout<<L*2+U*2<<endl;
  40. for(int i=1;i<=L;i++){
  41. cout<<'R';
  42. }
  43. for(int i=1;i<=D;i++){
  44. cout<<'D';
  45. }
  46. for(int i=1;i<=L;i++){
  47. cout<<'L';
  48. }
  49. for(int i=1;i<=D;i++){
  50. cout<<'U';
  51. }
  52. cout<<endl;
  53. }
  54. }
  55. return 0;
  56. }

C Yet Another Broken Keyboard

在字符串s中所有不能打出的字母的位置作为隔板,隔出来的每个子串(设\(len\)为子串的长度)对答案的贡献即为\(len \times (len+1) /2\)。

code

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define lson l,mid,p<<1
  5. #define rson mid+1,r,p<<1|1
  6. #define pb push_back
  7. #define ll long long
  8. using namespace std;
  9. const int inf=1e9;
  10. const int mod=1e9+7;
  11. const int maxn=2e5+10;
  12. int n,k;
  13. char s[maxn],t[30];
  14. int main(){
  15. ios::sync_with_stdio(false);
  16. //freopen("in","r",stdin);
  17. cin>>n>>k;
  18. cin>>s+1;
  19. for(int i=1;i<=k;i++){
  20. char c;
  21. cin>>c;
  22. t[c-'a']=1;
  23. }
  24. ll ans=0,cnt=0;
  25. for(int i=1;i<=n;i++){
  26. if(!t[s[i]-'a']){
  27. ans+=cnt*(cnt+1)/2;
  28. cnt=0;
  29. }else ++cnt;
  30. }
  31. ans+=cnt*(cnt+1)/2;
  32. cout<<ans<<endl;
  33. return 0;
  34. }

D Remove One Element

\(O(n)\)预处理出\(l[i]\)为\(i\)作为终点的最长严格递增子段,\(r[i]\)为\(i\)作为起始点的最长严格递增子段。

  • 不删除一个元素,\(ans=max\{l[i]\},(1<=i<=n)\)。
  • 删除一个元素,\(ans=max\{l[i-1]+r[i+1]\},(1<i<n\&\&a[i-1]<a[i+1])\)

code

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define lson l,mid,p<<1
  5. #define rson mid+1,r,p<<1|1
  6. #define pb push_back
  7. #define ll long long
  8. using namespace std;
  9. const int inf=1e9;
  10. const int mod=1e9+7;
  11. const int maxn=2e5+10;
  12. int n;
  13. int a[maxn];
  14. int l[maxn],r[maxn];
  15. int main(){
  16. ios::sync_with_stdio(false);
  17. //freopen("in","r",stdin);
  18. cin>>n;
  19. int ans=0;
  20. for(int i=1;i<=n;i++){
  21. cin>>a[i];
  22. if(a[i]>a[i-1]) l[i]=l[i-1]+1;
  23. else l[i]=1;
  24. ans=max(ans,l[i]);
  25. }
  26. r[n]=1;
  27. for(int i=n-1;i>=1;i--){
  28. if(a[i]<a[i+1]) r[i]=r[i+1]+1;
  29. else r[i]=1;
  30. }
  31. for(int i=2;i<n;i++){
  32. if(a[i+1]>a[i-1]) ans=max(ans,l[i-1]+r[i+1]);
  33. }
  34. cout<<ans<<endl;
  35. return 0;
  36. }

E Nearest Opposite Parity

能发现若\(d[i+-a[i]]\)的值已确定,那么\(d[i]=min(d[i],d[i+-a[i]]+1)\)。

跟据这个性质我们以反边建图,所有边的边权都为1,以所有能一步到达奇偶性不同的点的点作为源点来跑最短路就行了(因为所有边权都为1,所以直接bfs也行)。

code

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define lson l,mid,p<<1
  5. #define rson mid+1,r,p<<1|1
  6. #define pb push_back
  7. #define ll long long
  8. using namespace std;
  9. const int inf=1e9;
  10. const int mod=1e9+7;
  11. const int maxn=2e5+10;
  12. int n;
  13. int a[maxn];
  14. vector<int>g[maxn];
  15. int dis[maxn];
  16. struct ppo{
  17. int v,c;
  18. bool operator <(const ppo &r) const{
  19. return c>r.c;
  20. }
  21. };
  22. int main(){
  23. ios::sync_with_stdio(false);
  24. //freopen("in","r",stdin);
  25. cin>>n;
  26. for(int i=1;i<=n;i++){
  27. cin>>a[i];
  28. }
  29. for(int i=1;i<=n;i++){
  30. dis[i]=inf;
  31. if(i-a[i]>=1){
  32. if(a[i-a[i]]%2!=a[i]%2) dis[i]=1;
  33. g[i-a[i]].pb(i);
  34. }
  35. if(i+a[i]<=n){
  36. if(a[i+a[i]]%2!=a[i]%2) dis[i]=1;
  37. g[i+a[i]].pb(i);
  38. }
  39. }
  40. priority_queue<ppo>q;
  41. for(int i=1;i<=n;i++){
  42. if(dis[i]==1) q.push(ppo{i,dis[i]});
  43. }
  44. while(!q.empty()){
  45. int u=q.top().v;q.pop();
  46. for(int x:g[u]){
  47. if(dis[u]+1<dis[x]){
  48. dis[x]=dis[u]+1;
  49. q.push(ppo{x,dis[x]});
  50. }
  51. }
  52. }
  53. for(int i=1;i<=n;i++){
  54. if(dis[i]==inf) cout<<-1<<" ";
  55. else cout<<dis[i]<<" ";
  56. }
  57. return 0;
  58. }

Codeforces 1272 A-E的更多相关文章

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

  2. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  3. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  4. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

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

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

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

  8. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  9. Codeforces Round #605 (Div. 3) A. Three Friends(贪心)

    链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...

随机推荐

  1. Python中操作mysql的pymysql模块详解(转载)

    https://www.cnblogs.com/wt11/p/6141225.html

  2. (一)mybatis介绍

    一.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  3. IDEA如何本机调试springboot应用打的jar包

    背景: 我用命名行 执行 java -jar  ***.jar发现 springboot启动时抛出错误,因此想debug进去看看究竟为什么出错. 1  在命令行执行 java -jar -Xdebug ...

  4. python练习:函数4

    ''' 1.定义一个func(name),该函数效果如下. assert func("lilei") = "Lilei" assert func("h ...

  5. 南宁AI项目

    1.能了解并对项目整体进度情况有清晰的认识,什么时间点需要完成什么工作项. 2.认识了解项目干系人,能和客户独立沟通交流,理解现场业务,不要一问三不知,什么情况都不了. 3.能推动项目进展和问题及时处 ...

  6. Rsync同步过程中遇到的常见问题

    一.Rsync服务介绍 Rsync属于一款实现全量及增量同步数据的软件工具,适用于unix/linux/windows等多种操作系统平台. Rsync软件能实现本地复制,远程复制,或者远程守护进程方式 ...

  7. 2019 WebRtc AudioMixer混音流程

    本文简要说明最新版WebRtc AudioMixer混音流程. 本程序使用4个16KHz 单声道时长均大于10秒的Wav文件作为混音源,只合成前10秒的音频,输出也是16KHz单声道音频. 输入和输出 ...

  8. 3 webpack 4 加vue 2.0生产环境搭建

    1 在前两篇笔记中已经能把开发环境弄好了,接来下构建他的生产环境 2 使用npm 安装url-loader和file-loader来支持图片和字体 npm install --save-dev url ...

  9. 在Linux下执行Jmeter脚本

    前言 Jmeter这款接口测试工具,已经在越来越多的公司被要求会使用了. 而且,现在应该部分小伙伴们都开始用起来了. 但是,你们知道除了在Windows用图形化界面的Jmeter执行脚本之外,还有其他 ...

  10. stm32 定时器 通用定时器

    STM32F10xxx 2个基本定时器(TIM6.TIM7) 4个通用定时器(TIM2. TIM3. TIM4和TIM5) 2个高级定时器(TIM1.TIM8) 每个定时器都是完全独立的,没有互相共享 ...