You are on page 1of 10

Problem Solving Set VI

1. Version Control System


A version control system(VCS) is a repository of files, often the files for the source code of computer
programs, with monitored access. Every change made to the source is tracked, along with who made the
change, why they made it, and references to problems fixed, or enhancements introduced, by the change.
Version control systems are essential for any form of distributed, collaborative development. Whether it is
the history of a wiki page or large software development project, the ability to track each change as it was
made, and to reverse changes when necessary can make all the difference between a well managed and
controlled process and an uncontrolled ‘first come, first served’ system. It can also serve as a mechanism
for due diligence for software projects.
In this problem we'll consider a simplified model of a development project. Let's suppose, that there
are N source files in the project. All the source files are distinct and numbered from 1 to N.
A VCS, that is used for maintaining the project, contains two sequences of source files. The first sequence
contains the source files, that are ignored by the VCS. If a source file is not in the first sequence, then it's
considered to be unignored. The second sequence contains the source files, that are tracked by the VCS. If a
source file is not in the second sequence, then it's considered to be untracked. A source file can either be or
not be in any of these two sequences.
Your task is to calculate two values: the number of source files of the project, that are both tracked and
ignored, and the number of source files of the project, that are both untracked and unignored.

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of the test case description contains three integers N, M and K denoting the number of source
files in the project, the number of ignored source files and the number of tracked source files.
The second line contains M distinct integers denoting the sequence A of ignored source files. The sequence
is strictly increasing.
The third line contains K distinct integers denoting the sequence B of tracked source files. The sequence is
strictly increasing.

Output
For each test case, output a single line containing two integers: the number of the source files, that are both tracked
and ignored, and the number of the source files, that are both untracked and unignored.

Constraints
 1 ≤ T ≤ 100
 1 ≤ M, K ≤ N ≤ 100
 1 ≤ A1 < A2 < ... < AM ≤ N
 1 ≤ B1 < B2 < ... < BK ≤ N

Example
Input:
2
746
1467
123467
422
14
34

Output:
41
11
Explanation
In the first test case, the source files {1, 4, 6, 7} are both tracked and ignored, the source file {5} is both
untracked and unignored.
In the second test case, the source file {4} is both tracked and ignored, the source file {2} is both untracked
and unignored.

2. Good Joke!
Vadim and Roman like discussing challenging problems with each other. One day Vadim told his friend
following problem:
Given N points on a plane. Each point p is defined by it's two integer coordinates — px and py. The
distance between points a and b is min(|ax - bx|, |ay - by|). You should choose a starting point and make a
route visiting every point exactly once, i.e. if we write down numbers of points in order you visit them we
should obtain a permutation. Of course, overall distance walked should be as small as possible. The number
of points may be up to 40.
"40? Maybe 20? Are you kidding?" – asked Roman. "No, it's not a joke" – replied Vadim. So Roman had nothing to
do, but try to solve this problem. Since Roman is really weak in problem solving and you are the only friend, except
Vadim, with whom Roman can discuss challenging tasks, he has nobody else to ask for help, but you!

Input
Input description.
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases
follows.The first line of each test case contains a single integer N denoting the number of points on a plane. The
following N lines contain two space-separated integers each — coordinates of points.

Output
Output description.
Output the answer for every test case in a separate line. The answer for every test case is a permutation of length N. In
case there are several solutions that lead to minimal distance walked, you should choose the lexicographically
smallest one. Let P denote such permutation. To make output smaller, you should output H(P). H(P) = P1 xor P2 xor
... xor PN. Have a look at the example and it's explanation for better understanding.

Constraints
 1 ≤ T ≤ 10
 1 ≤ N ≤ 40
 0 ≤ absolute value of each coordinate ≤ 1000
 1 ≤ sum over all N in a single test file ≤ 120

Example
Input:
2
2
12
00
3
33
00
03
Output:
3
0

Explanation
For the first test case permutation [1, 2] is optimal. 1 xor 2 = 3.
For the second one both [2, 3, 1] and [1, 3, 2] lead us to the shortest walk, but the second one is
lexicographically smaller. So the answer is H([1, 3, 2]) = 1 xor 3 xor 2 = 0 .
3. Chef and Subarrays
Chef likes problems involving arrays. Unfortunately, the last one he tried to solve didn't quite get solved.
Chef has an array A of N positive numbers. He wants to find the number of subarrays for which the sum
and product of elements are equal.
Please help Chef find this number.

Input
The first line of input contains an integer T denoting the number of test cases. T test cases follow. The first line of
each test contains the integer N. The next line contains N integers — A1, A2, ..., AN — denoting the array.

Output
For each test case, output a single line with the answer for the instance.

Constraints
 1 ≤ T ≤ 50
 1 ≤ n ≤ 50
 1 ≤ Ai ≤ 109
 A1 * A2 * ... * An ≤ 109

