[Codeforces 1381C] Mastermind
by zzz
Problem Description
Given two arrays of length n, where the value belongs to [1, n + 1]
, there will be x
exact matching (value and index), and y
matching values.
The problem is given one of the two arrays, x
and y
, try to figure out the other array.
Sample Input
7
5 2 4
3 1 1 2 5
5 3 4
1 1 2 1 2
4 0 4
5 5 3 3
4 1 4
2 3 2 3
6 1 2
3 2 1 1 1 1
6 2 4
3 3 2 1 1 1
6 2 6
1 1 3 2 1 1
Sample Output
YES
3 1 6 1 2
YES
3 1 1 1 2
YES
3 3 5 5
NO
YES
4 4 4 4 3 1
YES
3 1 3 1 7 7
YES
2 3 1 1 1 1
Solution
Outline
- Find
x
exact match. - Find
y-x
value but not index match. - Fill other positions with the missing elements.
Step 1
It’s easy to figure out we need to use the most frequent element as much as possible.
Step 2
There are several good and bad ideas.
- Using the most frequent elements. Pop out two most frequent elements and assign to each other. This is incorrect! One counterexample is
1 2 3
. - Sort the elements and match with the reverse of the sorted array. This is incorrect! One counter example is
1 1 2 2 2 3 3
. - Using the most frequent elements, and for >2 number of same top frequenet of elements, we need to use them at the same time, by shifting them by 1.
- Sort the elements and shift the elements by the max number of occurance.
- Sort the elements and shift the elements by the half of the array length.
Looking back
The victory of acceptance makes the hard work to solve the problem totally worth it!
tags: Codeforces - Constructive - Matching - ShiftnigPosts
[Codeforces 1391E] Pairs of Pairs
[Codeforces 1393D] Rarity and New Dress
Convex Hull Trick
[Codeforces 1389G] Directing Edges
[Codeforces 1389F] Bicolored Segments
[Codeforces 1381C] Mastermind
[Codeforces 1375G] Tree Modification
[Codeforces 1375F] Integer Game
[Codeforces 1375E] Inversion SwapSort
subscribe via RSS