Codeforces Ozon Tech Challenge 2020 (Div1 + Div2) A ~ D

                                                                           A. Kuroni and the Gifts

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Kuroni has nn daughters. As gifts for them, he bought nn necklaces and nn bracelets:

  • the ii-th necklace has a brightness aiai, where all the aiai are pairwise distinct (i.e. all aiai are different),
  • the ii-th bracelet has a brightness bibi, where all the bibi are pairwise distinct (i.e. all bibi 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 ii-th daughter receives a necklace with brightness xixi and a bracelet with brightness yiyi, then the sums xi+yixi+yi should be pairwise distinct. Help Kuroni to distribute the gifts.

扫描二维码关注公众号,回复: 11446101 查看本文章

For example, if the brightnesses are a=[1,7,5]a=[1,7,5] and b=[6,1,2]b=[6,1,2], 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 a3+b1=11a3+b1=11.
  • Give the first necklace and the third bracelet to the second daughter, for a total brightness of a1+b3=3a1+b3=3.
  • Give the second necklace and the second bracelet to the third daughter, for a total brightness of a2+b2=8a2+b2=8.

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 a1+b1=7a1+b1=7.
  • Give the second necklace and the second bracelet to the second daughter, for a total brightness of a2+b2=8a2+b2=8.
  • Give the third necklace and the third bracelet to the third daughter, for a total brightness of a3+b3=7a3+b3=7.

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 tt (1≤t≤1001≤t≤100)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100)  — the number of daughters, necklaces and bracelets.

The second line of each test case contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000)  — the brightnesses of the necklaces.

The third line of each test case contains nn distinct integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤10001≤bi≤1000)  — the brightnesses of the bracelets.

Output

For each test case, print a line containing nn integers x1,x2,…,xnx1,x2,…,xn, representing that the ii-th daughter receives a necklace with brightness xixi. In the next line print nn integers y1,y2,…,yny1,y2,…,yn, representing that the ii-th daughter receives a bracelet with brightness yiyi.

The sums x1+y1,x2+y2,…,xn+ynx1+y1,x2+y2,…,xn+yn should all be distinct. The numbers x1,…,xnx1,…,xn should be equal to the numbers a1,…,ana1,…,an in some order, and the numbers y1,…,yny1,…,yn should be equal to the numbers b1,…,bnb1,…,bn 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

Copy

2
3
1 8 5
8 4 5
3
1 7 5
6 1 2

output

Copy

1 8 5
8 4 5
5 1 7
6 2 1

Note

In the first test case, it is enough to give the ii-th necklace and the ii-th bracelet to the ii-th daughter. The corresponding sums are 1+8=91+8=9, 8+4=128+4=12, and 5+5=105+5=10.

The second test case is described in the statement.

题目大意 :

给你两个数组,保证每个数组中的元素的值一定不同,让你对两个数组进行重新排列,使得对应下标的和都不相同

思路 :

排序后,两个数组对应下标之和一定不同

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

int a[1100], b[1100], n;

int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%d", &n);
		for (int i = 0; i < n; i++)
			cin >> a[i];
		for (int i = 0; i < n; i++)
			cin >> b[i];
		sort(a, a + n); sort(b, b + n);
		for (int i = 0; i < n; i++)
			cout << a[i] << " ";
		cout << endl;
		for (int i = 0; i < n; i++)
			cout << b[i] << " ";
		cout << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                               B. Kuroni and Simple Strings

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn characters '(' or ')' is simple if its length nn is even and positive, its first n2n2 characters are '(', and its last n2n2 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 aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.

Input

The only line of input contains a string ss (1≤|s|≤10001≤|s|≤1000) formed by characters '(' and ')', where |s||s| is the length of ss.

Output

In the first line, print an integer kk  — the minimum number of operations you have to apply. Then, print 2k2k lines describing the operations in the following format:

For each operation, print a line containing an integer mm  — the number of characters in the subsequence you will remove.

Then, print a line containing mm integers 1≤a1<a2<⋯<am1≤a1<a2<⋯<am  — 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 kk, you may print any of them.

Examples

input

Copy

(()((

output

Copy

1
2
1 3 

input

Copy

)(

output

Copy

0

input

Copy

(()())

output

Copy

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 22 and 33, which results in the same final string.

In the second sample, it is already impossible to perform any operations.

题目大意 :

给你一个括号序列,保证两种括号的数量相同,你要对于该序列进行此操作:删除一个子序列(不一定连续),使得前一半为 “(”, 后一半为 “)”,直到无法进行该操作,输出你最少执行多少次,并且升序输出每次执行的时候,删除的下标

思路 :

实在想不到好的方法,只好暴力,每次删除前都对该序列求一下某位置包括本身左边有多少个 “(”, 右边有多少个“)”,如果该位置两个值相等,那么说明可以删除这样的序列,遍历一遍后将删除的位置标记并保存,直到无法删除

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

string str;
int a[1100], b[1100], pre[1100], tot;
vector <int> G[1100];
bool vis[1100];

int main()
{
	cin >> str;
	string s = "0";
	s += str;
	str = s;
	int sz = SZ(str) - 1;
	while (true) {
		MEM(a, 0); MEM(b, 0);         // 初始化
		for (int i = 1; i <= sz; i++) {      // 左边包括本身有多少“(”
			a[i] += a[i - 1];
			if (vis[i])
				continue;
			if (str[i] == '(')
				a[i]++;
		}
		for (int i = sz; i >= 1; i--) {      // 右边有多少“)”
			b[i] += b[i + 1];
			if (vis[i])
				continue;
			if (str[i] == ')')
				b[i]++;
		}
		int max_ = 0;
		for (int i = 1; i <= sz; i++) 
			Max(max_, min(a[i], b[i + 1]));  // 找到删除的数量
		if (max_ == 0)                      // 无法删除退出
			break;
		tot++;
		int l = 0, r = 0;
		for (int i = 1; i <= sz; i++) {
			if (l == max_)
				break;
			if (vis[i])
				continue;
			if (str[i] == '(')
				G[tot].push_back(i), l++, vis[i] = true;  // 标记即删除
		}
		for (int i = sz; i >= 1; i--) {
			if (r == max_)
				break;
			if (vis[i])
				continue;
			if (str[i] == ')')
				G[tot].push_back(i), r++, vis[i] = true;
		}
	}
	cout << tot << endl;
	for (int i = 1; i <= tot; i++) {
		cout << SZ(G[i]) << endl;
		sort(ALL(G[i]));
		for (auto it : G[i])
			cout << it << " ";
		cout << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                          C. Kuroni and Impossible Calculation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

To become the king of Codeforces, Kuroni has to solve the following problem.

He is given nn numbers a1,a2,…,ana1,a2,…,an. Help Kuroni to calculate ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj|. As result can be very big, output it modulo mm.

If you are not familiar with short notation, ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj| is equal to |a1−a2|⋅|a1−a3|⋅|a1−a2|⋅|a1−a3|⋅ …… ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ …… ⋅|a2−an|⋅⋅|a2−an|⋅ …… ⋅|an−1−an|⋅|an−1−an|. In other words, this is the product of |ai−aj||ai−aj| for all 1≤i<j≤n1≤i<j≤n.

Input

The first line contains two integers nn, mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤10001≤m≤1000) — number of numbers and modulo.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109).

Output

Output the single number — ∏1≤i<j≤n|ai−aj|modm∏1≤i<j≤n|ai−aj|modm.

Examples

input

Copy

2 10
8 5

output

Copy

3

input

Copy

3 12
1 4 5

output

Copy

0

input

Copy

3 7
1 4 9

output

Copy

1

Note

In the first sample, |8−5|=3≡3mod10|8−5|=3≡3mod10.

In the second sample, |1−4|⋅|1−5|⋅|4−5|=3⋅4⋅1=12≡0mod12|1−4|⋅|1−5|⋅|4−5|=3⋅4⋅1=12≡0mod12.

In the third sample, |1−4|⋅|1−9|⋅|4−9|=3⋅8⋅5=120≡1mod7|1−4|⋅|1−9|⋅|4−9|=3⋅8⋅5=120≡1mod7.

题目大意 :

给你N个数,让你输出任意两个位置的数,差的绝对值的乘积的乘积对M取模后的结果,i和j只执行一次

思路 :

由于M给的很小,所以如果N的数量 > M,一定存在两个对m取模为0的值,那么乘积变成0,对于≤M的情况,直接暴力求解

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

ll a[MAXN], n, m, ans = 1;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		sc("%lld", &a[i]);
	if (n > m)
		cout << 0 << endl, exit(0);
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j <= n; j++) {
			ans = ((ans % m) * abs(a[i] - a[j]) % m) % m;
		}
	}
	cout << ans << endl;
	return 0;  // 改数组大小!!!
}

                                                            D. Kuroni and the Celebration

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn vertices. The tree is rooted at vertex rr, 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 uu and vv, and it'll return a vertex ww, 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 ⌊n2⌋⌊n2⌋ 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 nn (2≤n≤10002≤n≤1000), the number of vertices of the tree.

