Codeforces Round #301 (Div. 2)

来源:互联网 发布:眼镜 知乎 编辑:程序博客网 时间:2024/06/04 00:48



#include<iostream>#include<algorithm>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define maxn 1005#define inf 0x3f3f3f3fchar s1[maxn],s2[maxn];int main(){    int n;    cin>>n;    cin>>s1>>s2;    int d=0;    for(int i=0;i<n;i++){        if(s1[i]<=s2[i])d+=min(s2[i]-s1[i],s1[i]+10-s2[i]);        else d+=min(s1[i]-s2[i],s2[i]+10-s1[i]);    }    cout<<d<<endl;}

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: nkpx and y (1 ≤ n ≤ 999n is odd, 0 ≤ k < n1 ≤ p ≤ 1000n ≤ x ≤ n·p1 ≤ 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.

Sample test(s)
input
5 3 5 18 43 5 4
output
4 1
input
5 3 5 16 45 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个数,先给出p个数,现在要求另外n-p个数,使得这n个数的中位数大于等于y,并且n个数之和不大于x。没有方案就输出-1.

代码:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <algorithm>  
  5. #include <cmath>  
  6. #include <string>  
  7. #include <map>  
  8. #include <stack>  
  9. #include <vector>  
  10. #include <set>  
  11. #include <queue>  
  12. #pragma comment (linker,"/STACK:102400000,102400000")  
  13. #define maxn 1005  
  14. #define MAXN 2005  
  15. #define mod 1000000009  
  16. #define INF 0x3f3f3f3f  
  17. #define pi acos(-1.0)  
  18. #define eps 1e-6  
  19. #define lson rt<<1,l,mid  
  20. #define rson rt<<1|1,mid+1,r  
  21. #define FRE(i,a,b)  for(i = a; i <= b; i++)  
  22. #define FREE(i,a,b) for(i = a; i >= b; i--)  
  23. #define FRL(i,a,b)  for(i = a; i < b; i++)  
  24. #define FRLL(i,a,b) for(i = a; i > b; i--)  
  25. #define mem(t, v)   memset ((t) , v, sizeof(t))  
  26. #define sf(n)       scanf("%d", &n)  
  27. #define sff(a,b)    scanf("%d %d", &a, &b)  
  28. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)  
  29. #define pf          printf  
  30. #define DBG         pf("Hi\n")  
  31. typedef long long ll;  
  32. using namespace std;  
  33.   
  34. int n,p,k,x,y;  
  35. int a[maxn];  
  36. int ans[maxn];  
  37.   
  38. int main()  
  39. {  
  40. //    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);  
  41.     int i,j;  
  42.     while (~scanf("%d%d%d%d%d",&n,&p,&k,&x,&y))  
  43.     {  
  44.         int s=0,l=0,r=0,num=0,cnt=0;  
  45.         for (i=0;i<p;i++)  
  46.         {  
  47.             scanf("%d",&a[i]);  
  48.             s+=a[i];  
  49.             if (a[i]<y) l++; //记录比y小的数有多少  
  50.             else if (a[i]>=y) r++;//记录大于等于y的数有多少  
  51.         }  
  52.         if (l>=n/2+1){  //如果比y小的数超过了一半,那么中位数不可能达到y了  
  53.             printf("-1\n");  
  54.             continue;  
  55.         }  
  56.         int xx=n/2+1-r;  
  57.         while (xx>0){  //填充右边,尽量填y使得和最小  
  58.             ans[cnt++]=y;  
  59.             xx--;s+=y;  
  60.         }  
  61.         xx=n-r-l-cnt;  
  62.         while (xx>0)    //填充左边,填1使和尽量小  
  63.         {  
  64.             ans[cnt++]=1;  
  65.             xx--;s++;  
  66.         }  
  67.         if (s>x){   //和超过x输出-1  
  68.             printf("-1\n");  
  69.             continue;  
  70.         }  
  71.         printf("%d",ans[0]);  
  72.         for (i=1;i<cnt;i++)  
  73.             printf(" %d",ans[i]);  
  74.         printf("\n");  
  75.     }  
  76.     return 0;  
  77. }  




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'.

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
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.




#include<iostream>#include<algorithm>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespace std;#define maxn 505#define inf 0x3f3f3f3fint n,m;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};char s[maxn][maxn];int vis[maxn][maxn];int r1,c1,r2,c2;struct point{    int x,y;    point(int x,int y):x(x),y(y){};    point(){};}st,en;bool judge(int x,int y){    if(x>=1&&y>=1&&x<=n&&y<=m)return true;    return false;}bool bfs(){    queue<point>q;    st.x=r1,st.y=c1;    q.push(st);    point temp;    while(!q.empty()){        temp=q.front();        q.pop();        for(int i=0;i<4;i++){            int nx=temp.x+dir[i][0];            int ny=temp.y+dir[i][1];            if(judge(nx,ny)&&(s[nx][ny]=='.'||(nx==r2&&ny==c2))){                if(nx==r2&&ny==c2&&s[nx][ny]=='X')return true;                s[nx][ny]='X';            <span style="font-family: Arial, Helvetica, sans-serif;">//bfs不存在回溯的问题,因为bfs是同时扩散开去的,</span>                q.push(point(nx,ny));            }        }    }    return false;}int main(){    //freopen("in.txt","r",stdin);    while(cin>>n>>m){    for(int i=1;i<=n;i++){        scanf("%s",s[i]+1);    }    cin>>r1>>c1>>r2>>c2;    if(bfs()){        printf("YES\n");    }    else printf("NO\n");    }}


0 0