来自FallDream的博客,未经允许,请勿转载,谢谢,


第一次在cf上打acm...和同校大佬组队打

总共15题,比较鬼畜,最后勉强过了10题。

AB一样的题目,不同数据范围,一起讲吧

你有一个背包,最多装k本书,你在第i天需要拥有一本编号ai的书,但是你可以去商店购买并加入背包。

问你至少要买多少本书?n,k<=400000

显然可以贪心,留下下一次需要的时间最早的书就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #define pa pair<int,int>
  5. #define mp(x,y) make_pair(x,y)
  6. #define INF 2000000000
  7. #define MN 400000
  8. using namespace std;
  9. inline int read()
  10. {
  11. int x = , f = ; char ch = getchar();
  12. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  13. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  14. return x * f;
  15. }
  16.  
  17. priority_queue<pa> q;
  18. int n,k,a[MN+],ne[MN+],la[MN+],ans=;
  19. bool in[MN+];
  20.  
  21. int main()
  22. {
  23. n=read();k=read();
  24. for(int i=;i<=n;++i) a[i]=read();
  25. for(int i=n;i;--i)
  26. {
  27. ne[i]=la[a[i]]?la[a[i]]:INF;
  28. la[a[i]]=i;
  29. }
  30. for(int i=;i<=n;++i)
  31. {
  32. if(in[a[i]])
  33. ++k,q.push(mp(ne[i],a[i]));
  34. else
  35. {
  36. ++ans;
  37. while(q.size()==k) in[q.top().second]=,q.pop();
  38. q.push(mp(ne[i],a[i]));
  39. in[a[i]]=;
  40. }
  41.  
  42. }
  43. cout<<ans;
  44. return ;
  45. }

C. 还是同样的题面,只不过每本书要的钱不一样了,n,k<=80

把一本书留到下一次用看作一个区间覆盖,发现这就是一道k可重区间问题,费用流建图即可

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #define S 0
  6. #define MN 80
  7. #define T 81
  8. #define INF 2000000000
  9. using namespace std;
  10. inline int read()
  11. {
  12. int x = , f = ; char ch = getchar();
  13. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  14. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  15. return x * f;
  16. }
  17.  
  18. int d[MN+],n,k,head[MN+],cnt=,a[MN+],c[MN+],ans=,pi=,la[MN+];
  19. struct edge{int to,next,w,c;}e[MN*MN];
  20. bool inq[MN+],mark[MN+];
  21. deque<int> q;
  22.  
  23. inline void ins(int f,int t,int w,int c)
  24. {
  25. e[++cnt]=(edge){t,head[f],w,c}; head[f]=cnt;
  26. e[++cnt]=(edge){f,head[t],,-c};head[t]=cnt;
  27. }
  28.  
  29. bool modlabel()
  30. {
  31. for(int i=S;i<=T;++i) d[i]=INF;
  32. d[T]=;inq[T]=;q.push_front(T);
  33. while(!q.empty())
  34. {
  35. int x=q.front();q.pop_front();
  36. for(int i=head[x];i;i=e[i].next)
  37. if(e[i^].w&&e[i^].c+d[x]<d[e[i].to])
  38. {
  39. d[e[i].to]=d[x]+e[i^].c;
  40. if(!inq[e[i].to])
  41. {
  42. inq[e[i].to]=;
  43. if(d[e[i].to]<d[q.size()?q.front():]) q.push_front(e[i].to);
  44. else q.push_back(e[i].to);
  45. }
  46. }
  47. inq[x]=;
  48. }
  49. for(int i=S;i<=T;++i)
  50. for(int j=head[i];j;j=e[j].next)
  51. e[j].c+=d[e[j].to]-d[i];
  52. return pi+=d[S],d[S]<INF;
  53. }
  54.  
  55. int dfs(int x,int f)
  56. {
  57. if(x==T) return ans+=pi*f,f;
  58. int used=;mark[x]=;
  59. for(int i=head[x];i;i=e[i].next)
  60. if(e[i].w&&!e[i].c&&!mark[e[i].to])
  61. {
  62. int w=dfs(e[i].to,min(f-used,e[i].w));
  63. used+=w;e[i].w-=w;e[i^].w+=w;
  64. if(used==f) return used;
  65. }
  66. return used;
  67. }
  68.  
  69. int main()
  70. {
  71. n=read();k=read()-;
  72. for(int i=;i<=n;++i) a[i]=read();
  73. for(int i=;i<=n;++i) c[i]=read();
  74. for(int i=;i<=n;++i) ans+=c[a[i]];
  75. ins(S,,k,);ins(n,T,k,);
  76. for(int i=;i<n;++i) ins(i,i+,INF,);
  77. for(int i=;i<=n;++i)
  78. {
  79. if(la[a[i]])
  80. {
  81. if(la[a[i]]==i-) ans-=c[a[i]];
  82. else ins(la[a[i]]+,i,,-c[a[i]]);
  83. }
  84. la[a[i]]=i;
  85. }
  86. while(modlabel())
  87. do memset(mark,,sizeof(mark));
  88. while(dfs(S,INF));
  89. cout<<ans;
  90. return ;
  91. }

