跳到正文

题解

题解:CF1988B Make Majority

LuoFeng_Nanami约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月16日
篇幅
约 2 分钟
获赞
2
题目
CF1988B

版本

  1. v1当前

题目翻译

给出一个 0101 序列,每次可以选择一个区间 [l,r][l,r],将其变为区间内出现次数更多的数。特别地,如果 00 和 11 的出现次数一样多,则改为 00。你需要判断是否可以把原序列变为只有一个数 11。

分析

显而易见地,对于一段连续的 00 串,我们为了不浪费 11 的使用次数,可以把这一长串都缩成一个 00。 最后剩下来的一定是类似 101101010111…101101010111\dots 的形式,这时可以得到当 11 的数量大于 00 的时候可行,反之,小于等于时不可行。 具体方法为遇到一个 101101 就缩成一个 11,前后的 011011 和 110110 也一样。这样就可以得出上面的结论。 Talk is cheap.Let me show u the code.

C++
#include<bits/stdc++.h>
#define ll long long
#define rll register ll
#define F(i, a, b) for(rll i = a; i <= b; i++)
#define Fdn(i, a, b) for(rll i = a; i >= b; i--)
#define int ll
#define pii pair<int, int>

using namespace std;

const int inf = 0x3f3f3f3f3f3f3f3f, mod = 1e9 + 7;
const int maxn = 2e5 + 7;

int _;
int n;
int num[maxn], tot;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> _;
	while(_--) {
		tot = 0;
		cin >> n;
		char nm; cin >> nm; int lst = nm - '0';
		F(i, 2, n) {
			cin >> nm;
			if(nm == '1') num[++tot] = lst;
			else if(lst == 1) num[++tot] = lst;
			lst = nm - '0';
		}
		num[++tot] = lst;
		int cnt0 = 0, cnt1 = 0;
		F(i, 1, tot) {
			if(num[i] == 0) cnt0++;
			else cnt1++;
		}
		if(cnt1 > cnt0) cout << "Yes\n";
		else cout << "No\n";
	}

	return 0;
}
原文