Codeforces Round #615 (Div. 3) A, B, C, D, F

A. Collecting Coins

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has aa coins, Barbara has bb coins and Cerene has cc coins. Recently Polycarp has returned from the trip around the world and brought nn coins.

He wants to distribute all these nn coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives AA coins to Alice, BB coins to Barbara and CC coins to Cerene (A+B+C=nA+B+C=n), then a+A=b+B=c+Ca+A=b+B=c+C.

Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.

Your task is to find out if it is possible to distribute all nn coins between sisters in a way described above.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The next tt lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a,b,ca,b,c and nn (1≤a,b,c,n≤1081≤a,b,c,n≤108) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.

Output

For each test case, print "YES" if Polycarp can distribute all nn coins between his sisters and "NO" otherwise.

Example

input

Copy

5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3

output

Copy

YES
YES
NO
NO
YES

题目大意 :

三个人初始分别有X, Y, Z个硬币,第四个人有W个硬币,现在第四个人要将所有硬币分给前三个人使得他们的硬币数量相等,输出是否能够满足

解法:

试着先让三个人相等,最后剩余的平分就可以

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 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 p[5];

int main()
{
	int T; cin >> T;
	while (T--) {
		for (int i = 1; i <= 3; i++)
			sc("%d", &p[i]);
		int tmp; sc("%d", &tmp);
		sort(p + 1, p + 4);
		int ans = 2 * p[3] - p[1] - p[2];
		if (tmp < ans) { cout << "NO" << endl; continue; }
		tmp -= ans;
		if (tmp % 3 == 0) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;  // 改数组大小!!!
}

B. Collecting Packages

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.

The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).

As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.

The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1≤j≤n1≤j≤n that for all ii from 11 to j−1j−1 si=tisi=ti and sj<tjsj<tj. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.

Input

The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then test cases follow.

The first line of a test case contains one integer nn (1≤n≤10001≤n≤1000) — the number of packages.

The next nn lines contain descriptions of packages. The ii-th package is given as two integers xixi and yiyi (0≤xi,yi≤10000≤xi,yi≤1000) — the xx-coordinate of the package and the yy-coordinate of the package.

It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.

The sum of all values nn over test cases in the test doesn't exceed 10001000.

Output

Print the answer for each test case.

If it is impossible to collect all nn packages in some order starting from (0,00,0), print "NO" on the first line.

Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path.

Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable.

Example

input

Copy

3
5
1 3
1 2
3 3
5 5
4 3
2
1 0
0 1
1
4 3

output

Copy

YES
RUUURRRRUU
NO
YES
RRRRUUU

Note

For the first test case in the example the optimal path RUUURRRRUU is shown below:

题目大意 :

一个机器人,初始位置为(0, 0),只能往右往上走,给出若干个点的坐标,输出经过所有点的最小字典序,如果不能经过所有点,输出NO

解法:

字典序最小显然是先R再U,不难看出先排序,每次走到一个坐标时,将正上方的所有点全部走完,如果接下来有更低的点,那么一定不可行

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 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 = 2e3 + 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; }

struct node
{
	int x, y;
}p[MAXN];
bool cmp(node a, node b) {
	if (a.x == b.x)
		return a.y < b.y;
	return a.x < b.x;
}
int n;

int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%d", &n);
		p[0] = { 0, 0 };
		for (int i = 1; i <= n; i++)
			sc("%d %d", &p[i].x, &p[i].y);
		sort(p + 1, p + n + 1, cmp);
		bool flag = true;
		string step;
		for (int i = 1; i <= n; i++) {
			int last_x = p[i - 1].x;   // 上一次的坐标
			int last_y = p[i - 1].y;
			int now_x = p[i].x;
			int now_y = p[i].y;
			if (now_x < last_x || now_y < last_y) {  // 更低了不可行
				flag = false;
				break;
			}
			int ri = now_x - last_x;
			for (int j = 0; j < ri; j++) step += "R";
			int up = now_y - last_y;
			for (int j = 0; j < up; j++) step += "U";
		}
		if (!flag) { cout << "NO" << endl; continue; }
		cout << "YES" << endl;
		cout << step << endl;
	}
	return 0;  // 改数组大小!!!
}

C. Product of Three Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.

If there are several answers, you can print any.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The next nn lines describe test cases. The ii-th test case is given on a new line as one integer nn (2≤n≤1092≤n≤109).

Output

For each test case, print the answer on it. Print "NO" if it is impossible to represent nn as a⋅b⋅ca⋅b⋅c for some distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c.

Otherwise, print "YES" and any possible such representation.

Example

input

Copy

5
64
32
97
2
12345

output

Copy

YES
2 4 8 
NO
NO
NO
YES
3 5 823 

题目大意 :

输入一个N,找到三个数a, b, c,使得a * b * c == N, 不存在则输出NO

解法:

暴力即可

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 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 p[5];

int main()
{
	int T; cin >> T;
	while (T--) {
		ll n; sc("%lld", &n);
		int tot = 0;
		for (ll i = 2; i < sqrt(n); i++) {
			if (n % i == 0) {
				p[tot++] = i; 
				n /= i;
				break;
			}
		}
		if (!tot) { cout << "NO" << endl; continue; }
		for (ll i = 2; i < sqrt(n); i++) {
			if (n % i == 0 && i != p[0] && n / i != p[0]) {
				p[tot++] = i;
				p[tot++] = n / i;
				break;
			}
		}
		if (tot != 3) cout << "NO" << endl;
		else {
			cout << "YES" << endl;
			for (int i = 0; i < tot; i++)
				cout << p[i] << " ";
			cout << endl;
		}
	}
	return 0;  // 改数组大小!!!
}

D. MEX maximizing

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:

  • for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are presented in the array and 33 is the minimum non-negative integer not presented in the array;
  • for the array [1,2,3,4][1,2,3,4] MEX equals to 00 because 00 is the minimum non-negative integer not presented in the array;
  • for the array [0,1,4,3][0,1,4,3] MEX equals to 22 because 22 is the minimum non-negative integer not presented in the array.

You are given an empty array a=[]a=[] (in other words, a zero-length array). You are also given a positive integer xx.

You are also given qq queries. The jj-th query consists of one integer yjyj and means that you have to append one element yjyj to the array. The array length increases by 11 after a query.

In one move, you can choose any index ii and set ai:=ai+xai:=ai+x or ai:=ai−xai:=ai−x (i.e. increase or decrease any element of the array by xx). The only restriction is that aiai cannot become negative. Since initially the array is empty, you can perform moves only after the first query.

You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).

You have to find the answer after each of qq queries (i.e. the jj-th answer corresponds to the array of length jj).

Operations are discarded before each query. I.e. the array aa after the jj-th query equals to [y1,y2,…,yj][y1,y2,…,yj].

Input

The first line of the input contains two integers q,xq,x (1≤q,x≤4⋅1051≤q,x≤4⋅105) — the number of queries and the value of xx.

The next qq lines describe queries. The jj-th query consists of one integer yjyj (0≤yj≤1090≤yj≤109) and means that you have to append one element yjyj to the array.

Output

Print the answer to the initial problem after each query — for the query jj print the maximum value of MEX after first jj queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.

Examples

input

Copy

7 3
0
1
2
2
0
0
10

output

Copy

1
2
3
3
4
4
7

input

Copy

4 3
1
2
1
2

output

Copy

0
0
0
0

Note

In the first example:

  • After the first query, the array is a=[0]a=[0]: you don't need to perform any operations, maximum possible MEX is 11.
  • After the second query, the array is a=[0,1]a=[0,1]: you don't need to perform any operations, maximum possible MEX is 22.
  • After the third query, the array is a=[0,1,2]a=[0,1,2]: you don't need to perform any operations, maximum possible MEX is 33.
  • After the fourth query, the array is a=[0,1,2,2]a=[0,1,2,2]: you don't need to perform any operations, maximum possible MEX is 33 (you can't make it greater with operations).
  • After the fifth query, the array is a=[0,1,2,2,0]a=[0,1,2,2,0]: you can perform a[4]:=a[4]+3=3a[4]:=a[4]+3=3. The array changes to be a=[0,1,2,2,3]a=[0,1,2,2,3]. Now MEX is maximum possible and equals to 44.
  • After the sixth query, the array is a=[0,1,2,2,0,0]a=[0,1,2,2,0,0]: you can perform a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3. The array changes to be a=[0,1,2,2,3,0]a=[0,1,2,2,3,0]. Now MEX is maximum possible and equals to 44.
  • After the seventh query, the array is a=[0,1,2,2,0,0,10]a=[0,1,2,2,0,0,10]. You can perform the following operations:
    • a[3]:=a[3]+3=2+3=5a[3]:=a[3]+3=2+3=5,
    • a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3,
    • a[5]:=a[5]+3=0+3=3a[5]:=a[5]+3=0+3=3,
    • a[5]:=a[5]+3=3+3=6a[5]:=a[5]+3=3+3=6,
    • a[6]:=a[6]−3=10−3=7a[6]:=a[6]−3=10−3=7,
    • a[6]:=a[6]−3=7−3=4a[6]:=a[6]−3=7−3=4.
    The resulting array will be a=[0,1,2,5,3,6,4]a=[0,1,2,5,3,6,4]. Now MEX is maximum possible and equals to 77.

