Codeforces Round #394 (Div. 2) A,B,C,D,E
On her way to programming school tiger Dasha faced her first test — a huge staircase!
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
2 3
YES
3 1
NO
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5.
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<" "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; int main() { int a,b; scanf("%d%d",&a,&b); if(a==0&&b==0) return puts("NO"); if(abs(a-b)<=1) printf("YES\n"); else printf("NO\n"); return 0; }
暴力;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<" "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; int a[N],b[N]; int p[N],q[N]; int check(int n,int st) { int en=1; for(int i=st;i<=n;i++) if(q[i]!=p[en++]) return 0; for(int i=1;i<st;i++) if(q[i]!=p[en++]) return 0; return 1; } int main() { int n,l; scanf("%d%d",&n,&l); 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++) p[i]=a[i+1]-a[i]; p[n]=l-a[n]+a[1]; for(int i=1;i<=n;i++) q[i]=b[i+1]-b[i]; q[n]=l-b[n]+b[1]; for(int i=1;i<=n;i++) { if(check(n,i)) { return puts("YES\n"); } } puts("NO\n"); return 0; }
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:
- There is at least one digit in the string,
- There is at least one lowercase (small) letter of the Latin alphabet in the string,
- There is at least one of three listed symbols in the string: '#', '*', '&'.
Considering that these are programming classes it is not easy to write the password.
For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes1 in the corresponding strings (all positions are numbered starting from one).
During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.
You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.
The first line contains two integers n, m (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.
Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.
You have such input data that you can always get a valid password.
Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.
3 4
1**2
a3*0
c4**
1
5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&
3
In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.
In the second test one of possible algorithms will be:
- to move the pointer of the second symbol once to the right.
- to move the pointer of the third symbol twice to the right.
思路:打表找到每行可以到三种不同字符的最小距离;暴力枚举三种出现的情况
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<" "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=1e8+10,mod=1e9+7; const ll INF=1e18+10; char mp[110][110]; int n,m; int dis[110][5]; void init() { for(int i=0;i<60;i++) { for(int j=0;j<4;j++) dis[i][j]=inf; } } int ans[10]; int check(int x,int y,int z) { if(x==y||x==z||y==z) return -1; ans[1]=dis[x][1]+dis[y][2]+dis[z][3]; ans[2]=dis[x][1]+dis[y][3]+dis[z][2]; ans[3]=dis[x][2]+dis[y][1]+dis[z][3]; ans[4]=dis[x][2]+dis[y][3]+dis[z][1]; ans[5]=dis[x][3]+dis[y][1]+dis[z][2]; ans[6]=dis[x][3]+dis[y][2]+dis[z][1]; int minn=ans[1]; for(int i=1;i<=6;i++) minn=min(minn,ans[i]); return minn; } int main() { init(); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { int dis1=j-1; int dis2=m+1-j; if(mp[i][j]>='0'&&mp[i][j]<='9') dis[i][1]=min(dis1,min(dis2,dis[i][1])); else if(mp[i][j]=='*'||mp[i][j]=='&'||mp[i][j]=='#') dis[i][3]=min(dis1,min(dis2,dis[i][3])); else dis[i][2]=min(dis1,min(dis2,dis[i][2])); } } int ans=inf; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { for(int k=1;k<=n;k++) { int v=check(i,j,k); if(v!=-1) { ans=min(ans,v); } } } } printf("%d\n",ans); return 0; }
思路:模拟,排个序;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=2e5+10,M=1e6+10; const ll INF=1e18+10,mod=2147493647; int n,l,r; struct is { int a,p,pos; bool operator <(const is &b)const { return p<b.p; } }a[N]; int ans[N]; int main() { scanf("%d%d%d",&n,&l,&r); for(int i=1;i<=n;i++) scanf("%d",&a[i].a),a[i].pos=i; for(int i=1;i<=n;i++) scanf("%d",&a[i].p); sort(a+1,a+1+n); int st=l-a[1].a+1 ; ans[a[1].pos]=l; for(int i=2;i<=n;i++) { st=max(st,l-a[i].a); if(st+a[i].a>r)return puts("-1"); ans[a[i].pos]=st+a[i].a; st++; } for(int i=1;i<=n;i++) printf("%d ",ans[i]); return 0; }
题意:给你一棵树,平铺在二维坐标中,边平行x,y轴;
思路:利用类似二进制的思路;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=2e5+10,M=1e6+10; const ll INF=1e18+10,mod=2147493647; int n; struct is { int v,next; }edge[N<<1]; int edg,head[N]; int du[N]; pair<ll,ll>ans[N]; int c(int x) { if(x&1)return x+1; return x-1; } void init() { memset(head,-1,sizeof(head)); edg=0; } int flag[N][5]; void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].next=head[u]; head[u]=edg; } void dfs(int u,int fa,ll len) { for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa)continue; //cout<<u<<" "<<v<<endl; for(int i=1;i<=4;i++) { if(flag[u][i])continue; flag[v][c(i)]=1; flag[u][i]=1; //cout<<u<<" "<<v<<" "<<i<<" "<<len<<endl; if(i==1) ans[v]=make_pair(ans[u].first,ans[u].second+len); else if(i==2) ans[v]=make_pair(ans[u].first,ans[u].second-len); else if(i==3) ans[v]=make_pair(ans[u].first-len,ans[u].second); else if(i==4) ans[v]=make_pair(ans[u].first+len,ans[u].second); break; } dfs(v,u,len>>1); } } int check() { for(int i=1;i<=n;i++) if(du[i]>4) return 0; return 1; } int main() { init(); scanf("%d",&n); for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v),add(v,u); du[u]++,du[v]++; } if(check()==0)return puts("NO\n"); ans[1]=make_pair(0LL,0LL); dfs(1,-1,(1LL<<31)); printf("YES\n"); for(int i=1;i<=n;i++) printf("%lld %lld\n",ans[i].first,ans[i].second); return 0; }
On her way to programming school tiger Dasha faced her first test — a huge staircase!
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
2 3
YES
3 1
NO
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5.