Example
Input:
3
3
132
4
4121
6
122221

Output:
4
5
9

Explanation:
Example case 1. There are 4 such subarrays: A[1..1], A[2..2], A[3..3], A[1..3]. Consider A[1..3], sum = 1 +
3 + 2 = 6, product = 1 * 3 * 2 = 6.

4. Rectangle
You are given four integers a, b, c and d. Determine if there's a rectangle such that the lengths of its sides
are a, b, c and d (in any order).

Input
 The first line of the input contains a single integer T denoting the number of test cases. The description
of T test cases follows.
 The first and only line of each test case contains four space-separated integers a, b, c and d.

Output
For each test case, print a single line containing one string "YES" or "NO".

Constraints
 1 ≤ T ≤ 1,000
 1 ≤ a, b, c, d ≤ 10,000

Subtasks
Subtask #1 (100 points): original constraints
Example
Input:

3
1122
3223
1222

Output:

YES
YES
NO

5. Alternating subarray prefix


There's an array A consisting of N non-zero integers A1..N. A subarray of A is called alternating if any two
adjacent elements in it have different signs (i.e. one of them should be negative and the other should be
positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a
subarray Ax..y for the maximum possible y ≥ x. The length of such a subarray is y-x+1.

Input
 The first line of the input contains an integer T - the number of test cases.
 The first line of each test case contains N.
 The following line contains N space-separated integers A1..N.

Output
For each test case, output one line with N space-separated integers - the lengths of the longest alternating
subarray starting at x, for each x from 1 to N.

Constraints
 1 ≤ T ≤ 10
 1 ≤ N ≤ 105
 -109 ≤ Ai ≤ 109

Example
Input:
3
4
1234
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3

Output:
1111
4321
113211

Explanation
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a
single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5.

6. Two vs Ten
Chef Two and Chef Ten are playing a game with a number XX. In one turn, they can multiply XX by 22.
The goal of the game is to make XX divisible by 1010.
Help the Chefs find the smallest number of turns necessary to win the game (it may be possible to win in
zero turns) or determine that it is impossible.

Input
 The first line of the input contains a single integer TT denoting the number of test cases. The
description of TT test cases follows.
 The first and only line of each test case contains a single integer denoting the initial value of XX.

Output
For each test case, print a single line containing one integer — the minimum required number of turns
or −1−1 if there is no way to win the game.

Constraints
 1≤T≤10001≤T≤1000
 0≤X≤1090≤X≤109

Subtasks
Subtask #1 (100 points): original constraints

Example Input
3
10
25
1

Example Output
0
1
-1

7. Chef and his Sequence


Chef has a sequence of N numbers. He like a sequence better if the sequence contains his favorite sequence
as a substring.
Given the sequence and his favorite sequence(F) check whether the favorite sequence is contained in the
sequence

Input
The first line will contain the number of test cases and are followed by the cases.
Each test case consists of four lines: The length of the sequence, the sequence N,the length of F and the
sequence F

Output
Print "Yes" if the sequence contains the favourite sequence int it otherwise print "No"

Constraints
1<=T<=10
1<sizeof(n)
1<sizeof(f)
</sizeof(f)</sizeof(n)

Input:
2
6
123456
3
234
6
22 5 6 33 1 4
2
4 15

Output:
Yes
No

8. Is it a VOWEL or CONSONANT
Write a program to take a character (C)(C) as input and check whether the given character is a vowel or a
consonant.
NOTE:−NOTE:− Vowels are 'A', 'E', 'I', 'O', 'U'. Rest all alphabets are called consonants.
Input:
 First line will contain the character CC.

Output:
Print "Vowel" if the given character is a vowel, otherwise print "Consonant".

Constraints
 CC willwill bebe anan upperupper casecase EnglishEnglish alphabetalphabet

Sample Input:
Z

Sample Output:
Consonant

9. Movie Weekend
Little Egor is a huge movie fan. He likes watching different kinds of movies: from drama movies to
comedy movies, from teen movies to horror movies. He is planning to visit cinema this weekend, but he's
not sure which movie he should watch.
There are n movies to watch during this weekend. Each movie can be characterized by two
integers Li and Ri, denoting the length and the rating of the corresponding movie. Egor wants to
watch exactly one movie with the maximal value of Li × Ri. If there are several such movies, he would pick
a one with the maximal Ri among them. If there is still a tie, he would pick the one with the minimal index
among them.
Your task is to help Egor to pick a movie to watch during this weekend.

Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of the test case description contains an integer n.
The second line of the test case description contains n integers L1, L2, ...,Ln. The following line
contains n integers R1, R2, ..., Rn.

Output
For each test case, output a single integer i denoting the index of the movie that Egor should watch during
this weekend. Note that we follow 1-based indexing.

Constraints
 1≤T≤5
 1 ≤ n ≤ 100
 1 ≤ Li, Ri ≤ 100
Example
Input:
2
2
12
21
4
2141
2414

Output:
1
2

Explanation
In the first example case, both films have the same value of L × R, but the first film has a better rating.
In the second example case, the second and the fourth movies are equally good, but the second movie has a
smaller index.

10. Chef and Rainbow Array


Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves
Rainbow Arrays.
An array is Rainbow if it has the following structure:
 First a1 elements equal 1.
 Next a2 elements equal 2.
 Next a3 elements equal 3.
 Next a4 elements equal 4.
 Next a5 elements equal 5.
 Next a6 elements equal 6.
 Next a7 elements equal 7.
 Next a6 elements equal 6.
 Next a5 elements equal 5.
 Next a4 elements equal 4.
 Next a3 elements equal 3.
 Next a2 elements equal 2.
 Next a1 elements equal 1.
 ai can be any non-zero positive integer.
 There are no other elements in array.

Help Chef in finding out if the given array is a Rainbow Array or not.

Input
 The first line of the input contains an integer T denoting the number of test cases.
 The first line of each test case contains an integer N, denoting the number of elements in the given
array.
 The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.

Output
 For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if
the array is rainbow array or not.

Constraints
 1 ≤ T ≤ 100
 7 ≤ N ≤ 100
 1 ≤ Ai ≤ 10
Subtasks
 Subtask 1 (100 points) : Original constraints

Example
Input
3
19
1234456667666544321
14
12345676543211
13
1234568654321

Output
yes
no
no

Explanation
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6.

11. Devu and friendship testing


Devu has n weird friends. Its his birthday today, so they thought that this is the best occasion for testing
their friendship with him. They put up conditions before Devu that they will break the friendship unless he
gives them a grand party on their chosen day. Formally, ith friend will break his friendship if he does not
receive a grand party on dith day.
Devu despite being as rich as Gatsby, is quite frugal and can give at most one grand party daily. Also, he
wants to invite only one person in a party. So he just wonders what is the maximum number of friendships
he can save. Please help Devu in this tough task !!

Input
 The first line of the input contains an integer T denoting the number of test cases. The description
of T test cases follows.
 First line will contain a single integer denoting n.
 Second line will contain n space separated integers where ith integer corresponds to the day dith as given
in the problem.

Output
Print a single line corresponding to the answer of the problem.

Constraints
 1 ≤ T ≤ 104
 1 ≤ n ≤ 50
 1 ≤ di ≤ 100

Example
Input:
2
2
32
2
11
Output:
2
1
Explanation
Example case 1. Devu can give party to second friend on day 2 and first friend on day 3, so he can save
both his friendships.
Example case 2. Both the friends want a party on day 1, and as the Devu can not afford more than one
party a day, so he can save only one of the friendships, so answer is 1.

12. Chef And Coloring


After a long time, Chef has finally decided to renovate his house. Chef's house has N rooms in it numbered
from 1 to N. Each room is currently painted in one of the red, blue or green colors. Your are given
configuration of colors of his house by a string S consisting of N characters. In this string, color red will be
denoted by 'R', green by 'G' and blue by 'B'.
Chef does not like current painting configuration that much and would like to repaint the house such that
each room has same color.
For painting, Chef has all the 3 color paints available and mixing any 2 color paints will result into 3rd
color paint i.e
 R+B=G
 B+G=R
 G+R=B
For example, painting a room having red color before with green color paint will make the color of room
blue.
Also, Chef has many buckets of paint of each color. Simply put, you can assume that he will not run out of
paint.
Being extraordinary lazy, our little chef does not want to work much and therefore, he has asked you to find
the minimum number of rooms he has to repaint (possibly zero) in order to have all the rooms with same
color. Can you please help him?

Input
First line of input contains a single integer T denoting the number of test cases. First line of each test case
contains an integer N denoting the number of rooms in the chef's house. Next line of each test case contains
a string S denoting the current color configuration of rooms.

Output
For each test case, Print the minimum number of rooms need to be painted in order to have all the rooms
painted with same color i.e either red, blue or green.

Constraints
 1 ≤ T ≤ 10
 1 ≤ N ≤ 105
 Si = {'R','G','B'}

Scoring
 Subtask 1 (40 points) : 1 ≤ N ≤ 10
 Subtask 2 (60 points) : original constraints

Example
Input
3
3
RGR
3
RRR
3
RGB
Output
1
0
2

Explanation:
 Test 1: Chef prefers to paint room 2 with blue color such that the resulting color will be red and all the
rooms have same color i.e red.
 Test 2: Given configuration has all the rooms painted with red color and therefore, chef does not need
to do painting work at all.
 Test 3: One possible way of renovation is to paint room 1 with green color, room 2 with red color such
that all rooms have same color i.e blue.

You might also like