题目大意 :

输入一个数X,Q次操作,每次给你一个数T,你可以将T增加或减少任意数量的X,使得当前的MEX最大,并输出MEX

解法:

看到任意数量的X,不难想到和取模有关,每次输入一个数的时候存起来,然后看看能不能使MEX + 1,能就一直加下去,并减去相应的消耗

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 MPY(x, b) memcpy(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 N = 4e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(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; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

int a[N];

int main()
{
	int n, x, ans = 0;
	cin >> n >> x;
	while (n--) {
		int t;
		sc("%d", &t);
		a[t % x]++; 
		while (a[ans % x]) {
			a[ans % x]--;
			ans++;
		}
		printf("%d\n", ans);
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

F. Three Paths on a Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.

Your task is to choose three distinct vertices a,b,ca,b,c on this tree such that the number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc is the maximum possible. See the notes section for a better understanding.

The simple path is the path that visits each vertex at most once.

Input

The first line contains one integer number nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of vertices in the tree.

Next n−1n−1 lines describe the edges of the tree in form ai,biai,bi (1≤ai1≤ai, bi≤nbi≤n, ai≠biai≠bi). It is guaranteed that given graph is a tree.

Output

In the first line print one integer resres — the maximum number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc.

In the second line print three integers a,b,ca,b,c such that 1≤a,b,c≤n1≤a,b,c≤n and a≠,b≠c,a≠ca≠,b≠c,a≠c.

If there are several answers, you can print any.

Example

input

Copy

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

output

Copy

5
1 8 6

Note

The picture corresponding to the first example (and another one correct answer):

If you choose vertices 1,5,61,5,6 then the path between 11 and 55 consists of edges (1,2),(2,3),(3,4),(4,5)(1,2),(2,3),(3,4),(4,5), the path between 11 and 66 consists of edges (1,2),(2,3),(3,4),(4,6)(1,2),(2,3),(3,4),(4,6) and the path between 55 and 66 consists of edges (4,5),(4,6)(4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6)(1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 55. It can be shown that there is no better answer.

题目大意 :

给你一棵树,你要找到三个点a, b, c,使得他们之间的路径交最大,输出最大值和三个点

解法:

前两个点肯定是找直径,第三个点应该是距离直径端点之间的路径最远的点,这样想的话可以利用树上差分标记一下直径上的边,然后将直径中的所有点全部加入队列,跑最长路,最后遍历一下看哪个的距离最长,并且非端点(没考虑到这个wa了一发)

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 MPY(x, b) memcpy(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 N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(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; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

vector <int> G[N << 1];
int c[N], dis[N];
int n, mx, rt1, rt2;

void dfs(int x, int fa, int d) {
	if (d > mx)
		mx = d, rt2 = x;
	for (auto v : G[x]) {
		if (v != fa)
			dfs(v, x, d + 1);
	}
}
void DFS(int x, int fa) {
	for (auto v : G[x]) {
		if (v == fa)
			continue;
		DFS(v, x);
		c[x] += c[v];
	}
}
void bfs() {
	priority_queue <pir, vector <pir>, greater <pir>> q;
	MEM(dis, INF);
	for (int i = 1; i <= n; i++) {
		if (c[i])
			q.push({ 0, i }), dis[i] = 0;
	}
	while (!q.empty()) {
		pir now = q.top();
		q.pop();
		int u = now.second;
		for (auto v : G[u]) {
			if (dis[v] > dis[u] + 1) {
				dis[v] = dis[u] + 1;
				q.push({ dis[v], v });
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++) {
		int ui, vi;
		sc("%d %d", &ui, &vi);
		G[ui].push_back(vi);
		G[vi].push_back(ui);
	}
	dfs(1, 0, 0); 
	mx = 0, rt1 = rt2;
	dfs(rt2, 0, 0);      // rt1,rt2为直径的端点
	c[rt2]++;            // 差分一下
	DFS(rt1, 0);          // 再DFS标记直径上的点
	bfs();                  // 跑最长路
	int ans = mx, idx;
	mx = -INF;
	for (int i = 1; i <= n; i++) {
		if (dis[i] > mx && i != rt1 && i != rt2)
			mx = dis[i], idx = i;
	}
	printf("%d\n%d %d %d\n", ans + mx, rt1, rt2, idx);
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

猜你喜欢

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