CodeForces 540
2 seconds
256 megabytes
standard input
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?
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.
Print a single integer — the minimum number of moves Scrooge McDuck needs to open the lock.
5
82195
64723
13
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);
}
}
2 seconds
256 megabytes
standard input
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.
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.
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.
5 3 5 18 4
3 5 4
4 1
5 3 5 16 4
5 5 5
-1
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的更多相关文章
- Codeforces 540 D Bad Luck Island
Discription The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors andp pap ...
- codeforces 540 B School Marks【贪心】
题意:一共n个数,给出其中k个数,要求这n个数的中位数为y,这n个数的和不超过x,补全剩下的n-k个数 先统计给出的k个数里面比中位数小的数, 如果cnt<=n/2,说明中位数还没有出现,把这n ...
- codeforces 540 C Ice Cave【BFS】
题意:给出一个n*m的矩阵,“.”代表完整的冰,“X”代表破碎的冰,现在为了前进,需要掉下去一层,唯一的方法就是从破碎的冰上面掉下去 然后给出起点还有终点,问能否可达 即为到达终点的时候,终点必须是破 ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- 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> ...
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- 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 ...
- Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)
https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...
- Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)
https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...
随机推荐
- 出现could not find developer disk image解决办法和不受信任的开发者
真机测试问题 最近一直遇到这样的问题,很是让人心烦,但是还是要自己解决的,我也是从网上查了很多这样的解决办法,都没有成功,所以今天我要把自己的总结的方法和大家分享一下. iOS测试当中的问题 iOS ...
- WinForm------GridControl合并单元格
1.修改GridView的属性 2.点击Run Design修改需要合并的列的属性 3.给GridView添加事件(以上两步不行的情况下再使用此方法) private void gridView1_C ...
- C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用
1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...
- fedora 24 使用扇贝网页版没有声音
(扇贝的官方答疑:https://www.shanbay.com/help/faq/no_voice/) 第4步下载MP3测试文件没办法使用: 似乎因为MP3格式的文件是的版权问题. 打算安装能处理M ...
- kindeditor在光标处插入编辑器外的数据
页面 <div class="form-group clearfix"> <label class="control-label col-sm-3 co ...
- ASP.NET AJAX调用 WebService
同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...
- Navigation Bar options for Android (based on photosomething project)
1, Tab控件即标签页,可以在一页中切换显示n页内容,要使用此效果,需要用到TabHost和Tabwidget类.(过时了?) Tab控件具有两种实现过程,一是在同一个Activity中切换显示不同 ...
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...
- 练习题(登陆-进度条-微信接口判断qq-微信接口判断列车时刻表-)
1.写一个用户的登陆注册的界面,用户的密码用hashlib加密存在文件中,登陆时候,用户的密码要和文件中的密码一致才行 def sha(password): #加密函数 passwd = hashli ...
- stack.sh failing giving error "g-api did not start"
same issue i faced , tried with ./unstack.sh and ./clean.sh also but couldn't fix the issue.Followin ...