# LOJ#2343. 「JOI 2016 Final」集邮比赛 2

## 题目地址

https://loj.ac/problem/2343

## 题解

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

#define ll long long
const int N = 100010;

int n;
char s[N];
ll f[N], cnt[3], sl[N][3], sr[N][3], sf[N];

int idx(char c) {
if(c == 'J') return 0;
if(c == 'O') return 1;
return 2;
}

int main() {
scanf("%d%s", &n ,s + 1);
for(int i = n; i; --i) {
if(s[i] == 'I') f[i] = 1;
else f[i] = cnt[idx(s[i]) + 1];
cnt[idx(s[i])] += f[i];
}
cnt[0] = cnt[1] = cnt[2] = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 3; ++j) sl[i][j] = sl[i - 1][j];
sl[i][idx(s[i])]++;
}
for(int i = n; i; --i) {
for(int j = 0; j < 3; ++j) sr[i][j] = sr[i + 1][j];
sr[i][idx(s[i])]++;
sf[i] = sf[i + 1];
if(s[i] == 'O') sf[i] += f[i];
}
ll A = 0, ans = 0;
for(int i = 1; i <= n; ++i) if(s[i] == 'J') A += f[i];
// J
for(int i = 0; i <= n; ++i) ans = max(ans, A + sf[i + 1]);
// O
for(int i = 0; i <= n; ++i) {
ans = max(ans, A + sl[i][0] * sr[i + 1][2]);
}
// I
ll tot = 0;
for(int i = 1; i <= n; ++i) {
if(s[i] != 'I') {
if(s[i] == 'O') tot += cnt[0];
cnt[idx(s[i])]++;
}
ans = max(ans, A + tot);
}
printf("%lld\n", ans);
}

posted @ 2019-08-30 01:33  henry_y  阅读(...)  评论(...编辑  收藏