DEF比较牛逼 D题wa了个几十次,还剩半分钟的时候居然过了  感觉这种奇奇怪怪的题并没有发言权...

G题是送分的

H题

给你一个数字n(<=1000000),要求你构造两个字符串,使得第二个字符串作为子序列在第一个字符串中的出现次数恰好是n次,且第一个字符串的长度不超过200

考虑用组合数来解决。让第二个串为'aaaaab',那么第一个串中每个B的贡献都是他前面的a的个数选出5个的组合数。只要选择适当的地方插入就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #define MN 50
  4. using namespace std;
  5. inline int read()
  6. {
  7. int x = , f = ; char ch = getchar();
  8. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  9. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  10. return x * f;
  11. }
  12.  
  13. int num[MN+],n,C[MN+][];
  14.  
  15. int main()
  16. {
  17. n=read();C[][]=;
  18. for(int i=;i<=MN;++i)
  19. {
  20. C[i][]=;
  21. for(int j=;j<=;++j)
  22. C[i][j]=C[i-][j]+C[i-][j-];
  23. }
  24. for(int i=MN;i>=;--i)
  25. while(n>=C[i][]) n-=C[i][],++num[i];
  26. for(int i=;i<=MN;putchar('a'),++i)
  27. for(int j=;j<=num[i];++j) putchar('b');
  28. printf(" aaaaab");
  29. return ;
  30. }

I

给定一个长度为n字符串,求每个不同子串的出现次数的平方。

T(T<=10)组数据,n<=100000

