Codeforces Round #301 (Div. 2)

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.

Sample test(s)
Input
5
82195
64723
Output
13

题目链接: http://codeforces.com/contest/540/problem/A
代码清单:
#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 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 writen progress tests, for each test they will get a mark from 1 top. 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 lower thany 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 marksa1, ..., 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.

Sample test(s)
Input
5 3 5 18 4
3 5 4
Output
4 1
Input
5 3 5 16 4
5 5 5
Output
-1

题目链接: http://codeforces.com/contest/540/problem/B
代码清单:
#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 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 ther-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 andm (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 ofm characters "." (that is, intact ice) and "X" (cracked ice).

The next line contains two integers, r1 andc1 (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 andc2 (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 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
题意:地图由两种冰块组成,'X'代表的是一踩就碎的冰块,'.'代表的是踩两下才会碎的冰块。现在给你起点和终点,问能否走到终点并且走到终点时冰块会碎掉。
分析:dfs or bfs。
题目链接: http://codeforces.com/contest/540/problem/C
代码清单:
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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值