Then you will read n−1n−1 lines, the ii-th of them has two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n, xi≠yixi≠yi), denoting there is an edge connecting vertices xixi and yiyi. It is guaranteed that the edges will form a tree.

Then you can make queries of type "? u v" (1≤u,v≤n1≤u,v≤n) to find the lowest common ancestor of vertex uu and vv.

After the query, read the result ww as an integer.

In case your query is invalid or you asked more than ⌊n2⌋⌊n2⌋ queries, the program will print −1−1 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 rr, print "! rr" and quit after that. This query does not count towards the ⌊n2⌋⌊n2⌋ 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 nn and rr (2≤n≤10002≤n≤1000, 1≤r≤n1≤r≤n), denoting the number of vertices and the vertex with Kuroni's hotel.

The ii-th of the next n−1n−1 lines should contain two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n) — denoting there is an edge connecting vertex xixi and yiyi.

The edges presented should form a tree.

Example

input

Copy

6
1 4
4 2
5 3
6 3
2 3

3

4

4

output

Copy




? 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:

题目大意 :

给你一棵包含N个点的树,你可以最多查询N / 2次两个点的LCA,让你输出最后这棵树的根

思路 :

一棵树至少有2个叶子结点,正好对应了最多查询N / 2次,所以不难往叶子上想,假设这棵树的根是在最上面,两个叶子的LCA已经得知,那么根一定是在本次查询的LCA的上方,也可能是该LCA,而查询的两个点到他们的LCA之间的点(不包括LCA),就没有意义了,N只有1000,所以足够删除不可能存在根的子树,以及查询剩下子树的叶子结点。最后只剩下一个点的时候,就是这棵树的根了。以样例为例,假设查询 5和6的LCA为3,那么删除3 5和3 6这两条无向边,从他们的LCA3开始寻找剩下子树中的任意两个叶子结点,找到了1和3, 1 和 3 的LCA为4, 那么删除 4 1和4 2这两条无向边,只剩下4即为答案

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

set <int> G[MAXN << 1];
int dep[MAXN], n, ui, vi, rt;
bool ok = false;

void dfs(int x, int fa, int ans) {
	if (x == ans) {              // 到LCA就停
		G[x].erase(fa);          // 删边
		G[fa].erase(x);
		dep[x]--;                // 更新度数
		ok = true;               
		return;
	}
	for (auto it : G[x]) {
		if (it != fa) {
			dfs(it, x, ans);
			if (ok)             // 防止段错误
				return;
		}
	}
}
void find_(int x, int fa) {
	if (dep[x] == 1 && !ui)      // 找叶子
		ui = x;
	else if (dep[x] == 1 && ui)
		vi = x;
	for (auto it : G[x]) {
		if (it != fa)
			find_(it, x);
	}
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++) {
		int ui, vi;
		sc("%d %d", &ui, &vi);
		G[ui].insert(vi);
		G[vi].insert(ui);
		dep[ui]++, dep[vi]++;
	}
	for (int i = n; i >= 1; i--) {   // 随便找两个叶子
		if (ui && vi)
			break;
		if (dep[i] == 1 && !ui)
			ui = i;
		else if (dep[i] == 1 && ui)
			vi = i;
	}
	int tot = n / 2, rt = 1;
	while (tot--) {
		cout << "? " << ui << " " << vi << endl;
		cin >> rt;
		ok = false; dfs(ui, 0, rt);    // 删除从ui到他们LCA这条路径最后连的边
		ok = false; dfs(vi, 0, rt);    // 同理
		ui = vi = 0;
		find_(rt, 0);                   // 随便找剩下子树的两个叶子
		if (!ui || !vi)                 // 若只有一个叶子,就是根
			break;
	}
	cout << "! " << rt << endl;
	return 0;  // 改数组大小!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/104755056