建出后缀自动机之后,简单dp一下就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. inline int read()
  6. {
  7. int x = , f = ; char ch = getchar();
  8. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  9. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  10. return x * f;
  11. }
  12. int c[][],step[],fail[];
  13. long long val[];long long sum[];
  14. char s[];
  15. int cnt=,last=,n;
  16.  
  17. void ins(int x)
  18. {
  19. int p=last,np=++cnt;step[np]=step[last]+;val[np]=;
  20. for(;p&&!c[p][x];p=fail[p])c[p][x]=np;
  21. if(!p)fail[np]=;
  22. else
  23. {
  24. int q=c[p][x];
  25. if(step[q]==step[p]+) fail[np]=q;
  26. else
  27. {
  28. int nq=++cnt;step[nq]=step[p]+;
  29. for(int i=;i<;i++)c[nq][i]=c[q][i];
  30. fail[nq]=fail[q];fail[q]=fail[np]=nq;
  31. for(;c[p][x]==q;p=fail[p])c[p][x]=nq;
  32. }
  33. }
  34. last=np;
  35. }
  36.  
  37. int v[],rk[];
  38. void work()
  39. {
  40. memset(v,,sizeof(v));
  41. for(int i=;i<=cnt;i++)v[step[i]]++;
  42. for(int i=;i<=n;i++)v[i]+=v[i-];
  43. for(int i=cnt;i;i--) rk[v[step[i]]--]=i;
  44. for(int i=cnt;i;i--) val[fail[rk[i]]]+=val[rk[i]];
  45. }
  46. bool vis[];
  47. long long Dfs(int x)
  48. {
  49. if(vis[x]) return sum[x];
  50. if(x!=)sum[x]=1LL*val[x]*val[x];vis[x]=;
  51. for(int i=;i<;++i) if(c[x][i])
  52. sum[x]+=Dfs(c[x][i]);
  53. return sum[x];
  54. }
  55.  
  56. int main()
  57. {
  58. for(int T=read();T;--T)
  59. {
  60. memset(c,,sizeof(c));
  61. memset(fail,,sizeof(fail));
  62. memset(step,,sizeof(step));
  63. memset(vis,,sizeof(vis));
  64. memset(sum,,sizeof(sum));
  65. memset(val,,sizeof(val));
  66. cnt=last=;
  67. scanf("%s",s+);n=strlen(s+);
  68. for(int i=;s[i];ins(s[i++]-'a'));
  69. work();
  70. cout<<Dfs()<<endl;
  71. }
  72. return ;
  73. }

J是送分的

K

给定一棵树,有边权,你要从1号点出发,要求每个点经过次数不超过k的前提下,经过的边的权值和最大(多次经过只算一次)

n<=100000

显然是一道树形dp,用f[i]表示回到i号点的最大答案,f2[i]表示不回来的最大答案,然后用优先队列保存前k大就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #define mp(x,y) make_pair(x,y)
  5. #define pa pair<int,int>
  6. #define MN 100000
  7. using namespace std;
  8. inline int read()
  9. {
  10. int x = , f = ; char ch = getchar();
  11. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  12. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  13. return x * f;
  14. }
  15.  
  16. int n,k,cnt=,f[MN+],f2[MN+],head[MN+],mark[MN+];
  17. struct edge{int to,next,w;}e[MN*+];
  18. priority_queue<pa,vector<pa>,greater<pa> > q[MN+],q2[MN+];
  19.  
  20. inline void ins(int f,int t,int w)
  21. {
  22. e[++cnt]=(edge){t,head[f],w};head[f]=cnt;
  23. e[++cnt]=(edge){f,head[t],w};head[t]=cnt;
  24. }
  25.  
  26. void Dp(int x,int fa)
  27. {
  28. for(int i=head[x];i;i=e[i].next)
  29. if(e[i].to!=fa)
  30. {
  31. Dp(e[i].to,x);
  32. q[x].push(mp(f[e[i].to]+e[i].w,e[i].to));
  33. }
  34. int mx=,mx2=,mm;
  35. while(q[x].size()>k) q[x].pop();
  36. if(q[x].size()==k) mm=q[x].top().first,mark[q[x].top().second]=x; else mm=;
  37. while(q[x].size()>k-) q[x].pop();
  38. while(!q[x].empty())
  39. {
  40. pa now=q[x].top();
  41. f[x]+=now.first;
  42. mark[now.second]=x;
  43. q[x].pop();
  44. }
  45. for(int i=head[x];i;i=e[i].next)
  46. if(e[i].to!=fa)
  47. {
  48. if(mark[e[i].to]==x)
  49. mx=max(mx,f2[e[i].to]-f[e[i].to]);
  50. else mx2=max(mx2,e[i].w+f2[e[i].to]);
  51. }
  52. f2[x]=max(f[x],max(f[x]+mx2,f[x]+mx+mm));
  53. }
  54.  
  55. int main()
  56. {
  57. n=read();k=read();
  58. for(int i=;i<n;++i)
  59. {
  60. int x=read()+,y=read()+,w=read();
  61. ins(x,y,w);
  62. }
  63. Dp(,);
  64. printf("%d\n",max(f[],f2[]));
  65. return ;
  66. }

