Challenge
Challenge
Source : codeforces.com
Author : Unknown
You are given three integers n , a , and b. Determine if there exist two permutations p and q of
length n, for which the following conditions hold:
Example :
Input Format : n , a , b
Output Format : Yes/No
Input :
1 1 1
2 1 2
3 1 1
4 1 1
Output :
Yes
No
No
Yes
Note
In the first test case, [1] and [1] form a valid pair.
In the second test case and the third case, we can show that such a pair of permutations doesn't
exist.
In the fourth test case, [1,2,3,4]and [1,3,2,4] form a valid pair.
For his birthday, Kevin received the set of pairwise distinct numbers 1,2,3,…,n as a gift.
He is going to arrange these numbers in a way such that the minimum absolute difference
between two consecutive numbers be maximum possible. More formally, if he arranges numbers
in order p1,p2,…,pn he wants to maximize the value
min|pi+1−pi| for all I from 1 to n-1
where |x| denotes the absolute value of x.
Example :
Input Format : n
Input :
2
4
3
Output :
2 4 1 3
1 2 3
Note
In the first test case the minimum absolute difference of consecutive elements equals min{|
4−2|,|1−4|,|3−1|}=min{2,3,2}=2. It's easy to prove that this answer is optimal.
In the second test case each permutation of numbers 1,2,3 is an optimal answer. The minimum
absolute difference of consecutive elements equals to 11.
However, the ring itself hates equal adjacent elements. So if two adjacent elements in the
sequence are equal at any time, one of them will be erased immediately. The sequence
doesn't contain equal adjacent elements initially.
Muxii can perform the following operation until the sequence becomes empty:
Choose an element in a and erase it.
For example, if ring is [1,2,4,2,3,2], and Muxii erases element 4, then ring would erase one of
the elements equal to 2, and the ring will become [1,2,3,2].
Input :
4
1 2 3 2
4
1 2 1 2
1
1
Output :
4
3
Note
In the first test case, you can erase the second element first, then erase the remaining elements
one by one in any order. In total, you can perform the operation 4 times. Note that if you erase
the first element first, then the sequence will be turned into [2,3,2] and then immediately
become [2,3].
In the second test case, you can erase the first element first, then the sequence becomes [2,1].
Then you can erase all remaining elements one by one in any order.
Input:
N=10 K=3
arr[] = {2, 3, 5}
Output: 2
Explanation: The leaves eaten by the first
caterpillar are (2, 4, 6, 8, 10). The leaves
eaten by the second caterpilllar are (3, 6, 9).
The leaves eaten by the third caterpilllar
are (5, 10). Ultimately, the uneaten leaves are
1, 7 and their number is 2.
Question 5 : Stripes
On an 8×8 grid, some horizontal rows have been painted red, and some vertical columns have
been painted blue, in some order. The stripes are drawn sequentially, one after the other. When
the stripe is drawn, it repaints all the cells through which it passes.
Input :
4
....B...
....B...
....B...
RRRRRRRR
....B...
....B...
....B...
....B...
RRRRRRRB
B......B
B......B
B......B
B......B
B......B
B......B
RRRRRRRB
RRRRRRBB
.B.B..BB
RRRRRRBB
.B.B..BB
.B.B..BB
RRRRRRBB
.B.B..BB
.B.B..BB
........
........
........
RRRRRRRR
........
........
........
........
Output :
R
B
B
R
Note
The first test case is pictured in the statement.
In the second test case, the first blue column is painted first, then the first and last red rows, and
finally the last blue column. Since a blue stripe is painted last, the answer is B.