Codeforces 1131

比赛链接

hack一个暴力失败了两次最后还是没成功身败名裂= = CF跑的也太快了吧...

不过倒也涨了不少。

A.Sea Battle

  1. //想麻烦了,但是无所谓...
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <vector>
  7. #include <cstring>
  8. #include <algorithm>
  9. #define mp std::make_pair
  10. #define pr std::pair<int,int>
  11. #define pc putchar
  12. #define gc() getchar()
  13. typedef long long LL;
  14. inline int read()
  15. {
  16. int now=0,f=1;register char c=gc();
  17. for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
  18. for(;isdigit(c);now=now*10+c-48,c=gc());
  19. return now*f;
  20. }
  21. LL c(LL a,LL b)
  22. {
  23. return ((a+4+b)<<1)-4;
  24. }
  25. int main()
  26. {
  27. LL w1=read(),h1=read(),w2=read(),h2=read();
  28. LL ans=c(w1,h1)+c(w2,h2)-((std::min(w1,w2)+2)<<1);
  29. printf("%d\n",ans);
  30. return 0;
  31. }

B.Draw!

  1. //有点不知道说什么...看代码吧...
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <vector>
  7. #include <cstring>
  8. #include <algorithm>
  9. #define mp std::make_pair
  10. #define pr std::pair<int,int>
  11. #define pc putchar
  12. #define gc() getchar()
  13. typedef long long LL;
  14. const int N=1e5+5;
  15. int A[N],B[N];
  16. inline int read()
  17. {
  18. int now=0,f=1;register char c=gc();
  19. for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
  20. for(;isdigit(c);now=now*10+c-48,c=gc());
  21. return now*f;
  22. }
  23. int main()
  24. {
  25. int n=read();
  26. for(int i=1; i<=n; ++i) A[i]=read(),B[i]=read();
  27. LL ans=0;
  28. for(int i=1,las=0; i<=n; ++i)
  29. {
  30. int x=std::max(A[i-1],B[i-1]),y=std::min(A[i],B[i]);
  31. x=std::max(x,las), las=y+1;
  32. ans+=std::max(y-x+1,0);
  33. }
  34. printf("%I64d\n",ans);
  35. return 0;
  36. }

C.Birthday

sort一下从中间往左右依次分配。没证,但感觉就是对的。

  1. //31ms 0KB
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <vector>
  7. #include <cstring>
  8. #include <algorithm>
  9. #define mp std::make_pair
  10. #define pr std::pair<int,int>
  11. #define pc putchar
  12. #define gc() getchar()
  13. typedef long long LL;
  14. const int N=1e3+5;
  15. int A[N],B[N];
  16. inline int read()
  17. {
  18. int now=0,f=1;register char c=gc();
  19. for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
  20. for(;isdigit(c);now=now*10+c-48,c=gc());
  21. return now*f;
  22. }
  23. int main()
  24. {
  25. int n=read();
  26. for(int i=1; i<=n; ++i) A[i]=read();
  27. std::sort(A+1,A+1+n);
  28. int mid=n+1>>1; B[mid]=A[1];
  29. for(int i=3,t=mid-1; i<=n; i+=2) B[t--]=A[i];
  30. for(int i=2,t=mid+1; i<=n; i+=2) B[t++]=A[i];
  31. for(int i=1; i<=n; ++i) printf("%d ",B[i]);
  32. return 0;
  33. }

D.Gourmet choice(拓扑排序)

容易想到拓扑一下分配数字。对于等号,就先把它们合并成一个连通块,再拓扑。

