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
Note

In the sample he needs 13 moves:

  • 1 disk:
  • 2 disk:
  • 3 disk:
  • 4 disk:
  • 5 disk:

思路:题目很简单,就是找这个数到目标数的最短距离,其实简单比较下大小就行。我直接打了张表,然后做的。

public class Yaya {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int length = sc.nextInt();
String s = sc.next();
String t = sc.next();
int[][] path = new int[10][10];
for(int i = 0; i != 10; ++i)
path[i][i] = 0;
for(int i = 0; i != 10; ++i) {
for(int j = i + 1; j != 10; ++j) {
if(j - i <= 5) {
path[i][j] = path[j][i] = j - i;
} else {
path[i][j] = path[j][i] = 10 - j + i;
}
}
}
int res = 0;
for(int i = 0; i != length; ++i) {
res += path[s.charAt(i) - '0'][t.charAt(i) - '0'];
}
System.out.println(res);
}
}
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.

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

思路:这道题我做的时候一上手就想错了,贪心的方法就不对。然后学习了题解。统计比中位数小的数的个数。

如果比中位数小的数的数目大于一半,就不可能。否则,用“1” 和 “4” 去构造答案。如果剩余的科目用“1” 和 “4”构造出来都比最大分数的,也不可能,否则输出答案。

if(ans<=n/){
l=min(n/-ans,n-k);
r=n-l-k;
sum+=l+r*y; if(sum>x) printf("-1\n");
else{
for(int i=;i<=l;i++) printf("1 ");
for(int i=;i<=r;i++) printf("%d ",y);
printf("\n");
}
} else
printf("-1\n");

CodeForces 540的更多相关文章

  1. Codeforces 540 D Bad Luck Island

    Discription The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors andp pap ...

  2. codeforces 540 B School Marks【贪心】

    题意:一共n个数,给出其中k个数,要求这n个数的中位数为y,这n个数的和不超过x,补全剩下的n-k个数 先统计给出的k个数里面比中位数小的数, 如果cnt<=n/2,说明中位数还没有出现,把这n ...

  3. codeforces 540 C Ice Cave【BFS】

    题意:给出一个n*m的矩阵,“.”代表完整的冰,“X”代表破碎的冰,现在为了前进,需要掉下去一层,唯一的方法就是从破碎的冰上面掉下去 然后给出起点还有终点,问能否可达 即为到达终点的时候,终点必须是破 ...

  4. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  5. Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1

    A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...

  6. Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix

    https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...

  7. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  8. Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)

    https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...

  9. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

随机推荐

  1. 10月30日下午 PHP精确查询(模糊查询、模糊+关键字共同查询)

    1.一个条件的模糊查询 <body> <br /> <form action="main.php" method="post"&g ...

  2. Top 5 iPad Pro Apps for Your Apple Pencil

    1. Procreate - 5 to 10 dollars 2. Adobe Sketch - Free 3. Paper - Free 4. Pixelmator 5. Notes

  3. 添加删除表格append或 createElement

    方法一: js代码:增加一行五列的表格 function AddList(){ $len= document.getElementsByName('goods_name[]').length; obj ...

  4. 8-9 MyBatis基础课

    慕课网,'通过自动回复小机器人学习Mybatis',看了一半,没网了... Jsp+servlet+jdbc

  5. 都别说工资低了,我们来一起写简单的dom选择器吧!

    前言 我师父(http://www.cnblogs.com/aaronjs/)说应当阅读框架(jquery),所以老夫就准备开始看了 然后公司的师兄原来写了个dom选择器,感觉不错啊!!!原来自己从来 ...

  6. unix编程书中的 ourhdr.h代码

    真心不知到里面写的具体什么意思,先记下吧. /*Our own header, to be included after all standard system headers*/ #ifndef _ ...

  7. ASP.NET Core--基于授权的资源

    翻译如下: 通常授权取决于正在访问的资源. 例如,文档可以具有作者属性. 将只允许文档作者对其进行更新,因此必须在进行授权评估之前从文档存储库加载资源. 这不能使用Authorize属性来完成,因为属 ...

  8. JDK source 之 LinkedHashMap原理浅谈

    注:本文参考JDK1.7.0_45源码. LinkedHashMap是基于HashMap实现的数据结构,与HashMap主要的不同为每个Entry是使用双向链表实现的,并且提供了根据访问顺序进行排序的 ...

  9. js导出表格数据

    考虑到浏览器兼容性问题,采用原生js和后台交互下载网页数据 js: var table = $('.table-panel table'); // Header var tdData ="& ...

  10. 用Javascript(js)进行HTML转义工具(处理特殊字符显示)

    转自:http://blog.csdn.net/hj7jay/article/details/51280405  众所周知页面上的字符内容通常都需要进行HTML转义才能正确显示,尤其对于Input,T ...