ZZULIOJ 2626 H Small games

Topic Link

Bloggers too lazy, do not want to write a description of the subject

Ideas:

First, regardless of the final result can be found and the sequence of operations, it is possible to operate from the beginning, it is operated once encountered 1, updates the value behind, but if the string behind each update time consume too much, it is possible to record each with an array digital position number is operated, the operation corresponding to the even number of times is not operated, based on the operation of the current digital number and the original digital

So the problem ~ converted into how the number of operations recorded

The method of the differential section may be used, for each position, if it is 1, the latter figure must be reversed, i.e. the number of digits behind the add operation 1, the corresponding cnt [i] ++, each cycle a new when the line is calculated and the position of the prefix (i.e., the value of the current position), because the first one may not guarantee before the first one, it is possible while circulating prefix and determining the difference array, each operation time complexity linear

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
const int N = 100010;
char s[N];
int cnt[N], sum[N];
int main()
{
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	while (n--){
		memset(cnt, 0, sizeof cnt);
		memset(sum, 0, sizeof sum);
		cin >> s;
		int len = strlen(s);
		int res = 0;
		if(s[0] == '1'){
			res++;
			cnt[1]++;
			cnt[len]--;
		}
		for (int i = 1; i < len; i++){
			sum[i] = sum[i - 1] + cnt[i];
			if(s[i] == '1' && sum[i] % 2 == 0){
				res++;
				cnt[i + 1]++;
				cnt[len]--;
			}
			else if(s[i] == '0' && sum[i] % 2 == 1){
				res++;
				cnt[i + 1]++;
				cnt[len]--;
			}
		}
		if(res & 1)cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8198

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/103674291