M

给样例猜题意,输出前k小的数字的和就行了。

N

给定两个长度为n的序列,你要从每个序列中选出k个,并且满足a序列中选出的第i个的位置小等于从b序列中选出的第i个,求最小的和。

n<=2000

考虑二分一个值,并把所有数字减去那个值,通过n^2dp求出最小的情况下最多/最少选出多少个,区间包含了k时输出答案即可

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define ll long long
  5. #define MN 2200
  6. using namespace std;
  7. inline int read()
  8. {
  9. int x = , f = ; char ch = getchar();
  10. while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
  11. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  12. return x * f;
  13. }
  14.  
  15. ll f[MN+][MN+],N[MN+][MN+],N2[MN+][MN+];
  16. int a[MN+],b[MN+],n,k;
  17.  
  18. int main()
  19. {
  20. n=read();k=read();
  21. for(int i=;i<=n;++i) a[i]=read();
  22. for(int i=;i<=n;++i) b[i]=read();
  23. int l=-(1e9),r=1e9,mid;
  24. while(l<=r)
  25. {
  26. mid=1LL*l+r>>;
  27. memset(f,,sizeof(f));
  28. for(int i=;i<=n;++i) f[][i]=f[i][]=;
  29. for(int i=;i<=n;++i)
  30. for(int j=i;j<=n;++j)
  31. {
  32. if(f[i-][j]<f[i][j]) f[i][j]=f[i-][j],N[i][j]=N[i-][j],N2[i][j]=N2[i-][j];
  33. else if(f[i-][j]==f[i][j]) N[i][j]=max(N[i][j],N[i-][j]),N2[i][j]=min(N2[i][j],N2[i-][j]);
  34. if(f[i][j-]<f[i][j]) f[i][j]=f[i][j-],N[i][j]=N[i][j-],N2[i][j]=N2[i][j-];
  35. else if(f[i][j-]==f[i][j]) N[i][j]=max(N[i][j],N[i][j-]),N2[i][j]=min(N2[i][j],N2[i][j-]);
  36. if(f[i-][j-]+a[i]-mid+b[j]-mid<f[i][j])
  37. f[i][j]=f[i-][j-]+a[i]-mid+b[j]-mid,N[i][j]=N[i-][j-]+,N2[i][j]=N2[i-][j-]+;
  38. else if(f[i-][j-]+a[i]-mid+b[j]-mid==f[i][j])
  39. N[i][j]=max(N[i][j],N[i-][j-]+),N2[i][j]=min(N2[i][j],N2[i-][j-]+);
  40. }
  41. if(N[n][n]>=k&&N2[n][n]<=k) return *printf("%lld",f[n][n]+2LL*k*mid);
  42. else if(N[n][n]>k) r=mid-;
  43. else l=mid+;
  44. }
  45. return ;
  46. }

O题意相同 数据范围500000

发现这就像一个二分图匹配,并且每个b之和之前的a连边,考虑二分之后用堆来贪心。

