Codeforces Round #224 (Div. 2)(数学、dfs)
来源:互联网 发布:返利软件有哪些 编辑:程序博客网 时间:2024/05/01 00:21
A.Ksenia and Pan Scales
好久没写代码了,写的太烂了。。。
#include<iostream>#include<string>#include<cstdio>using namespace std;int Abs(int x){ return x>0?x:(-x);}int main(){ //freopen("in.txt","r",stdin); string a,b; int numl=0,numr=0; bool flag=false; cin>>a>>b; for(int i=0;i<a.size();i++) { if(!flag&&a[i]!='|') numl++; else if(a[i]=='|') { flag=true; } else numr++; } if(Abs(numr-numl)<=b.size()) { int m=Abs(numr-numl); int p=m+b.size(); if(p%2==0) { if(m==b.size()) { if(numl>numr)cout<<a<<b; else cout<<b<<a; } else { string tmp(b.begin(),b.begin()+p/2); string tmp1(b.begin()+p/2,b.end()); if(numl<numr)cout<<tmp<<a<<tmp1<<endl; else cout<<tmp1<<a<<tmp<<endl; } } else cout<<"Impossible"<<endl; } else cout<<"Impossible"<<endl; return 0;}
B. Number Busters
题意:求经过多少时间c<=a,c和a都有变换的方式。
思路:找循环节,c每秒都减1,a在b<x的时候减1,并且b = b + w - x,在b>=x的时候不变化,并且b = b - x..也就是a和c只有在b = b - x的时候差距才会减少1,所以b一共减了(c-a)次x, 设加了k次w - x.那么得到b最后的值为b - x * (c - a) + k * (w - x).然后b最后的值+x 必然不小于x(因为b >= x 才是执行b = b - x)..所以得到公式b - x * (c - a) + k *(w - x) + x >= x ==> k = (ceil)((b - x * (c - a))/(w - x)).
刚开始自己写了一个半暴力,挂了,果断超时,却只需要一个小小的公式,弱爆了。
#include<iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;long long a, b, w, x, c;long long solve() {if (c <= a) return 0;long long ans = (long long)ceil((x * (c - a) - b) * 1.0 / (w - x)) + (c - a);return ans;}int main() {cin>>a>>b>>w>>x>>c;cout<<solve()<<endl;return 0;}
C. Arithmetic Progression
给你n个数,让你添加一个数使其成为等差数列,输出所有符合要求的数。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=100010;int n,a[maxn];int main(){ //freopen("in.txt","r",stdin); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; if(n==1){cout<<-1<<endl;return 0;} sort(a+1,a+n+1); int num=0,x=a[2]-a[1],y=-1,numx=1,numy=0,biao; for(int i=3;i<=n;i++) { int tmp=a[i]-a[i-1]; if(tmp==x)numx++; else if(tmp!=x&&y==-1){y=tmp;biao=i-1;numy++;} else if(tmp!=x&&tmp==y)numy++; else if(tmp!=x&&tmp!=y) {cout<<0<<endl;return 0;} } if(numx>1&&numy>1){cout<<0;return 0;} if(numx==1&&x>y)biao=1; if((numx==1&&numy==1)||(numx==1&&numy>1&&x>y)||(numy==1&&x<y)) { int p=min(x,y); int q=max(x,y); if(p*2!=q){cout<<0<<endl;return 0;} cout<<1<<endl<<a[biao]+p<<endl; } else if(numy==0) { if(x==0){cout<<1<<endl<<a[1]<<endl;return 0;} int tmp=a[2]-a[1]; if(n==2&&(tmp)%2==0) cout<<3<<endl<<a[1]-tmp<<" "<<a[1]+tmp/2<<" "<<a[2]+tmp<<endl; else cout<<2<<endl<<a[1]-tmp<<" "<<a[n]+tmp<<endl; } else cout<<0<<endl; return 0;}
D. Ksenia and Pawns
思路:先对每个格子进行dfs记录下从当前格子直到遇到#可以走多少步,并举录下最大的步数的格子,然后验证最大的格子是否有相交的时候。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int MAXN=2010;const int INF=1000000000;struct Node{ int x,y;} node[2*MAXN];char grid[MAXN][MAXN];int num[MAXN][MAXN];bool vis[MAXN][MAXN];int n,m,ansn,k,flag;void init(){ memset(vis,false,sizeof(vis)); memset(num,-1,sizeof(num));}int dfs(int x,int y){ int t; if(num[x][y]!=-1)return num[x][y]; if(grid[x][y]=='^') { if(vis[x-1][y]) return INF; vis[x-1][y]=1; t=dfs(x-1,y); vis[x-1][y]=0; } else if(grid[x][y]=='v') { if(vis[x+1][y]) return INF; vis[x+1][y]=1; t=dfs(x+1,y); vis[x+1][y]=0; } else if(grid[x][y]=='<') { if(vis[x][y-1]) return INF; vis[x][y-1]=1; t=dfs(x,y-1); vis[x][y-1]=0; } else if(grid[x][y]=='>') { if(vis[x][y+1]) return INF; vis[x][y+1]=1; t=dfs(x,y+1); vis[x][y+1]=0; } else t=-1; num[x][y]=t+1; return num[x][y];}void dfs1(int x,int y){ if(grid[x][y]=='#') { if(k) flag=1; return ; } vis[x][y]=1; if(grid[x][y]=='^') { if(!vis[x-1][y]) dfs1(x-1,y); } else if(grid[x][y]=='v') { if(!vis[x+1][y]) dfs1(x+1,y); } else if(grid[x][y]=='<') { if(!vis[x][y-1]) dfs1(x,y-1); } else if(grid[x][y]=='>') { if(!vis[x][y+1]) dfs1(x,y+1); }}void solve(){ int tmp,cnt=0,ans=-1; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(grid[i][j]=='#')continue; vis[i][j]=true; tmp=dfs(i,j); vis[i][j]=0; if(tmp>ans) { ans=tmp; cnt=1; node[cnt].x=i,node[cnt].y=j; } else if(tmp==ans) { node[++cnt].x=i,node[cnt].y=j; } if(ans>=INF) { ansn=-1; return; } } } if(ans==-1) { ansn=0; return; } memset(vis,0,sizeof(vis)); k=0; dfs1(node[1].x,node[1].y); flag=0; k=1; for(int i=2; i<=cnt; i++) { dfs1(node[i].x,node[i].y); if(flag) break ; } ansn=2*ans-1+flag;}int main(){ //freopen("in.txt","r",stdin); cin>>n>>m; for(int i=1; i<=n; i++) cin>>grid[i]+1; init(); solve(); cout<<ansn<<endl; return 0;}
0 0
- Codeforces Round #224 (Div. 2)(数学、dfs)
- Codeforces Round #428 (Div. 2) Journey(数学期望+dfs)
- Codeforces Round #369 (Div. 2) D. Directed Roads (dfs+组合数学 图论)
- Codeforces Round #224 (Div. 2) B 数学推理
- Codeforces Round #301 C (Div. 2) 【dfs】
- Codeforces Round #306 (Div. 2) B(dfs)
- Codeforces Round #316 (Div. 2)dfs序
- Codeforces Round #346 (Div. 2) E (dfs)
- Codeforces Round #356 (Div. 2) D DFS
- Codeforces Round #357 (Div. 2) D DFS
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #359 (Div. 2) D DFS
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #306 (Div. 2) B DFS
- Codeforces Round #363 (Div. 2) D DFS
- Codeforces Round #364 (Div. 2) E DFS
- Codeforces Round #369 (Div. 2) D DFS
- Codeforces Round #376 (Div. 2)C(DFS)
- EBS OAF开发中遇到的奇葩错误JBO-25003的分析和解决
- ffmpeg参数详解
- Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd
- 解决Win8无法升级.NET Framework 3.5.1 提示错误0x800F0906
- Linux Epoll vs Windows IOCP
- Codeforces Round #224 (Div. 2)(数学、dfs)
- FOJ 1003 Counterfeit Dollar
- 直接排序
- 迟到的2013总结
- 回顾博客
- c语言return;返回什么
- 黑马程序员_7k面试题—交通灯
- android 4.4 下载文件
- POJ 1679 The Unique 次最小生成树 MST