Codeforces Round #661(Div. 3)解题报告

Codeforces Round #661(Div. 3)

A. Remove Smallest

题目大意

∣ a i − a j ∣ ≤ 1 |a_{i}-a_{j}|\leq 1 aiaj1则可以删去 m i n ( a i , a j ) min(a_{i},a_{j}) min(ai,aj),问你能否将 a a a数组删至只剩一个元素

解题思路

a a a数组进行排序,对于每两个相邻的元素判断差值是否大于1

AC代码

#include <bits/stdc++.h>
using namespace std;
int a[100];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		bool flag = true;
		for (int i = 2; i <= n; ++i) {
			if (a[i] - a[i - 1] > 1) {
				flag = false;
				break;
			}
		}
		cout << (flag ? "YES" : "NO") << '\n';
	}
	return 0;
}

B. Gifts Fixing

题目大意

有两个序列 a a a b b b,你每次可以将 a i a_{i} ai减一或将 b i b_{i} bi减一或将它们同时减一,问你使得 a 1 = a 2 = . . . = a n a_{1}=a_{2}=...=a_{n} a1=a2=...=an b 1 = b 2 = . . . = b n b_{1}=b_{2}=...=b_{n} b1=b2=...=bn所需的最小操作次数是多少

解题思路

显然最终情况为 a 1 = a 2 = . . . = a n = m i n ( a 1 , a 2 , . . . , a n ) a_{1}=a_{2}=...=a_{n}=min(a_{1},a_{2},...,a_{n}) a1=a2=...=an=min(a1,a2,...,an) b 1 = b 2 = . . . = b n = m i n ( b 1 , b 2 , . . . , b n ) b_{1}=b_{2}=...=b_{n}=min(b_{1},b_{2},...,b_{n}) b1=b2=...=bn=min(b1,b2,...,bn)

考虑 ( a i , b i ) (a_{i},b_{i}) (ai,bi),两个数分别需要的操作次数为 a i − m i n a a_{i}-min_{a} aimina b i − m i n b b_{i}-min_{b} biminb,那么我们显然可以使用操作三将它们共同的部分一并减去,那么其所需的最少操作数为 m a x ( a i − m i n a , b i − m i n b ) max(a_{i}-min_{a},b_{i}-min_{b}) max(aimina,biminb)

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[110], b[110];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		ll Mina = LONG_MAX, Minb = LONG_MAX;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
			Mina = min(Mina, a[i]);
		}
		for (int i = 1; i <= n; ++i) {
			cin >> b[i];
			Minb = min(Minb, b[i]);
		}
		ll res = 0;
		for (int i = 1; i <= n; ++i) {
			res += max(a[i] - Mina, b[i] - Minb);
		}
		cout << res << '\n';
	}
	return 0;
}

C. Boats Competition

题目大意

有两个序列 a a a b b b,让你挑一些元组 ( a i , b i ) (a_{i},b_{i}) (ai,bi)使得 a 1 + b 1 = a 2 + b 2 = . . . = a k + b k a_{1}+b_{1}=a_{2}+b_{2}=...=a_{k}+b_{k} a1+b1=a2+b2=...=ak+bk,问你 k k k的最大值是多少

解题思路

由于 n n n只有 50 50 50,并且 1 ≤ a i , b i ≤ n 1\leq a_{i},b_{i} \leq n 1ai,bin,那么 a i + b i a_{i}+b_{i} ai+bi不会超过 100 100 100,我们暴力枚举 a i + b i a_{i}+b_{i} ai+bi的值,然后进行贪心匹配即可

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[110];
int solve(int x) {
	int res = 0;
	int l = 1, r = n;
	while (true) {
		if (l >= r) break;
		if (a[r] + a[l] > x) --r;
		else if (a[r] + a[l] < x) ++l;
		else {
			++res;
			++l;
			--r;
		}
	}
	return res;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		int nn = n * 2;
		int res = 0;
		for (int i = 1; i <= nn; ++i) {
			res = max(res, solve(i));
		}
		cout << res << '\n';
	}
	return 0;
}

D. Binary String To Subsequences

题目大意

给你一个由 0 0 0 1 1 1组成的字符串,让你将其分成很多个子序列,每个子序列满足没有任意两个相同字母相邻,问你最少需要多少个子序列才能将字符中的每个字符分完,并输出每个字符所属的子序列标号

解题思路

我们维护两个队列,分别存储当前以 0 0 0结尾和当前以 1 1 1结尾的子序列标号,若 s [ i ] = 0 s[i]=0 s[i]=0,那么如果 1 1 1队列不为空,我们将其归入队尾的子序列,并使其出队,否则令其成为新子序列的开头, s [ i ] = 1 s[i]=1 s[i]=1同理

AC代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		string s;
		cin >> s;
		vector<int> v[2];
		int tot = 0;
		for (int i = 0; i < n; ++i) {
			if (s[i] == '0') {
				if (v[1].empty()) a[i] = ++tot;
				else {
					a[i] = a[v[1].back()];
					v[1].pop_back();
				}
				v[0].push_back(i);
			}
			else {
				if (v[0].empty()) a[i] = ++tot;
				else {
					a[i] = a[v[0].back()];
					v[0].pop_back();
				}
				v[1].push_back(i);
			}
		}
		cout << tot << '\n';
		for (int i = 0; i < n; ++i) cout << a[i] << " ";
		cout << '\n';
	}
	return 0;
}
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页