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.

Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ

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
Note

In the sample he needs 13 moves:

  • 1 disk: Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ
  • 2 disk: Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ
  • 3 disk: Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ
  • 4 disk: Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ
  • 5 disk: Codeforces Round 301 (Div. 2) - 风未定 - NGUNAUJ
题目大意找出旋转的最小次数。 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
string s,t;
int ans;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n;
while(cin>>n){
ans=0;
cin>>s>>t;
for(int i=0;i<n;i++){
int x=abs((s[i]-'0')-(t[i]-'0'));
int y=10-x;
ans+=min(x,y);
}
cout<<ans<<endl;
}
return 0;
}


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 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  再给出他已经做完的题目数量k  要求做完剩下的n-k道题目,这n-k个题目的分数由你来定(最大分数为p),使得总体的分数不超过x 且中位数为y  。
思路:贪心,尽量把剩下的n-k个题目的分数置为y。最后判断一下>=y的题目数 是否>=n/2+1即可;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
int n,k,p,x,y;
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>k>>p>>x>>y){
for(int i=1;i<=k;i++){
cin>>a[i];
x-=a[i];
}
for(int i=k+1;i<=n;i++){//赋值为1
a[i]=1;
x-=1;
}
if(x<0){
cout<<-1<<endl;
return 0;
}
for(int i=k+1;i<=n;i++){
int d=min(x,y-1);
x-=d;
a[i]+=d;
}
int num=0;
for(int i=1;i<=n;i++){
if(a[i]>=y)num++;
}
if(num<n/2+1){
cout<<-1<<endl;
return 0;
}
for(int i=k+1;i<=n;i++)
cout<<a[i]<<" ";
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 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 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.

题目大意 给定起点 终点,问能否从起点走到终点,“X”不能走。注意:经过“.”之后“.”就变成“X”了,起点是“X”,终点要变成“X”之后才能走上去;

思路:BFS

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;
struct node{
int x,y;
};
bool cmp(int a,int b)
{
return a>b;
}
int n,m;
char mp[510][510];
int dir[4][2]={1,0,0,1,0,-1,-1,0};
int sx,sy,ex,ey;
void bfs(){
queue<node>q;
node u;
u.x=sx;
u.y=sy;
q.push(u);
while(!q.empty()){
node u=q.front();q.pop();
for(int i=0;i<4;i++){
node v;
v.x=u.x+dir[i][0];
v.y=u.y+dir[i][1];
if(v.x<1||v.x>n||v.y>m||v.y<1)continue;
if(v.x==ex&&v.y==ey&&mp[v.x][v.y]=='X') {cout<<"YES"<<endl;return;}
if(mp[v.x][v.y]=='X')continue;
q.push(v);
mp[v.x][v.y]='X';
}
}
cout<<"NO"<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>mp[i][j];
cin>>sx>>sy>>ex>>ey;
//cout<<sx<<sy<<ex<<ey<<endl;
bfs();
}
return 0;
}


D题是概率DP。。弱还在练习DP..

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值