题解:CF1988B Make Majority

题目大意

题面写得很清楚,我就不再赘述了。

解题思路

涉及知识点:字符串,构造。

由于所有相邻的 \(0\) 合并完会变成一个 \(0\),所以先贪心地把所有挨在一起的 \(0\) 合并起来,放在一个新的字符串里。

而且题目需要你判断是否最终是否能合并成一个 \(1\),所以 \(1\) 是不需要想 \(0\) 一样合并的,这样才能保证答案最优。

while (now < n) {
		if (s[now] == '1') t += '1', now++;
		else {
			t += '0';
			while (now < n && s[now] == '0') now++;
		}
	}

合并完成之后,我们得到了一个对于当前最优的序列,现在只需判断 \(0\)\(1\) 的大小关系即可。

代码

#include <bits/stdc++.h>
#define ull unsigned long long
#define int long long
#define ll __int128
#define ldb long double
#define db double
#define bl bool
#define endl '\n'
#define PII pair<int, int>
#define p_q priority_queue
#define n_m unordered_map
#define il inline
#define re register
#define ve vector
#define bs bitset
using namespace std;

//#define O2 1
#ifdef O2
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#endif

namespace OI {
	template <typename T>
	il T read() {
		T x = 0, f = 1;
		int ch = getchar();
		while (!isdigit(ch)) {
			if (ch == '-') f = -1;
			ch = getchar();
		}
		while (isdigit(ch)) {
			x = (x << 3) + (x << 1) + (ch ^ 48);
			ch = getchar();
		}
		return x * f;
	}
	template <typename TE>
	il void write(TE x) {
		if (x < 0) {
			x = -x;
			putchar('-');
		}
		TE sta[35];
		int top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while (x);
		while (top) putchar(sta[--top] + '0');
	}
	il string read_with_string() {
		string s = "";
		char ch = getchar();
		while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
			s += ch;
			ch = getchar();
		}
		return s;
	}
	il void write_with_string(string s) {
		for (int i = 0; i < s.size(); i++) putchar(s[i]);
	}
}
using namespace OI;

//#define fre 1
#define IOS 1
#define multitest 1

const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int inf = 1e12;

int n;
string s;
string t;
int c1, c2;

il void Init() {
	cin >> n >> s;
}

il void Solve() {
	int now = 0;
	t = "";
	while (now < n) {
		if (s[now] == '1') t += '1', now++;
		else {
			t += '0';
			while (now < n && s[now] == '0') now++;
		}
	}
	c1 = c2 = 0;
//	cout << t << endl;
	for (int i = 0; i < t.size(); i++) {
		c1 += t[i] == '0';
		c2 += t[i] == '1';
	}
	if (c1 >= c2) cout << "No\n";
	else cout << "Yes\n";
}

signed main() {
	int T;
#ifdef IOS
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
#ifdef multitest
	cin >> T;
#else
	T = 1;
#endif
	while (T--) {
		Init();
		Solve();
	}
	return 0;
}
/*

*/

记录

posted @ 2024-10-25 11:05  zla_2012  阅读(1)  评论(0编辑  收藏  举报