Codeforces Round #301 (Div. 2 ),
A. Combination LockTime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
The combination lock is representedNRotating disks with digits from 0 to 9 written on them. scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. in one move, he can rotate one disk one digit forwards or backwards. in particle, in one move he can go from digit 0 to digit 9 and vice versa. what minimum number of actions does he need for that?
Input
The first line contains a single integerN(1 digit ≤ DigitNMemory ≤ memory 1000)-the number of disks on the combination lock.
The second line contains a stringNDigits-the original state of the disks.
The third line contains a stringNDigits-Scrooge McDuck's combination that opens the lock.
Output
Print a single integer-the minimum number of moves Scrooge McDuck needs to open the lock.
Sample test (s) Input
58219564723
Output
13
Link: http://codeforces.com/contest/540/problem/A
Code List:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;const int maxn = 50000 +5;int n,ans;string s1,s2;int main(){ cin>>n; cin>>s1>>s2; ans=0; for(int i=0;i<n;i++){ int x=s1[i]-'0'; int y=s2[i]-'0'; ans+=min(abs(x-y),10-abs(x-y)); } cout<<ans<<endl;}
B. School MarksTime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Little Vova studies programming in an elite school. Vova and his classmates are supposed to writeNProgress tests, for each test they will get a mark from 1P. Vova is very smart and he can write every test for any mark, but he doesn' t want to stand out from the crowd too much. if the sum of his marks for all tests exceeds valueX, Then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lowerYPoints (the definition of a median is given in the notes), then his mom will decide that he gets too bad marks and forbid him to play computer games.
Vova has already wroteKTests and got marksA1, middle..., middle ,...,AK. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.
Input
The first line contains 5 space-separated integers:N,K,P,XAndY(1 digit ≤ DigitNLimit ≤ limit 999,NIs odd, 0 records ≤ bytesKLatency <latencyN, 1 limit ≤ limitPLimit ≤ limit 1000,NLimit ≤ limitXLimit ≤ limitN·P, 1 limit ≤ limitYLimit ≤ limitP). HereNIs the number of tests that Vova is planned to write,KIs the number of tests he has already written,PIs the maximum possible mark for a test,XIs the maximum total number of points so that the classmates don't yet disturb Vova,YIs the minimum median point so that mom still lets him play computer games.
The second line containsKSpace-separated integers:A1, middle..., middle ,...,AK(1 digit ≤ DigitAILimit ≤ limitP)-The marks that Vova got for the tests he has already written.
Output
If Vova cannot achieve the desired result, print "-1 ".
Otherwise, printNAccept-Encoding-KSpace-separated integers-the marks that Vova shocould get for the remaining tests. If there are multiple possible solutions, print any of them.
Sample test (s) Input
5 3 5 18 43 5 4
Output
4 1
Input
5 3 5 16 45 5 5
Output
-1
Link: http://codeforces.com/contest/540/problem/ B
Code List:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;const int maxn = 1000 + 5;int n,k,p,x,y;int cnt[maxn];int Min,Max,sum;int main(){ cin>>n>>k>>p>>x>>y; Min=0; Max=0; sum=0; for(int i=0;i<k;i++){ cin>>cnt[i]; sum+=cnt[i]; if(cnt[i]>=y) Max++; if(cnt[i]<y) Min++; } if(Max>(n-1)/2){ if(sum+n-k>x) cout<<"-1"<<endl; else{ for(int i=0;i<n-k;i++) cout<<"1"<<" "; cout<<endl; } } else{ int d=(n-1)/2-Min; if(d<0 || d+(n-d-k)*y+sum>x) cout<<"-1"<<endl; else{ for(int i=0;i<d;i++) cout<<"1"<<" "; for(int i=0;i<n-k-d;i++) cout<<y<<" "; cout<<endl; } } return 0;}
C. Ice CaveTime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
You play a computer game. your character stands on some level of a multilevel ice cave. in order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are a rectangular square gridNRows andMColumns. each cell consists either from intact or from cracked ice. from each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, I. e. jump from a cell to itself ). if you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let's number the rows with integers from 1NFrom top to bottom and the columns with integers from 1MFrom left to right. Let's denote a cell on the intersection ofR-Th row andC-Th column (R, Bytes,C).
You are staying in the cell (R1, bytes,C1) and this cell is cracked because you 've just fallen here from a higher level. You need to fall down through the cell (R2, bytes,C2) since the exit to the next level is there. Can you do this?
Input
The first line contains two integers,NAndM(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 500)-the number of rows and columns in the cave description.
Each of the nextNLines describes the initial state of the level of the cave, each line consistsMCharacters "." (that is, intact ice) and "X" (cracked ice ).
The next line contains two integers,R1 andC1 (1 digit ≤ DigitR1 bytes ≤ bytesN, Memory 1 ≤ memoryC1 bytes ≤ bytesM)-Your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (R1, bytes,C1), that is, the ice on the starting cell is initially cracked.
The next line contains two integersR2 andC2 (1 digit ≤ DigitR2 bytes ≤ bytesN, Memory 1 ≤ memoryC2 bytes ≤ bytesM)-The coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
Output
If you can reach the destination, print 'yes', otherwise print 'no '.
Sample test (s) Input
4 6X...XX...XX..X..X.......1 62 2
Output
YES
Input
5 4.X.....XX.X......XX.5 31 1
Output
NO
Input
4 7..X.XX..XX..X.X...X..X......2 21 6
Output
YES
A map consists of two types of ice cubes. 'X' indicates the ice cubes that are broken upon a step, and '.' indicates the ice cubes that are broken only after two steps. Now let's give you the starting and ending points and ask if you can reach the ending point and the ice will break down when you reach the ending point.
Analysis: dfs or bfs.
Link: http://codeforces.com/contest/540/problem/C
Code List:
Dfs
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;const int maxv = 500 + 5;int n,m;int sx,sy,ex,ey;char s[maxv][maxv];bool ok=false;int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};bool judge(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return true; return false;}void dfs(int x,int y){ if(x==ex&&y==ey){ ok=true; return ; } for(int i=0;i<4;i++){ int rx=x+xy[i][0]; int ry=y+xy[i][1]; if(judge(rx,ry)&&s[rx][ry]=='.'){ s[rx][ry]='X'; dfs(rx,ry); if(ok) return ; } }}int getNum(int x,int y){ int sum=0; for(int i=0;i<4;i++){ int rx=x+xy[i][0]; int ry=y+xy[i][1]; if(judge(rx,ry)&&s[rx][ry]=='.') sum++; } return sum;}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",s[i]); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); sx--; sy--; ex--; ey--; s[sx][sy]='.'; int sun=getNum(ex,ey); if(sx==ex&&sy==ey){ if(sun>0) printf("YES\n"); else printf("NO\n"); } else{ int check=0; if(s[ex][ey]=='X'){ s[ex][ey]='.'; check=1; } dfs(sx,sy); if(ok&&(check||sun>1)) printf("YES\n"); else printf("NO\n"); } return 0;}
Bfs
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;const int maxv = 500 + 5;int n,m;int sx,sy,ex,ey;char s[maxv][maxv];bool ok=false,vis[maxv][maxv];int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};typedef pair<int,int>P;bool judge(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return true; return false;}void bfs(){ queue<P>q; while(q.size()) q.pop(); vis[sx][sy]=true; q.push(P(sx,sy)); while(q.size()){ P p=q.front(); q.pop(); if(p.first==ex&&p.second==ey){ ok=true; break; } for(int i=0;i<4;i++){ int rx=p.first+xy[i][0]; int ry=p.second+xy[i][1]; if(judge(rx,ry)&&!vis[rx][ry]&&s[rx][ry]=='.'){ vis[rx][ry]=true; q.push(P(rx,ry)); } } }}int getNum(int x,int y){ int sum=0; for(int i=0;i<4;i++){ int rx=x+xy[i][0]; int ry=y+xy[i][1]; if(judge(rx,ry)&&s[rx][ry]=='.') sum++; } return sum;}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",s[i]); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); sx--; sy--; ex--; ey--; s[sx][sy]='.'; int sun=getNum(ex,ey); if(sx==ex&&sy==ey){ if(sun>0) printf("YES\n"); else printf("NO\n"); } else{ int check=0; if(s[ex][ey]=='X'){ s[ex][ey]='.'; check=1; } bfs(); if(ok&&(check||sun>1)) printf("YES\n"); else printf("NO\n"); } return 0;}