Codeforces Round #224 (Div. 2)(數學、dfs)
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;
}
相關文章
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #359 (Div. 2) D DFS
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- Codeforces Round #360 (Div. 2) C DFS判斷二分圖
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C 數學
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)
- Codeforces Round #173 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 948 (Div. 2)