题目翻译
给出一个 序列,每次可以选择一个区间 ,将其变为区间内出现次数更多的数。特别地,如果 和 的出现次数一样多,则改为 。你需要判断是否可以把原序列变为只有一个数 。
分析
显而易见地,对于一段连续的 串,我们为了不浪费 的使用次数,可以把这一长串都缩成一个 。 最后剩下来的一定是类似 的形式,这时可以得到当 的数量大于 的时候可行,反之,小于等于时不可行。 具体方法为遇到一个 就缩成一个 ,前后的 和 也一样。这样就可以得出上面的结论。 Talk is cheap.Let me show u the code.
#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;
}