A. Combination Lock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 represented by n rotating 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 particular, 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 integer n (1 ≤ n ≤ 1000) — the number of disks on the combination lock.

The second line contains a string of n digits — the original state of the disks.

The third line contains a string of n digits — 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.

Examples
Input
5
82195
64723
Output
13
Note

In the sample he needs 13 moves:

  • 1 disk:
  • 2 disk:
  • 3 disk:
  • 4 disk:
  • 5 disk:

题意:长度为n的10位锁 给你初始状态与密码 问最少需要旋转多少步

题解:水

 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 using namespace std;
 4 int  n;
 5 char a[1005];
 6 char b[1005];
 7 int main()
 8 {
 9     scanf("%d",&n);
10     scanf("%s",a);
11     scanf("%s",b);
12     int ans=0;
13     for(int i=0;i<n;i++)
14     {
15         int aa=a[i]-'0';
16         int bb=b[i]-'0';
17         if(aa<bb)
18             swap(aa,bb);
19         ans=ans+min(aa-bb,bb+(10-aa));
20     }
21     printf("%d\n",ans);
22     return 0;
23 }

 

 

B. School Marks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. 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 value x, 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 lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.

Vova has already wrote k tests and got marks a1, ..., 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, x and y (1 ≤ n ≤ 999, n is odd, 0 ≤ k < n, 1 ≤ p ≤ 1000, n ≤ x ≤ n·p, 1 ≤ y ≤ p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don't yet disturb Vova, y is the minimum median point so that mom still lets him play computer games.

The second line contains k space-separated integers: a1, ..., ak (1 ≤ ai ≤ p) — the marks that Vova got for the tests he has already written.

Output

If Vova cannot achieve the desired result, print "-1".

Otherwise, print n - k space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.

Examples
Input
5 3 5 18 4
3 5 4
Output
4 1
Input
5 3 5 16 4
5 5 5
Output
-1
Note

The median of sequence a1, ..., an where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of ai.

In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.

Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: "4 2", "2 4", "5 1", "1 5", "4 1", "1 4" for the first test is correct.

In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is "-1".

题意:要求构造一个长度为n的序列 数字范围为[1,p]  现在已经给你k个数  构造剩下的n-k个数  使得n个数的和不大于x 中位数不小于y 如果无法构造输出-1

题解:细节模拟 具体看代码

 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 using namespace std;
 4 int n,k,p,x,y;
 5 int a[1005];
 6 int main()
 7 {
 8     scanf("%d %d %d %d %d",&n,&k,&p,&x,&y);
 9     int sum=0;
10     for(int i=1; i<=k; i++){
11         scanf("%d",&a[i]);
12         sum+=a[i];
13     }
14     int gg=k;
15     if(x-sum<(n-k)){
16         printf("-1\n");
17         return 0;
18     }
19     else{
20         int j;
21         if(k>0){
22             sort(a+1,a+1+k);
23             for(j=1; j<=k; j++){
24                 if(a[j]>=y)
25                     break;
26             }
27             if(j==k+1){
28                 if(j>n){
29                     printf("-1\n");
30                     return 0;
31                 }
32                 else{
33                     a[j]=y;
34                     k++;
35                 }
36             }
37         }
38         else{
39             a[1]=y;
40             k++;
41             j=1;
42             sum+=y;
43             if(y>x){
44                 printf("-1\n");
45                 return 0;
46             }
47         }
48         int exm=n/2+1;
49         if(k-j+1>=exm){
50             for(int i=k+1; i<=n; i++)
51                 a[i]=1;
52         }
53         else{
54             int jishu=k+1;
55             int all=0;
56             all=(exm-(k-j+1))*y+(n-k-(exm-(k-j+1)));
57             if(((exm-(k-j+1))+k)>n||all+sum>x){
58                 printf("-1\n");
59                 return 0;
60             }
61             else{
62                 for(int i=1; i<=exm-(k-j+1); i++)
63                     a[jishu++]=y;
64                 for(int j=jishu; j<=n; j++)
65                     a[j]=1;
66             }
67         }
68     }
69     for(int i=gg+1; i<=n; i++)
70         printf("%d ",a[i]);
71     printf("\n");
72     return 0;
73 }
74 /*
75 5 3 5 18 4
76 3 5 4
77 
78 5 3 5 1 4
79 3 5 4
80 
81 5 4 5 10 1
82 1 2 2 2
83 
84 1 0 3 2 3
85 
86 5 3 5 17 4
87 5 5 5
88  */

 

C. Ice Cave
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 is a rectangular square grid of n rows and m columns. 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 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

You are staying in the cell (r1, 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, c2) since the exit to the next level is there. Can you do this?

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — 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'.

Examples
Input
4 6
X...XX
...XX.
.X..X.
......
1 6
2 2
Output
YES
Input
5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1
Output
NO
Input
4 7
..X.XX.
.XX..X.
X...X..
X......
2 2
1 6
Output
YES
Note

In the first sample test one possible path is:

After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.

题意:给你一个n*m的矩阵 ‘X’代表破碎的冰块 不能踩 ‘.’代表冰块 可以踩一次 给你起点与终点 问你是否能从终点落下去 每次只能移动到周围的冰块

题解:bfs

 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 using namespace std;
 4 int n,m;
 5 char mp[505][505];
 6 int visit[505][505];
 7 int x1,y1,x2,y2;
 8 struct node
 9 {
10     int x,y;
11 }now,exm;
12 queue<node> q;
13 int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
14 bool bfs()
15 {
16     while(!q.empty())
17         q.pop();
18     now.x=x1;
19     now.y=y1;
20     visit[x1][y1]=1;
21     q.push(now);
22     while(!q.empty())
23     {
24         now=q.front();
25         q.pop();
26         if(visit[x2][y2]>=2){
27           return true;
28         }
29         for(int i=0;i<4;i++)
30         {
31             int aa=now.x+dis[i][0];
32             int bb=now.y+dis[i][1];
33             if((aa>=1&&aa<=n&&bb>=1&&bb<=m)&&(mp[aa][bb]=='.'&&visit[aa][bb]==0)||(aa==x2&&bb==y2))
34             {
35                 exm.x=aa;
36                 exm.y=bb;
37                 q.push(exm);
38                 visit[aa][bb]++;
39             }
40         }
41     }
42     return false;
43 }
44 int main()
45 {
46     memset(visit,0,sizeof(visit));
47     scanf("%d %d",&n,&m);
48     for(int i=1;i<=n;i++){
49         scanf("%s",mp[i]+1);
50         }
51         for(int i=1;i<=n;i++)
52         {
53             for(int j=1;j<=m;j++)
54             {
55                 if(mp[i][j]=='X')
56                     visit[i][j]++;
57             }
58         }
59     scanf("%d %d",&x1,&y1);
60     scanf("%d %d",&x2,&y2);
61     if(bfs())
62         printf("YES\n");
63     else
64         printf("NO\n");
65     return 0;
66 }

 

D. Bad Luck Island
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors and p papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time.

Input

The single line contains three integers r, s and p (1 ≤ r, s, p ≤ 100) — the original number of individuals in the species of rock, scissors and paper, respectively.

Output

Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10 - 9.

Examples
Input
2 2 2
Output
0.333333333333 0.333333333333 0.333333333333
Input
2 1 2
Output
0.150000000000 0.300000000000 0.550000000000
Input
1 1 3
Output
0.057142857143 0.657142857143 0.285714285714

题意:
题解:
//ing
 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 using namespace std;
 4 double dp[150][150][150];
 5 double a,b,c;
 6 int main()
 7 {
 8     int r,s,p;
 9     scanf("%d %d %d",&r,&s,&p);
10     dp[r][s][p]=1;
11     for(int i=r;i>=0;i--)
12     {
13         for(int j=s;j>=0;j--)
14         {
15             for(int k=p;k>=0;k--)
16             {
17                 double all=i*j*1.0+j*k*1.0+i*k*1.0;
18                 if(all==0) continue;
19                 if(k>=1) dp[i][j][k-1]+=dp[i][j][k]*(double)(j*1.0*k)/all;
20                 if(j>=1) dp[i][j-1][k]+=dp[i][j][k]*(double)(j*1.0*i)/all;
21                 if(i>=1) dp[i-1][j][k]+=dp[i][j][k]*(double)(i*1.0*k)/all;
22             }
23         }
24     }
25     for(int i=1;i<=r;i++) a+=dp[i][0][0];
26     for(int i=1;i<=s;i++) b+=dp[0][i][0];
27     for(int i=1;i<=p;i++) c+=dp[0][0][i];
28     printf("%.9f %.9f %.9f\n",a,b,c);
29     return 0;
30 }