Official

C - Robot Takahashi Editorial by en_translator


Arrange the children and adults in a single line in the increasing order of their weights. (In other words, sort all the people by their weights.)
Then, Takahashi judges in a way that it separates the line between two people with different weights, or on the left of the leftmost person or on the right of the rightmost person, and regards those in the left group as children and those in the right group as adults. Note that Takahashi cannot make a partition between two people with the same weight because it cannot distinguish them.

Naively counting the number of people who are correctly judged for all patterns of separations costs a total time complexity of \(\Theta(N^2)\), which does not fit in the time limit under the Constraint of this problem (where \(N\) can be at most \(2\times 10^5\)).

Instead, first consider the state where the partition is on the left of the leftmost person, and updating its position to the right little by little.
Initially, Takahashi regards all people are adults, so the number of correct verdicts is equal to the number of adults.

Next, let us consider what will happen when the partition in the left of the \(i\)-th person from the left has been moved on his/her right. Here, the verdicts for those other than the \(i\)-th person do not change, so the number of correct verdicts for them do not change. The change occurs only for the \(i\)-th person, in a way that the “adult” verdict for that person changes to the “child” verdict. Thus, if the \(i\)-th person from the left is a child, then the number of correct verdict increases by \(1\); if the \(i\)-th person from the left is an adult, then the number of correct verdicts decreases by \(1\).

Repeat this procedure, and for every appropriate moment (where the partition is between two people with different weights, or at the leftmost or the rightmost position), count the number of correct verdicts, and finally find their maximum value.

Sorting by weights costs \(O(N\log N)\), counting the number of adults at first costs \(O(N)\), and each update on the shift of the partition costs \(O(1)\) each, for a total from the leftmost to the rightmost of \(O(N)\), so the total time complexity is \(O(N\log N)\), which is fast enough.

When sorting by weights, we can use std::pair in c++, or in python, we can sort the pairs of the value of weight and the flag of whether the person is a child or an adult by sorting two-dimensional array appropriately.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, x, ans;
	string s;
	vector<pair<int, char> >a;
	cin >> n;
	cin >> s;
	ans = 0;
	for (int i = 0; i < n; i++) {
		cin >> x;
		a.push_back({ x,s[i] });
		if (s[i] == '1')ans++;
	}
	sort(a.begin(), a.end());
	x = ans;
	for (int i = 0; i < n; i++) {
		if (a[i].second == '1')x--;
		else x++;
		if (i < (n - 1)) {
			if (a[i].first != a[i + 1].first)ans = max(ans, x);
		}
		else ans = max(ans, x);
	}
	cout << ans << endl;
	return 0;
}

posted:
last update: