Codeforces Round #626 (Div. 2)
A. Even Subset Sum Problem
You are given an array consisting of positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by ) or determine that there is no such subset.
Both the given array and required subset may contain equal values.
Input
The first line contains a single integer , number of test cases to solve. Descriptions of t test cases follow.
A description of each test case consists of two lines. The first line contains a single integer , length of array .
The second line contains integers , elements of . The given array a can contain equal values (duplicates).
Output
For each test case output if there is no such subset of elements. Otherwise output positive integer , number of elements in the required subset. Then output distinct integers , indexes of the chosen elements. If there are multiple solutions output any of them.
Example
input
3
3
1 4 3
1
15
2
3 5
output
1
2
-1
2
1 2
Note
There are three test cases in the example.
In the first test case, you can choose the subset consisting of only the second element. Its sum is and it is even.
In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.
In the third test case, the subset consisting of all array’s elements has even sum.
Translation
给一个数集,要求找到任意一个非空子集,使得子集内的所有元素和为偶数。
Idea
如果给定数集中有一个偶数,可以把这个偶数单独拿出来作为子集;如果集合中没有偶数,那就拿两个奇数出来,如果连两个奇数都没有,那就不存在这样的子集。
Code
1 |
|
B. Count Subrectangles
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n×m formed by following rule: (i.e. multiplied by ). It’s easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) consisting only of ones are there in ?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers a subrectangle is an intersection of the rows and the columns .
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers and , length of array , length of array and required size of subrectangles.
The second line contains n integers , elements of .
The third line contains m integers , elements of .
Output
Output single integer — the number of subrectangles of with size (area) consisting only of ones.
Examples
input
3 3 2
1 0 1
1 1 1
output
4
input
3 5 4
1 1 1
1 1 1 1 1
output
14
Note
In first example matrix is:
There are subrectangles of size consisting of only ones in it:
In second example matrix is:
Translation
给一个长度为的数组,一个长度为的数组,连个数组都只有或者。数组是二维数组,中连续的一片组成一块面积,问面积为k的矩形有几个。
Idea
中一段连续的长度为的,和中一段连续的长度为的,得到中一块的面积。
特殊的,在中找一段连续的长度为的,然后在中一段连续的长度为的,能得到一块的面积。
只要找出的所有因子,对于每个因子,统计在中长度为个的子列个数,然后在中统计长度为个的子列个数。
Code
1 |
|
C. Unusual Competitions
A bracketed sequence is called correct (regular) if by inserting “+” and “1” you can get a well-formed mathematical expression from it. For example, sequences “(())()”, “()” and “(()(()))” are correct, while “)(”, “(()” and “(()))(” are not.
The teacher gave Dmitry’s class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima’s turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.
Dima suspects now that he simply missed the word “correct” in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.
The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes l nanoseconds, where l is the length of the subsegment being reordered. It’s easy to see that reorder operation doesn’t change the number of opening and closing brackets. For example for “))((” he can choose the substring “)(” and do reorder “)()(” (this operation will take 2 nanoseconds).
Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it’s impossible.
Input
The first line contains a single integer — the length of Dima’s sequence.
The second line contains string of length , consisting of characters “(” and “)” only.
Output
Print a single integer — the minimum number of nanoseconds to make the sequence correct or “-1” if it is impossible to do so.
Examples
input
8
))((())(
output
6
input
3
(()
output
-1
Note
In the first example we can firstly reorder the segment from first to the fourth character, replacing it with “()()”, the whole sequence will be “()()())(”. And then reorder the segment from the seventh to eighth character, replacing it with “()”. In the end the sequence will be “()()()()”, while the total time spent is nanoseconds.
Translation
给一个括号序列,每次可以选择一个长度为的子列,任意调整其中括号的位置,花费的时间是,问至少需要花费多少时间,得到一个合法序列。
Idea
如果整个括号序列’(‘和’)‘的个数不相同,那么肯定不能得到合法的括号序列,否则一定能(比如直接调整整个序列)。
贪心得选择一段’(‘和’)‘个数相同的不合法子列(以’(‘结尾,’(‘和’)'个数相同),将其调整,然后寻找下一段没有调整的子列。此处运用单指针对位置进行迭代。
Code
1 |
|