【BZOJ4236】JOIOJI【数学】【排序】【乱搞】

【题目链接】

%%%【lych_cys的题解】

/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 200005;

int n, si[maxn], sj[maxn], so[maxn];
char str[maxn];

struct _data {
	int x, y, w;

	bool operator < (const _data &u) const {
		return x != u.x ? x < u.x : (y != u.y ? y < u.y : w < u.w);
	}

	bool operator == (const _data &u) const {
		return x == u.x && y == u.y;
	}

} p[maxn];

int main() {
	scanf("%d%s", &n, str + 1);
	for(int i = 1; i <= n; i++) {
		si[i] = si[i - 1] + (str[i] == 'I');
		sj[i] = sj[i - 1] + (str[i] == 'J');
		so[i] = so[i - 1] + (str[i] == 'O');
	}

	for(int i = 0; i <= n; i++)
		p[i] = (_data){sj[i] - si[i], so[i] - si[i], i};

	int ans = 0, last = 0;
	sort(p, p + 1 + n);
	for(int i = 1; i <= n; i++)
		if(p[i - 1] == p[i]) ans = max(ans, p[i].w - p[last].w);
		else last = i;

	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(乱搞)