Submission #639565

# Submission time Handle Problem Language Result Execution time Memory
639565 2022-09-10T16:07:57 Z valerikk Monochrome Points (JOI20_monochrome) C++17
0 / 100
1 ms 468 KB
#include <bits/stdc++.h>

using namespace std;

struct Fenwick {
	int sz;
	vector<int> f;

	void add(int i, int val) {
		for (++i; i <= sz; i += i & -i) {
			f[i] += val;
		}
	}

	int get(int i) {
		int res = 0;
		for (; i > 0; i -= i & -i) {
			res += f[i];
		}
		return res;
	}

	Fenwick(int sz_) : sz(sz_) {
		f.resize(sz + 1, 0);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	string s;
	cin >> n >> s;
	vector<int> black, white;
	for (int i = 0; i < 2 * n; ++i) {
		if (s[i] == 'W') {
			white.push_back(i);
		} else {
			black.push_back(i);
		}
	}
	sort(black.begin(), black.end());
	sort(white.begin(), white.end());
	auto get = [&](int k) {
		k %= n;
		vector<vector<int>> start(2 * n + 1), finish(2 * n + 1);
		Fenwick fenw(2 * n);
		vector<pair<int, int>> segments;
		for (int i = 0; i < n; ++i) {
			int x = black[i], y = white[(i + k) % n];
			segments.push_back({min(x, y), max(x, y)});
		}
		for (int i = 0; i < n; ++i) {
			start[segments[i].first + 1].push_back(i);
			finish[segments[i].second].push_back(i);
		}
		long long res = 0;
		for (int i = 0; i < 2 * n; ++i) {
			for (int j : start[i]) {
				fenw.add(segments[j].first, 1);
			}
			for (int j : finish[i]) {
				fenw.add(segments[j].first, -1);
			}
			for (int j : finish[i]) {
				res += fenw.get(2 * n) - fenw.get(segments[j].first + 1);
			}
		}
		return res;
	};
	if (n == 1) {
		cout << get(0) << endl;
		return 0;
	}
	if (get(0) < get(1)) {
		reverse(white.begin(), white.end());
		reverse(black.begin(), black.end());
	}
	assert(get(0) > get(1));
	long long g0 = get(0);
	int l = 0, r = n;
	while (r - l > 1) {
		int m = (l + r) / 2;
		if (get(m) < g0) {
			l = m;
		} else {
			r = m;
		}
	}
	r = n;
	while (r - l > 5) {
		int m = (l + r) / 2;
		if (get(m) < get(m + 1)) {
			l = m;
		} else {
			r = m + 1;
		}
	}
	long long ans = 0;
	for (int k = l; k <= r; ++k) {
		ans = max(ans, get(k));
	}
	cout << ans << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -