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位锁 给你初始状态与密码 问最少需要旋转多少步

题解:水

 #include<bits/stdc++.h>
#define ll __int64
using namespace std;
int n;
char a[];
char b[];
int main()
{
scanf("%d",&n);
scanf("%s",a);
scanf("%s",b);
int ans=;
for(int i=;i<n;i++)
{
int aa=a[i]-'';
int bb=b[i]-'';
if(aa<bb)
swap(aa,bb);
ans=ans+min(aa-bb,bb+(-aa));
}
printf("%d\n",ans);
return ;
}
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

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

 #include<bits/stdc++.h>
#define ll __int64
using namespace std;
int n,k,p,x,y;
int a[];
int main()
{
scanf("%d %d %d %d %d",&n,&k,&p,&x,&y);
int sum=;
for(int i=; i<=k; i++){
scanf("%d",&a[i]);
sum+=a[i];
}
int gg=k;
if(x-sum<(n-k)){
printf("-1\n");
return ;
}
else{
int j;
if(k>){
sort(a+,a++k);
for(j=; j<=k; j++){
if(a[j]>=y)
break;
}
if(j==k+){
if(j>n){
printf("-1\n");
return ;
}
else{
a[j]=y;
k++;
}
}
}
else{
a[]=y;
k++;
j=;
sum+=y;
if(y>x){
printf("-1\n");
return ;
}
}
int exm=n/+;
if(k-j+>=exm){
for(int i=k+; i<=n; i++)
a[i]=;
}
else{
int jishu=k+;
int all=;
all=(exm-(k-j+))*y+(n-k-(exm-(k-j+)));
if(((exm-(k-j+))+k)>n||all+sum>x){
printf("-1\n");
return ;
}
else{
for(int i=; i<=exm-(k-j+); i++)
a[jishu++]=y;
for(int j=jishu; j<=n; j++)
a[j]=;
}
}
}
for(int i=gg+; i<=n; i++)
printf("%d ",a[i]);
printf("\n");
return ;
}
/*
5 3 5 18 4
3 5 4 5 3 5 1 4
3 5 4 5 4 5 10 1
1 2 2 2 1 0 3 2 3 5 3 5 17 4
5 5 5
*/
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

 #include<bits/stdc++.h>
#define ll __int64
using namespace std;
int n,m;
char mp[][];
int visit[][];
int x1,y1,x2,y2;
struct node
{
int x,y;
}now,exm;
queue<node> q;
int dis[][]={{,},{-,},{,},{,-}};
bool bfs()
{
while(!q.empty())
q.pop();
now.x=x1;
now.y=y1;
visit[x1][y1]=;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(visit[x2][y2]>=){
return true;
}
for(int i=;i<;i++)
{
int aa=now.x+dis[i][];
int bb=now.y+dis[i][];
if((aa>=&&aa<=n&&bb>=&&bb<=m)&&(mp[aa][bb]=='.'&&visit[aa][bb]==)||(aa==x2&&bb==y2))
{
exm.x=aa;
exm.y=bb;
q.push(exm);
visit[aa][bb]++;
}
}
}
return false;
}
int main()
{
memset(visit,,sizeof(visit));
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",mp[i]+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(mp[i][j]=='X')
visit[i][j]++;
}
}
scanf("%d %d",&x1,&y1);
scanf("%d %d",&x2,&y2);
if(bfs())
printf("YES\n");
else
printf("NO\n");
return ;
}
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
 #include<bits/stdc++.h>
#define ll __int64
using namespace std;
double dp[][][];
double a,b,c;
int main()
{
int r,s,p;
scanf("%d %d %d",&r,&s,&p);
dp[r][s][p]=;
for(int i=r;i>=;i--)
{
for(int j=s;j>=;j--)
{
for(int k=p;k>=;k--)
{
double all=i*j*1.0+j*k*1.0+i*k*1.0;
if(all==) continue;
if(k>=) dp[i][j][k-]+=dp[i][j][k]*(double)(j*1.0*k)/all;
if(j>=) dp[i][j-][k]+=dp[i][j][k]*(double)(j*1.0*i)/all;
if(i>=) dp[i-][j][k]+=dp[i][j][k]*(double)(i*1.0*k)/all;
}
}
}
for(int i=;i<=r;i++) a+=dp[i][][];
for(int i=;i<=s;i++) b+=dp[][i][];
for(int i=;i<=p;i++) c+=dp[][][i];
printf("%.9f %.9f %.9f\n",a,b,c);
return ;
}

Codeforces Round #301 (Div. 2)A B C D 水 模拟 bfs 概率dp的更多相关文章

  1. Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  2. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  3. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  4. 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

    题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...

  5. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP

    D. Bad Luck Island Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...

  7. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  8. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  9. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

随机推荐

  1. Docker运行简单的Demo

    打开cmd.exe 输入docker run hello-world,本机没有这个images实例,将会从官方下载下载 运行一个简单的web实例,例如输入: docker run --name asp ...

  2. Windows操作系统C盘占用空间过多

    Windows操作系统C盘占用空间过多 大部分的windows电脑用户在长时间使用PC时都会遇到一个问题,就是C盘占用的空间会越来越多,乃至占满整个C盘. 后来在百度了一波,发现各种方法都试过了,也不 ...

  3. 最大公共子串:DP

    标题:最大公共子串 最大公共子串长度问题就是:求两个串的所有子串中能够匹配上的最大长度是多少. 比如:"abcdkkk" 和 "baabcdadabc",可以找 ...

  4. NO.1:自学tensorflow之路------神经网络背景知识

    引言 从本周,我将开始tensorflow的学习.手头只有一本<tensorflow:实战Google深度学习框架>,这本书对于tensorflow的入门有一定帮助.tensorflow中 ...

  5. Machine Learning方法总结

    Kmeans——不断松弛(?我的理解)模拟,将点集分成几堆的算法(堆数需要自己定). 局部加权回归(LWR)——非参数学习算法,不用担心自变量幂次选择.(因此当二次欠拟合, 三次过拟合的时候不妨尝试这 ...

  6. PayPal接洽苹果 欲承接手机支付外包

    不久前,<华尔街日报>等媒体报道,苹果正计划利用iTunes内部支付功能,推出第三方手机支付服务.美国科技 新闻网站Recode1月30日引述消息人士称,移动支付领军厂商PayPal,目前 ...

  7. Spark入门(Python)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  8. jdbc 3.0

    1.将Blob.Clob类型数据保存到数据库 import java.io.File; import java.io.FileInputStream; import java.io.FileReade ...

  9. div跟随鼠标移动

    1.目标是实现div跟随鼠标而移动,分三种情况进行实现 a)首先获取div,进行绑定鼠标移动事件,给div开启定位功能 第一种实现方式,假如body的大小跟页面大小一样,则可以用这个方法. 1)获取鼠 ...

  10. 【final】Scrum站立会议第2次....11.20

    小组名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 时间:2016.11.9    12:00——12:30 地点:传媒西楼220室 本次对fnal阶段 ...