Submission #918760

#TimeUsernameProblemLanguageResultExecution timeMemory
918760myst6Election (BOI18_election)C++14
28 / 100
3026 ms1876 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
	int N;
	cin >> N;
	string s;
	cin >> s;
	int Q;
	cin >> Q;
	while (Q--) {
		int L, R;
		cin >> L >> R;
		int k = R-L+1;
		string t = s.substr(L-1, k);
		vector<int> forward(k);
		for (int i=0; i<k; i++) {
			forward[i] = (i == 0 ? 0 : forward[i-1]) + (t[i] == 'C' ? 1 : -1);
		}
		map<int,int> first;
		for (int i=k-1; i>=0; i--) {
			first[forward[i]] = i;
		}
		int ans = 0;
		for (int i=-1;; i--) {
			if (first.count(i) == 0) break;
			int idx = first[i];
			t[idx] = 'x';
			ans += 1;
		}
		// cout << t << "\n";
		vector<int> backward(k);
		for (int i=k-1; i>=0; i--) {
			backward[i] = (i == k-1 ? 0 : backward[i+1]) + (t[i] == 'C' ? 1 : t[i] == 'T' ? -1 : 0);
		}
		int hi = 0;
		for (int i=0; i<k; i++) {
			hi = max(hi, -backward[i]);
		}
		cout << ans+hi << "\n";
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...