Codeforces #304 Div 2 简要题解
来源:互联网 发布:vb if 编辑:程序博客网 时间:2024/05/21 13:57
比赛总结
这次CF打得还不错,比上次好很多,做了4题,其中3题都是firstblood,E题最后30分钟才开始做,没做出来,B题wa了一次才ac,其间被网速杀了好几回,好在对最后的罚时影响并不是很大。
比赛提交记录:
最终比赛排名(含vp选手,共8445人)
在正式选手里排名123名(正式选手共3249人)
A. Soldier and Bananas
题目链接
http://codeforces.com/contest/546/problem/A
题目大意
买香蕉要花
思路
水题,不说了
代码
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;typedef long long int LL;int main(){ LL k,w,n; scanf("%I64d%I64d%I64d",&k,&n,&w); LL need=(k+w*k)*w/2-n; if(need<=0) printf("0\n"); else printf("%I64d\n",need); return 0;}
B. Soldier and Badges
题目链接
http://codeforces.com/contest/546/problem/B
题目大意
给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同
思路
首先对序列进行排序,排序后的序列里,我们从左到右扫一遍所有元素,若当前的元素小于等于它左边的元素,就让当前的元素变成它左边的元素大小+1,如此反复,显然可以得到正确答案
代码
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;typedef long long int LL;int n;LL val[4000];int main(){ LL ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&val[i]); sort(val+1,val+n+1); for(int i=2;i<=n;i++) if(val[i]<=val[i-1]) {ans+=val[i-1]-val[i]+1,val[i]=val[i-1]+1;} printf("%I64d\n",ans); return 0;}
C. Soldier and Cards
题目链接
http://codeforces.com/contest/546/problem/C
题目大意
两个士兵做游戏,他们手上有
思路
由于
代码
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;typedef long long int LL;int n;LL val[4000];int main(){ LL ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&val[i]); sort(val+1,val+n+1); for(int i=2;i<=n;i++) if(val[i]<=val[i-1]) {ans+=val[i-1]-val[i]+1,val[i]=val[i-1]+1;} printf("%I64d\n",ans); return 0;}
D. Soldier and Number Game
题目链接
http://codeforces.com/contest/546/problem/D
题目大意
问
思路
实际上就是问
代码
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <queue>#define MAXN 6000000using namespace std;typedef long long int LL;bool isPrime[MAXN];int sum[MAXN];int cnt[MAXN];void GetPrime(){ memset(isPrime,true,sizeof(isPrime)); isPrime[0]=isPrime[1]=false; for(int i=2;i<MAXN;i++) { if(!isPrime[i]) continue; for(int j=i+i;j<MAXN;j+=i) { isPrime[j]=false; } for(LL j=(LL)i;j<MAXN;j*=(LL)i) for(LL t=j;t<MAXN;t+=j) cnt[t]++; } for(int i=1;i<MAXN;i++) sum[i]=sum[i-1]+cnt[i];}int T;int main(){ /*for(int i=1;i<MAXN;i++) { int tmp=i,nowt=0; for(int i=2;i*i<=tmp;i++) { while(!tmp%i) tmp/=i,nowt++; } sum[i]+=nowt; }*/ GetPrime(); //cout<<tot<<endl; scanf("%d",&T); while(T--) { int a,b; scanf("%d%d",&a,&b); printf("%d\n",sum[a]-sum[b]); } return 0;}
E. Soldier and Traveling
题目链接
http://codeforces.com/contest/546/problem/E
题目大意
给你一张无向图,每个节点
思路
显然是个最大流问题。我们可以建立这样一个模型:
每个点拆成入点和出点。入点代表初始时每个点的状态,出点代表最终每个点的状态。源点向每个点
其实这样的一个模型很好建立,但是我还是太弱了,考场上没想出来。。。
代码
#include <sstream>#include <string>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#define MAXV 1000#define MAXE 3000#define MAXN 1000#define INF 0x3f3f3f3fusing namespace std;int S=MAXV-2,T=MAXV-1;struct edge{ int u,v,cap,next;}edges[MAXE];int head[MAXV],nCount=1;void AddEdge(int U,int V,int C){ edges[++nCount].u=U; edges[nCount].v=V; edges[nCount].cap=C; edges[nCount].next=head[U]; head[U]=nCount;}void add(int U,int V,int C){ AddEdge(U,V,C); AddEdge(V,U,0);}int layer[MAXV],q[MAXE*2];bool CountLayer(){ memset(layer,-1,sizeof(layer)); int h=0,t=1; q[h]=S; layer[S]=1; while(h<t) { int u=q[h++]; for(int p=head[u];p!=-1;p=edges[p].next) { int v=edges[p].v; if(layer[v]==-1&&edges[p].cap) { layer[v]=layer[u]+1; q[t++]=v; } } } return layer[T]!=-1;}int DFS(int u,int flow){ if(u==T) return flow; int used=0; for(int p=head[u];p!=-1;p=edges[p].next) { int v=edges[p].v; if(layer[v]==layer[u]+1&&edges[p].cap) { int tmp=DFS(v,min(flow-used,edges[p].cap)); used+=tmp; edges[p].cap-=tmp; edges[p^1].cap+=tmp; if(used==flow) return used; } } if(!used) layer[u]=-1; return used;}int Dinic(){ int maxflow=0; while(CountLayer()) maxflow+=DFS(S,INF); return maxflow;}int n,m;int a[MAXV],b[MAXV];bool inX[MAXV];int vec[MAXV][MAXV];int rest[MAXV];int main(){ memset(head,-1,sizeof(head)); int sumA=0,sumB=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); for(int i=1;i<=n;i++) { add(S,i,a[i]); add(i,i+n,INF); //!!!! add(i+n,T,b[i]); sumA+=a[i]; sumB+=b[i]; } for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v+n,INF); add(v,u+n,INF); } if(sumA!=sumB) { printf("NO\n"); return 0; } int mf=Dinic(); if(mf!=sumB) { printf("NO\n"); return 0; } printf("YES\n"); for(int p=2;p<=nCount;p+=2) { if(edges[p].u==S||edges[p].u==T||edges[p].v==S||edges[p].v==T) continue; vec[edges[p].u][edges[p].v-n]=edges[p^1].cap; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%d ",vec[i][j]); } printf("\n"); } return 0;}
- Codeforces #304 Div 2 简要题解
- [Codeforces #294(Div 2)]简要题解
- [Codeforces #295(Div 2)]简要题解
- [Codeforces #296 (Div. 2)]简要题解
- [Codeforces #297(Div 2)]简要题解
- Codeforces #306 Div 2 简要题解
- Codeforces #303 Div 2 简要题解
- Codeforces #301 Div 2 简要题解
- Codeforces #291 Div 2 简要题解
- Codeforces Round #259 (Div. 2) ABC简要题解
- [Codeforces #295(Div 1)]简要题解
- Codeforces #299 Div 1 简要题解
- Codeforces #292 Div 1 简要题解
- Codeforces #305 Div 1 简要题解
- Codeforces #290 Div. 1 简要题解
- Codeforces #286 Div 1 简要题解
- Codeforces #285 Div 1 简要题解
- Codeforces #284 Div 1 简要题解
- Delphi 常用API 函数
- 服务号微信订阅号是否要合并了?服务号要被集中到订阅列表中
- 缓存机制Cache ARC算法(一)
- 交换两个整数不通过中间变量
- 解决Windows x64bit环境下无法使用PLSQL Developer连接到Oracle DB中的问题
- Codeforces #304 Div 2 简要题解
- 缓存机制 Cache ARC算法(二)
- 在WEB.xml文件中配置Spring容器
- linux内核树的建立
- Ubuntu 系统的降温方案
- Linux文件查找命令find详述
- 计算机专业学生必看的网站
- 虚函数的内部实现机制
- opencv pcl confict 冲突