CodeForces 540

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);
    }
}
View Code
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/2){
        l=min(n/2-ans,n-k);
        r=n-l-k;    
        sum+=l+r*y;

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

 

 

posted @ 2015-05-27 22:00  unicoe  阅读(232)  评论(0编辑  收藏  举报