不合法情况就是同一连通块中出现了><

  1. //61ms 43900KB
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <vector>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <algorithm>
  10. #define mp std::make_pair
  11. #define pr std::pair<int,int>
  12. #define pc putchar
  13. #define gc() getchar()
  14. typedef long long LL;
  15. const int N=1e6+5,M=1e3+5;
  16. int dgr[N],bel[N],fa[N],Ans[N];
  17. char s[M][M];
  18. struct Graph
  19. {
  20. int Enum,H[N],nxt[N],to[N];
  21. inline void AE(int u,int v,int f)
  22. {
  23. dgr[v]+=f, to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
  24. }
  25. }G,T;
  26. inline int read()
  27. {
  28. int now=0,f=1;register char c=gc();
  29. for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
  30. for(;isdigit(c);now=now*10+c-48,c=gc());
  31. return now*f;
  32. }
  33. int Find(int x)
  34. {
  35. return x==fa[x]?x:fa[x]=Find(fa[x]);
  36. }
  37. bool Check(int n,int m,int cnt)
  38. {
  39. static int q[N];
  40. int tot=n+m,h=0,t=0;
  41. for(int i=1; i<=n; ++i)
  42. for(int j=1; j<=m; ++j)
  43. if(bel[i]==bel[j+n]&&s[i][j]!='=') return 0;
  44. for(int i=1; i<=cnt; ++i) if(!dgr[i]) q[t++]=i, Ans[i]=1;
  45. while(h<t)
  46. {
  47. int x=q[h++];
  48. for(int i=T.H[x],v; i; i=T.nxt[i])
  49. {
  50. v=T.to[i], Ans[v]=std::max(Ans[v],Ans[x]+1);
  51. if(!--dgr[v]) q[t++]=v;
  52. }
  53. }
  54. if(t<cnt) return 0;
  55. puts("Yes");
  56. for(int i=1; i<=n; ++i) printf("%d ",Ans[bel[i]]);
  57. puts("");
  58. for(int i=n+1; i<=tot; ++i) printf("%d ",Ans[bel[i]]);
  59. return t==cnt;
  60. }
  61. int main()
  62. {
  63. int n=read(),m=read(),tot=n+m;
  64. for(int i=1; i<=tot; ++i) fa[i]=i;
  65. for(int i=1; i<=n; ++i)
  66. {
  67. scanf("%s",s[i]+1);
  68. for(int j=1; j<=m; ++j)
  69. if(s[i][j]=='>') G.AE(j+n,i,0);
  70. else if(s[i][j]=='<') G.AE(i,j+n,0);
  71. else fa[Find(i)]=Find(j+n);
  72. }
  73. int cnt=0;
  74. for(int i=1; i<=tot; ++i) if(fa[i]==i) bel[i]=++cnt;
  75. for(int i=1; i<=tot; ++i) if(fa[i]!=i) bel[i]=bel[Find(i)];
  76. for(int x=1; x<=tot; ++x)
  77. for(int i=G.H[x],v; i; i=G.nxt[i])
  78. if(bel[x]!=bel[v=G.to[i]]) T.AE(bel[x],bel[v],1);
  79. if(!Check(n,m,cnt)) puts("No");
  80. return 0;
  81. }

E.String Multiplication(思路)

从后往前对最后一个字符串\(p_n\)的前后缀是否相同讨论一下。具体看这里吧不想写了= =

可以像上面那样递归去做,也可以从\(p_1\)往后递推。因为用到的是某个字符的最长连续子串,所以令\(f[i][j]\)表示\(p_1\cdot p_2\cdot...\cdot p_i\)中\(j\)字符的最长连续长度,转移时判一下\(p_{i+1}\)是否都是由\(j\)字符构成的,是就用\(len\times(f[i][j]+1)+f[i][j]\)更新,否则用\(1+j字符的最长前后缀\)更新(\(p_{i-1}\)中含\(j\)字符才能转移)。

复杂度\(O(\sum|p_i|)\)(递归)或\(O(26\sum|p_i|)\)(递推)(然而前者常数比较大)。

  1. //46ms 10100KB
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define S 26
  6. typedef long long LL;
  7. const int N=1e5+5;
  8. int f[N][S],pre[S],suf[S];
  9. char s[N];
  10. void Solve(int *f)
  11. {
  12. scanf("%s",s+1);
  13. int l=strlen(s+1);
  14. for(int j=0; j<S; ++j)
  15. {
  16. int mx=0;
  17. for(int i=1,cnt=0; i<=l; ++i)
  18. if(s[i]==j+'a') mx=std::max(mx,++cnt);
  19. else cnt=0;
  20. f[j]=mx, pre[j]=suf[j]=0;
  21. for(int i=1; i<=l; ++i)
  22. if(s[i]==j+'a') ++pre[j];
  23. else break;
  24. for(int i=l; i; --i)
  25. if(s[i]==j+'a') ++suf[j];
  26. else break;
  27. }
  28. }
  29. int main()
  30. {
  31. int n; scanf("%d",&n);
  32. Solve(f[1]);
  33. for(int i=2; i<=n; ++i)
  34. {
  35. Solve(f[i]);
  36. int l=strlen(s+1);
  37. for(int j=0; j<S; ++j)
  38. if(f[i-1][j])
  39. if(pre[j]!=l) f[i][j]=std::max(f[i][j],pre[j]+suf[j]+1);
  40. else f[i][j]=std::max(f[i][j],l*(f[i-1][j]+1)+f[i-1][j]);
  41. }
  42. int ans=0;
  43. for(int i=0; i<S; ++i) ans=std::max(ans,f[n][i]);
  44. printf("%d\n",ans);
  45. return 0;
  46. }