从前往后确定每一个b的匹配,往堆里加入a,这个b只能和堆顶配对,如果更优秀的话就配对。但是这可能不是最优的,所以之后还要加入-b来表示推流,更换一个b和这个a配对。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #define ll long long
  5. #define mp(x,y) make_pair(x,y)
  6. #define pa pair<ll,int>
  7. #define MN 500000
  8. using namespace std;
  9. inline int read()
  10. {
  11. int x = ; char ch = getchar();
  12. while(ch < '' || ch > '') ch = getchar();
  13. while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
  14. return x;
  15. }
  16.  
  17. int A[MN+],B[MN+],n,m;ll tot;
  18. priority_queue<pa,vector<pa>,greater<pa> > q;
  19.  
  20. int Solve(int x)
  21. {
  22. tot=;int num=;
  23. for(int i=;i<=n;++i)
  24. {
  25. q.push(mp(A[i],));
  26. ll t=q.top().first,now=B[i]-x;
  27. if(now+t<)
  28. {
  29. tot+=now+t;
  30. q.pop();
  31. q.push(mp(-now,));
  32. }
  33. }
  34. while(!q.empty()) num+=q.top().second,q.pop();
  35. return num;
  36. }
  37.  
  38. int main()
  39. {
  40. n=read();m=read();
  41. for(int i=;i<=n;++i) A[i]=read();
  42. for(int i=;i<=n;++i) B[i]=read();
  43. int l=,r=2e9,mid;
  44. while(l<=r)
  45. {
  46. mid=1LL*l+r>>;int num=Solve(mid);
  47. if(num==m) return *printf("%lld\n",tot+1LL*m*mid);
  48. if(num<m) l=mid+;
  49. else r=mid-;
  50. }
  51. return ;
  52. }

[Helvetic Coding Contest 2017 online mirror]的更多相关文章

  1. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

    第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...

  2. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)

    G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J

    Description Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their comm ...

  4. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M

    Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...

  5. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A

    Description Your search for Heidi is over – you finally found her at a library, dressed up as a huma ...

  6. Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)

    http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...

  7. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  8. Helvetic Coding Contest 2016 online mirror A1

    Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...

  9. Helvetic Coding Contest 2016 online mirror F1

    Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...

随机推荐

  1. visualVM使用jstatd和jmx连接远程jvm及遇到的问题解决

    visualVM使用jstatd和jmx连接远程jvm及遇到的问题解决 JMX方式: 编辑Tomact里bin目录的catalina.sh . 在其头部加入 JAVA_OPTS=" -Dco ...

  2. react中的DOM操作

    前面的话 某些情况下需要在典型数据流外强制修改子代.要修改的子代可以是 React 组件实例,也可以是 DOM 元素.这时就要用到refs来操作DOM 使用场景 下面是几个适合使用 refs 的情况 ...

  3. 职场选择之大公司 VS 小公司

    其实这是个非常难回答的问题,很多职场新人都会有类似的顾虑和疑问. 这个问题就好比业界比较容易引起争议的编程语言哪个是最好的一样.大公司还是小公司里面发展,只有身处其中才能体会,如人饮水,冷暖自知. 笔 ...

  4. Django REST framework+Vue 打造生鲜超市(一)

    一.项目介绍 1.1.掌握的技术 Vue + Django Rest Framework 前后端分离技术 彻底玩转restful api 开发流程 Django Rest Framework 的功能实 ...

  5. 你考虑清楚了吗就决定用 Bootstrap ?

    近年来,在前端项目中, Bootstrap 已经成为了一个非常受欢迎的工具. Bootstrap 的确有很多优点,然而,如果你的团队中恰好有一个专职的前端工程师.那我推荐你们不要使用 Bootstra ...

  6. Spark快速入门

    Spark 快速入门   本教程快速介绍了Spark的使用. 首先我们介绍了通过Spark 交互式shell调用API( Python或者scala代码),然后演示如何使用Java, Scala或者P ...

  7. spring-oauth-server实践:使用授权方式四:client_credentials 模式下access_token做业务!!!

    spring-oauth-server入门(1-10)使用授权方式四:client_credentials 模式下access_token做业务!!! 准备工作 授权方式四::客户端方式: 服务网关地 ...

  8. nginx和nfs

    1.安装nginx #yum install epel-release -y #yum install nginx -y #vim /usr/local/nginx/conf/nginx.conf - ...

  9. AtCoder Beginner Contest 073

    D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...

  10. Typescript学习

    一 什么是Typescript 简单的说,TypeScript 是 JavaScript 的一个超集,主要提供了类型系统和对 ES6 的支持,它由 Microsoft 开发,代码开源于 GitHub  ...