Ozon Tech Challenge 2020 (Div.1 + Div.2)
A. Kuroni and the Gifts
Kuroni has n daughters. As gifts for them, he bought necklaces and bracelets:
- the -th necklace has a brightness , where all the are pairwise distinct (i.e. all are different),
- the -th bracelet has a brightness , where all the bi are pairwise distinct (i.e. all are different).
Kuroni wants to give exactly one necklace and exactly one bracelet to each of his daughters. To make sure that all of them look unique, the total brightnesses of the gifts given to each daughter should be pairwise distinct. Formally, if the -th daughter receives a necklace with brightness and a bracelet with brightness , then the sums should be pairwise distinct. Help Kuroni to distribute the gifts.
For example, if the brightnesses are and , then we may distribute the gifts as follows:
Give the third necklace and the first bracelet to the first daughter, for a total brightness of .
Give the first necklace and the third bracelet to the second daughter, for a total brightness of .
Give the second necklace and the second bracelet to the third daughter, for a total brightness of .
Here is an example of an invalid distribution:
Give the first necklace and the first bracelet to the first daughter, for a total brightness of .
Give the second necklace and the second bracelet to the second daughter, for a total brightness of .
Give the third necklace and the third bracelet to the third daughter, for a total brightness of .
This distribution is invalid, as the total brightnesses of the gifts received by the first and the third daughter are the same. Don’t make them this upset!
Input
The input consists of multiple test cases. The first line contains an integer — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer — the number of daughters, necklaces and bracelets.
The second line of each test case contains distinct integers — the brightnesses of the necklaces.
The third line of each test case contains n distinct integers — the brightnesses of the bracelets.
Output
For each test case, print a line containing integers , representing that the -th daughter receives a necklace with brightness . In the next line print integers , representing that the -th daughter receives a bracelet with brightness .
The sums should all be distinct. The numbers should be equal to the numbers in some order, and the numbers should be equal to the numbers in some order.
It can be shown that an answer always exists. If there are multiple possible answers, you may print any of them.
Example
input
2
3
1 8 5
8 4 5
3
1 7 5
6 1 2
output
1 8 5
8 4 5
5 1 7
6 2 1
Note
In the first test case, it is enough to give the -th necklace and the -th bracelet to the -th daughter. The corresponding sums are ,$ 8+4=12$, and .
The second test case is described in the statement.
Translation
给两个数组,它们都有个元素,输出,使得每一个是唯一的,保证这样的解存在。
Idea
最小的和最小的配对,最大的和最大的配对。
Code
1 |
|
B. Kuroni and Simple Strings
Now that Kuroni has reached 10 years old, he is a big boy and doesn’t like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!
We say that a string formed by n characters ‘(’ or ‘)’ is simple if its length is even and positive, its first characters are ‘(’, and its last characters are ‘)’. For example, the strings “()” and “(())” are simple, while the strings “)(” and “()()” are not simple.
Kuroni will be given a string formed by characters ‘(’ and ‘)’ (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn’t have to be continuous. For example, he can apply the operation to the string “)()(()))”, to choose a subsequence of bold characters, as it forms a simple string “(())”, delete these bold characters from the string and to get “))()”.
Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.
Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?
A sequence of characters a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The only line of input contains a string formed by characters ‘(’ and ‘)’, where is the length of .
Output
In the first line, print an integer — the minimum number of operations you have to apply. Then, print lines describing the operations in the following format:
For each operation, print a line containing an integer — the number of characters in the subsequence you will remove.
Then, print a line containing m integers — the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string.
If there are multiple valid sequences of operations with the smallest , you may print any of them.
Examples
input
(()((
output
1
2
1 3
input
)(
output
0
input
(()())
output
1
4
1 2 5 6
Note
In the first sample, the string is "(()((. The operation described corresponds to deleting the bolded subsequence. The resulting string is “(((”, and no more operations can be performed on it. Another valid answer is choosing indices 2 and 3, which results in the same final string.
In the second sample, it is already impossible to perform any operations.
Translation
要从一个括号序列中删除一个形如 $ stringsimple$ $ string$ 前个字符都为’(‘,后个字符都为’)'。
Idea
若原字符串中含有 $ string$,那么我们一次把这个 $ string$ 全部删除,最少的操作次数是;如果根本没有 $ string$,那么最少操作次数就是。
删除对称的括号时,为了不破坏对称性,用双指针 从两边向中心逼近,对称地删除括号,这样得到的解是最优的。
Code
1 |
|
C. Kuroni and Impossible Calculation
To become the king of Codeforces, Kuroni has to solve the following problem.
He is given numbers . Help Kuroni to calculate . As result can be very big, output it modulo .
If you are not familiar with short notation, $\prod\limits_{1 \leqslant i < j \leqslant n} {\left| a_i - a_j \right|} $ is equal to . In other words, this is the product of for all .
Input
The first line contains two integers — number of numbers and modulo.
The second line contains integers .
Output
Output the single number — .
Examples
input
2 10
8 5
output
3
input
3 12
1 4 5
output
0
input
3 7
1 4 9
output
1
Note
In the first sample, .
In the second sample, .
In the third sample, .
Translation
给一个数组,计算。
Idea
直接暴力计算肯定不行,但是有意思的是,题目给了模数。
当时,我们可以直接暴力算;但是当时(比如),显然会TLE,这时模数 就很耐人寻味。
一个数,对取余的结果在区间内,也就是说,个数中最对只能有个数对取余的结果不同,剩下的个数一定能在个数中找到余数相同的数,所以相乘答案为。
Code
1 |
|
D. Kuroni and the Celebration
This is an interactive problem.
After getting AC after 13 Time Limit Exceeded verdicts on a geometry problem, Kuroni went to an Italian restaurant to celebrate this holy achievement. Unfortunately, the excess sauce disoriented him, and he’s now lost!
The United States of America can be modeled as a tree (why though) with vertices. The tree is rooted at vertex , wherein lies Kuroni’s hotel.
Kuroni has a phone app designed to help him in such emergency cases. To use the app, he has to input two vertices and , and it’ll return a vertex , which is the lowest common ancestor of those two vertices.
However, since the phone’s battery has been almost drained out from live-streaming Kuroni’s celebration party, he could only use the app at most times. After that, the phone would die and there will be nothing left to help our dear friend! :(
As the night is cold and dark, Kuroni needs to get back, so that he can reunite with his comfy bed and pillow(s). Can you help him figure out his hotel’s location?
Interaction
The interaction starts with reading a single integer , the number of vertices of the tree.
Then you will read lines, the i-th of them has two integers and , denoting there is an edge connecting vertices and . It is guaranteed that the edges will form a tree.
Then you can make queries of type “? u v” $ (1≤u,v≤n)$ to find the lowest common ancestor of vertex and .
After the query, read the result w as an integer.
In case your query is invalid or you asked more than queries, the program will print and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you find out the vertex r, print “! r” and quit after that. This query does not count towards the limit.
Note that the tree is fixed beforehand and will not change during the queries, i.e. the interactor is not adaptive.
After printing any query do not forget to print end of line and flush the output. Otherwise, you might get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
see the documentation for other languages.
Hacks
To hack, use the following format:
The first line should contain two integers and , denoting the number of vertices and the vertex with Kuroni’s hotel.
The -th of the next lines should contain two integers and — denoting there is an edge connecting vertex and .
The edges presented should form a tree.
Example
input
6
1 4
4 2
5 3
6 3
2 3
3
4
4
output
? 5 6
? 3 1
? 1 2
! 4
Note
Note that the example interaction contains extra empty lines so that it’s easier to read. The real interaction doesn’t contain any empty lines and you shouldn’t print any extra empty lines as well.
The image below demonstrates the tree in the sample test:
Translation
这是一道互动题,测评 机会给你一颗树,你可以向测评机询问一对节点的LCA,要求询问不超过$\left\lfloor {\frac{n}{2}} \right\rfloor $次,得到树的根节点。
Idea
应该说,树的根节点应该是相对的,每一个节点都可以作为根节点。通过询问,我们能找到测评机所选定的根节点。
我们将所有节点放入集合,并建成树形图;每次询问一对叶子节点(度数为)的LCA,如果得到的LCA是这两个叶子节点的其中一个,那么我们就找到根节点了;如果还没有找到,我们就把叶子节点删掉(从节点集合中去掉,从树上删除节点),继续找。
Code
1 |
|
E. Kuroni and the Score Distribution
Kuroni is the coordinator of the next Mathforces round written by the “Proof by AC” team. All the preparation has been done, and he is discussing with the team about the score distribution for the round.
The round consists of n problems, numbered from to . The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array , where is the score of -th problem.
Kuroni thinks that the score distribution should satisfy the following requirements:
The score of each problem should be a positive integer not exceeding .
A harder problem should grant a strictly higher score than an easier problem. In other words, .
The balance of the score distribution, defined as the number of triples such that and , should be exactly .
Help the team find a score distribution that satisfies Kuroni’s requirement. In case such a score distribution does not exist, output .
Input
The first and single line contains two integers and — the number of problems and the required balance.
Output
If there is no solution, print a single integer .
Otherwise, print a line containing n integers , representing a score distribution that satisfies all the requirements. If there are multiple answers, print any of them.
Examples
input
5 3
output
4 5 9 13 18
input
8 0
output
10 11 12 13 14 15 16 17
input
4 10
output
-1
Note
In the first example, there are triples that contribute to the balance of the score distribution.
Translation
构造一个长度为的数列,使得满足的三元数对的个数恰好为个。
Idea
显然这样的数字能构成最多的满足条件的数对。对于,满足条件的数对个数有。若最多只给个数,但是仍然小于,那么就不能构造出要求的数列;否则一定能构造出来。
找规律得到,末尾的每增加,满足条件的数对就减少一个,所以只要输出刚刚达到上限的,若还不足个数,以后每隔输出一个数作为补全。
Code
1 |
|