Codeforces Round #588 (Div. 2) A ~ D

                                                             A. Dawid and Bags of Candies

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Dawid has four bags of candies. The ii-th of them contains aiai candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?

Note, that you can't keep bags for yourself or throw them away, each bag should be given to one of the friends.

Input

The only line contains four integers a1a1, a2a2, a3a3 and a4a4 (1≤ai≤1001≤ai≤100) — the numbers of candies in each bag.

Output

Output YES if it's possible to give the bags to Dawid's friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).

Examples

input

Copy

1 7 11 5

output

Copy

YES

input

Copy

7 3 2 5

output

Copy

NO

Note

In the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive 1212 candies.

In the second sample test, it's impossible to distribute the bags.

题目大意 :

有四袋糖果,问能否平均分给两个人

解法:

先排序,然后看能否1 - 3分,或者2 - 2分,其中2 - 2分有两种情况,能就输出“YES”

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;
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 main()
{
	int a[10];
	cin >> a[0] >> a[1] >> a[2] >> a[3];
	sort(a, a + 4);
	if (a[3] == a[0] + a[1] + a[2] || a[0] + a[3] == a[2] + a[1] || a[3] + a[1] == a[0] + a[2])
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

                                                                         B. Ania and Minimizing

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ania has a large integer SS. Its decimal representation has length nn and doesn't contain any leading zeroes. Ania is allowed to change at most kk digits of SS. She wants to do it in such a way that SS still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?

Input

The first line contains two integers nn and kk (1≤n≤2000001≤n≤200000, 0≤k≤n0≤k≤n) — the number of digits in the decimal representation of SS and the maximum allowed number of changed digits.

The second line contains the integer SS. It's guaranteed that SS has exactly nn digits and doesn't contain any leading zeroes.

Output

Output the minimal possible value of SS which Ania can end with. Note that the resulting integer should also have nn digits.

Examples

input

Copy

5 3
51528

output

Copy

10028

input

Copy

3 2
102

output

Copy

100

input

Copy

1 1
1

output

Copy

0

Note

A number has leading zeroes if it consists of at least two digits and its first digit is 00. For example, numbers 0000, 0006900069 and 01010101 have leading zeroes, while 00, 30003000 and 10101010 don't have leading zeroes.

题目大意 :

一个长度为N的字符串表示的数字,你可以最多更改其中的K个数字,输出更改后的最小数

解法:

特判几种情况:1、K = 0,直接输出   2、N = 1,输出0   3、如果第一位不为1,要将其该为1,剩下的位数能改0则改0

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;
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 n, k;

int main()
{
	cin >> n >> k >> str;
	if (!k)
		cout << str << endl, exit(0);
	if (n == 1)
		cout << 0 << endl, exit(0);
	if (str[0] != '1')
		str[0] = '1', k--;
	int j = 1;
	while (j < n && k) {       // 能改0则改
		if (str[j] != '0')
			str[j] = '0', k--;
		j++;
	}
	cout << str << endl;
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

                                                                           C. Anadi and Domino

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every aa and bb such that 1≤a≤b≤61≤a≤b≤6, there is exactly one domino with aa dots on one half and bb dots on the other half. The set contains exactly 2121 dominoes. Here is an exact illustration of his set:

Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.

When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.

How many dominoes at most can Anadi place on the edges of his graph?

Input

The first line contains two integers nn and mm (1≤n≤71≤n≤7, 0≤m≤n⋅(n−1)20≤m≤n⋅(n−1)2) — the number of vertices and the number of edges in the graph.

The next mm lines contain two integers each. Integers in the ii-th line are aiai and bibi (1≤a,b≤n1≤a,b≤n, a≠ba≠b) and denote that there is an edge which connects vertices aiai and bibi.

The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.

Output

Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.

Examples

input

Copy

4 4
1 2
2 3
3 4
4 1

output

Copy

4

input

Copy

7 0

output

Copy

0

input

Copy

3 1
1 3

output

Copy

1

input

Copy

7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7

output

Copy

16

Note

Here is an illustration of Anadi's graph from the first sample test:

And here is one of the ways to place a domino on each of its edges:

Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 11 have three dots.

题目大意 :

有21个多米诺骨牌模型,现在让你将他们放到一张图的边上(只可放一次),使得一个点所连边的一侧骨牌上的数字相等,输出你最多可以放多少个骨牌

解法:

转换一下就是点染色,由于点特别小,DFS即可解决

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;
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 op[10], n, m, ans;
bool edge[10][10], G[10][10];

int check() {
	int tot = 0;
	MEM(edge, 0);
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (!G[i][j])              // 存在该边
				continue;
			if (!edge[op[i]][op[j]])    // 骨牌未使用过
				tot++, edge[op[i]][op[j]] = edge[op[j]][op[i]] = true;
		}
	}
	return tot;
}
void dfs(int x) {
	if (x == n + 1) {
		Max(ans, check());
		return;
	}
	for (int i = 1; i <= 6; i++) {  // 每个点染哪个颜色
		op[x] = i;
		dfs(x + 1);
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int ui, vi;
		sc("%d %d", &ui, &vi);
		G[ui][vi] = true, G[vi][ui] = true;
	}
	dfs(1);
	cout << ans << endl;
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

                                                                      D. Marcin and Training Camp

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Marcin is a coach in his university. There are nn students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.

Let's focus on the students. They are indexed with integers from 11 to nn. Each of them can be described with two integers aiai and bibi; bibi is equal to the skill level of the ii-th student (the higher, the better). Also, there are 6060 known algorithms, which are numbered with integers from 00 to 5959. If the ii-th student knows the jj-th algorithm, then the jj-th bit (2j2j) is set in the binary representation of aiai. Otherwise, this bit is not set.

Student xx thinks that he is better than student yy if and only if xx knows some algorithm which yy doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.

Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?

Input

The first line contains one integer nn (1≤n≤70001≤n≤7000) — the number of students interested in the camp.

The second line contains nn integers. The ii-th of them is aiai (0≤ai<2600≤ai<260).

The third line contains nn integers. The ii-th of them is bibi (1≤bi≤1091≤bi≤109).

Output

Output one integer which denotes the maximum sum of bibi over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.

Examples

input

Copy

4
3 2 3 6
2 8 5 10

output

Copy

15

input

Copy

3
1 2 3
1 2 3

output

Copy

0

input

Copy

1
0
1

output

Copy

0

Note

In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of bibi.

In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.

题目大意 :

有N个学生, 每个学生有两个属性,一是60项技能的掌握情况(用二进制表示),二是能力值。学生A认为自己比学生B强,当且仅当学生A掌握的某项技能学生B未掌握,同时两个学生都可能认为自己比对方强,如果一个集合中,有学生认为自己比所有人都强,那么他们将无法工作,输出一个X,表示至少两人组成的集合能力值总和的最大值

解法:

N特别小,但是技能掌握情况共有2 ^ 60次方个,所以先将所有技能掌握情况相同的学生合并为一个,能力值也合并,然后开始合并能够一起工作的,观察后可以发现,如果两个集合(指技能掌握情况相同的)的人都不止一个,那么他们一定可以一起工作,因为每个人都有技能掌握情况相同的,而一个集合人数 > 1,另一个人数 == 1,只有在集合人数 > 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 = 7e3 + 100;
const int INF = 0x3f3f3f3f;
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 f[N], sz[N], tot;
ll a[N], dp[N];
vector <int> G[N << 1];
unordered_map <ll, int> num, idx;
unordered_map <ll, ll> val, ev;

void init() {
	for (int i = 1; i <= tot; i++)
		f[i] = i, sz[i] = 1, dp[i] = val[ev[i]];
}
int find_(int x) {
	while (x != f[x])
		x = f[x];
	return x;
}
void unite(int x, int y) {
	x = find_(x);
	y = find_(y);
	if (x == y)
		return;
	if (sz[x] > sz[y])
		swap(x, y);
	f[x] = y;
	sz[y] += sz[x];
	dp[y] += dp[x];   // 能力值合并
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		sc("%lld", &a[i]);
		num[a[i]]++;
		if (idx.count(a[i]) == NULL)
			idx[a[i]] = ++tot, ev[tot] = a[i];  // idx为编号,ev为反编号
	}
	for (int i = 1; i <= n; i++) {
		ll w;
		sc("%lld", &w);
		val[a[i]] += w;           // 合并相同情况的能力值
	}
	init();
	for (auto i : num) {
		for (auto j : num) {
			ll ski1 = i.first, ski2 = j.first;     // 技能掌握情况
			int id1 = idx[ski1], id2 = idx[ski2];   // 编号
			int num1 = i.second, num2 = j.second;    // 数量
			if (ski1 == ski2)
				continue;
			if (num1 > 1 && num2 > 1)            // 都不止一个,可以合并
				unite(id1, id2);
			ll cnt = (ski1 | ski2); 
			if (cnt == ski1 && num1 > 1)          // 以下两种为人多且强的情况
				unite(id1, id2);
			if (cnt == ski2 && num2 > 1)
				unite(id1, id2);
		}
	}
	ll mx = 0;
	for (int i = 1; i <= tot; i++) {
		if (num[ev[i]] == 1)         // 单独一个不考虑
			continue;
		Max(mx, dp[find_(i)]);
	}
	cout << mx << endl;
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

猜你喜欢

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