F.Asya And Kittens(链表)

容易发现只要模拟一下按顺序合并连通块即可。可以用vector+启发式合并,也可以链表。用链表复杂度\(O(n\alpha(n))\)。

  1. //78ms 1600KB
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <vector>
  7. #include <cstring>
  8. #include <algorithm>
  9. #define mp std::make_pair
  10. #define pr std::pair<int,int>
  11. #define pc putchar
  12. #define gc() getchar()
  13. typedef long long LL;
  14. const int N=150005;
  15. int fa[N],R[N],ed[N];
  16. inline int read()
  17. {
  18. int now=0,f=1;register char c=gc();
  19. for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
  20. for(;isdigit(c);now=now*10+c-48,c=gc());
  21. return now*f;
  22. }
  23. int Find(int x)
  24. {
  25. return x==fa[x]?x:fa[x]=Find(fa[x]);
  26. }
  27. int main()
  28. {
  29. int n=read();
  30. for(int i=1; i<=n; ++i) fa[i]=ed[i]=i;
  31. for(int i=1; i<n; ++i)
  32. {
  33. int x=read(),y=read();
  34. int r1=Find(x),r2=Find(y);
  35. fa[r2]=r1, R[ed[r1]]=r2, ed[r1]=ed[r2];
  36. }
  37. for(int i=1; i<=n; ++i)
  38. if(fa[i]==i)
  39. {
  40. for(int x=i; x; x=R[x]) printf("%d ",x);
  41. break;
  42. }
  43. return 0;
  44. }

G.Most Dangerous Shark

待填坑(怕是永远也填不上了)


Codeforces Round #541 (Div. 2) (A~F)的更多相关文章

  1. Codeforces Round #541 (Div. 2)

    Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #573 (Div. 1) 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  3. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  4. Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))

    F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #541 (Div. 2)题解

    不知道该更些什么 随便写点东西吧 https://codeforces.com/contest/1131 ABC 太热了不写了 D 把相等的用并查集缩在一起 如果$ x<y$则从$ x$往$y$ ...

  6. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  7. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

  8. Codeforces Round #600 (Div. 2)E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  9. Codeforces Round #346 (Div. 2) E F

    因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...

随机推荐

  1. 攻击WordPress和其他程序

    1.SAAS服务(Software as a Service )管理其他作为内容管理的一个人框架的软件,http://www.turnkeylinx.org 上发布了很多测试应用的程序.WordPre ...

  2. 处理Task引发的异常

    处理方法:线程启动后,使用try-catch,通过wait或者waitall来捕获异常. 单个Task的情况: System.Threading.Tasks.Task task1 = new Syst ...

  3. CAS—认证原理

    CAS,Central Authentication Service—中央认证服务,是Yale 大学发起的一个企业级的.开源的项目,旨在为Web应用系统提供一种可靠的SSO解决方案.下面简单介绍SSO ...

  4. Spring Boot学习--项目启动时执行指定service的指定方法

    Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...

  5. Facebook的React Native之所以能打败谷歌的原因有7个(ReactNative vs Flutter)

    https://baijiahao.baidu.com/s?id=1611028483072699113&wfr=spider&for=pc 如果你喜欢用(或希望能够用)模板搭建应用, ...

  6. dubbo的工作原理

    dubbo工作原理 第一层:service层,接口层,给服务提供者和消费者来实现的 第二层:config层,配置层,主要是对dubbo进行各种配置的 第三层:proxy层,服务代理层,透明生成客户端的 ...

  7. 網管利器!開源管理系統-LibreNMS

    https://www.4rbj4.com/442 https://www.ichiayi.com/wiki/tech/librenms

  8. pp 总结一

    1.JQ $.get() <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  9. ubuntu安装php+mysql+apche

    步骤一,安装apache2 ? sudo apt-get install apache2 安装完成. 运行如下命令重启下: ? sudo /etc/init.d/apache2 restart 在浏览 ...

  10. Temporal Segment Networks

    摘要 解决问题 用CNN框架有效提取video长时序特征 在UCF101等训练集受限的情况下训练网络 贡献 TSN网络,基于长时间时序结构模型.稀疏时序采样策略,视频层监督有效学习整个视频. HMDB ...