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:

题目链接:http://codeforces.com/contest/540/problem/A

题意:给你两串n个长度的密码,要你求出将第一串密码变为第二串密码所需的最小操作数!
分析:模拟一下就好了!
下面给出AC代码:
 #include <bits/stdc++.h>
using namespace std;
char a[],b[];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
cin>>a;
cin>>b;
int sum=;
for(int i=;i<n;i++)
{
if(a[i]>=b[i])
sum+=min(a[i]-b[i],b[i]+-a[i]);
else
sum+=min(b[i]-a[i],a[i]+-b[i]);
}
printf("%d\n",sum);
}
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".

题目链接:http://codeforces.com/contest/540/problem/B

题意:有一个n个元素的序列(n为奇数),现在给出其中的k个元素,让你构造其它的n-k个元素使得该序列的中位数>=y、序列总和<=x、最大元素<=p。输出任意一种可能即可。

分析:由于中位数的区间很小,我们可以去枚举。对于当前的中位数i,我们求出小于i的元素个数l,大于i的元素个数r,等于i的元素个数cnt。除去中位数自身,对于cnt-1(前提cnt!=0),我们贪心的处理,尽可能的放在i的右边,下面就是填数使左右区间元素个数相等。显然左边填1,右边填i就可以保证序列和尽可能小。

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1ll<<60;
int main()
{
int n,k,p,x,y;
cin>>n>>k>>p>>x>>y;
int sum=,a;
int t1=;//统计小于y的个数
int t2=;//统计大于等于y的个数
int m=n-k;//剩余的标记数
for(int i=;i<=k;i++)
{
cin>>a;
sum+=a;
if(a<y)
t1++;
else t2++;
}
x-=sum;//剩余的总分
if(x<||t1>n/||m>x)
{
cout<<-<<endl;
return ;
}
int cnt=(n+)/-t2;//判断大于y的个数是否大于总数的一半,(n+1)/2表示向上取整
int s=x-(n/-t1);//最终剩余分数
if(cnt>&&s/cnt<y)
{
cout<<-<<endl;
return ;
}
if(cnt<)
t1=n-k;
else t1=(n/)-t1;
for(int i=;i<=t1;i++)
cout<<<<" ";
t2=(n+)/-t2;
for(int i=;i<=t2;i++)
cout<<y<<" ";
return ;
}

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.

题目链接:http://codeforces.com/contest/540/problem/C

题意:

n*m的地图,'X'表示有裂痕的冰块,'.'表示完整的冰块,有裂痕的冰块再被踩一次就会碎掉,完整的冰块被踩一次会变成有裂痕的冰块,

现在告诉起点和终点,问从起点能否走到终点并且使终点的冰块碎掉。不能原地跳。起点和终点可能会在同一个位置。

分析:

在只走‘.’的情况下把终点的冰踩碎,输入n*m的矩阵以及走的开始和终点位置,在开始点,上下左右找‘.’,有就走,并把改点设置为‘X’,走到终点时候,若终点是‘X’则成功。其他情况都失败,DFS即可。

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1ll<<60;
const int N=;
char dp[N][N];
int a,b,x,y,n,m;
int flag=;
void DFS(int i,int j)
{
if(dp[i][j]=='X')
{
if(i==x&&j==y)
{
cout<<"YES"<<endl;
exit();// exit是在调用处强行退出程序,运行一次程序就结束
}
}
else if(i>=&&i<n&&j>=&&j<m)
{
dp[i][j]='X';
DFS(i+,j);
DFS(i-,j);
DFS(i,j+);
DFS(i,j-);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
scanf("%s",dp[i]);
scanf("%d%d%d%d",&a,&b,&x,&y);
--a;
--b;
--x;
--y;
dp[a][b]='.';
DFS(a,b);
if(flag==)
printf("NO\n");
}
return ;
}

Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)的更多相关文章

  1. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

  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 #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  6. Codeforces Round #301 (Div. 2)A B C D 水 模拟 bfs 概率dp

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

  7. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces Round #366 (Div. 2) C 模拟queue

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. 题解——Codeforces Round #508 (Div. 2) T1 (模拟)

    依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...

随机推荐

  1. 深度优先搜索(DFS)——部分和问题

    对于深度优先搜索,这里有篇写的不错的博客:DFS算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...

  2. n年前,我没钱但年轻,我怕n年后我老时,还是一无所成——2017我的收获和反思

    记得当年我刚从学校里出来时,应该和现在的95后差不多,当时还是很惶恐的,怕找不到工作,怕无法挣到足够的钱买房子支撑家庭,(当然还有其它的担心点),却唯独没意识到自己拥有着最宝贵的财富:年轻. 年轻意味 ...

  3. Java 银行家算法

    实验存档,代码特别烂.. 测试.java package operating.test; import operating.entity.bank.Bank; import operating.ent ...

  4. ArcGIS API for JavaScript 4.2学习笔记[8] 2D与3D视图同步

    同一份数据不同视图查看可能用的比较少,因为3D视图放大很多后就和2D地图差不多了,畸变很小,用于超大范围的地图显示时有用,很多时候都是在平面地图上进行分析.查询.操作.教学需要可能会对这个有要求? 本 ...

  5. MySQL数据库入门(建库和建表)--陈远波

    建库.建表 1.建库 (1)SQL语句命令建库: Create database数据库名称  (该方法创建的数据库没有设置编码乱码) 1 2 3 4 5 -- 创建数据库时,设置数据库的编码方式 -- ...

  6. Gulp-静态网页模块化

    前言: 在做纯静态页面开发的过程中,难免会遇到一些的尴尬问题.比如:整套代码有50个页面,其中有40个页面顶部和底部模块相同.那么同样的两段代码我们复制了40遍(最难受的方法).然后,这个问题就这样解 ...

  7. php一篇入门

    <?php header("Content-type: text/html; charset=utf-8");//设置编码也可以通过html中的 head中的 <met ...

  8. Head First设计模式之桥接模式

    一.定义 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. 主要解决:在多维可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活. 何时使 ...

  9. MySQL优化一 简绍

    优化方面: 存储层:数据表”存储引擎”选取.字段类型选取.逆范式(3范式) 设计层:索引.分区/分表 架构层:分布式部署(主从模式/共享) sql语句层:结果一样的情况下,要选择效率高.速度快.节省资 ...

  10. [js高手之路]从零开始打造一个javascript开源框架gdom与插件开发免费视频教程连载中

    百度网盘下载地址:https://pan.baidu.com/s/1kULNXOF 优酷土豆观看地址:http://v.youku.com/v_show/id_XMzAwNTY2MTE0MA==.ht ...