2018年3月6日 星期二

(POI) XVIII OI Round II - day 1 Task Difference

https://szkopul.edu.pl/problemset/problem/xmyBMI5AsEiW30_RyePNSXiG/site/?key=statement

a nice problem!

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 1000006;
const int S = 31;

char c[N];

int idx(char c)
{
    return c-'a';
}

int cnt_i[S][S];
int cnt_j[S][S];
bool is_first_j[S][S];

int main ()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    cin >> c;
    int ans=0;
    for (int x=0;n>x;x++)
    {
        int now_c = idx(c[x]);
        //now_c as i
        int i,j;
        i = now_c;
        for (int j=0;S>j;j++)
        {
            if (i==j) continue;
            cnt_i[i][j]++;
            if (cnt_j[i][j])
            {
                ans = max(ans, cnt_i[i][j] - cnt_j[i][j] + (is_first_j[i][j] && cnt_j[i][j] != 1) );
            }
        }
        j = now_c;
        for (int i=0;S>i;i++)
        {
            if (i==j) continue;
            cnt_j[i][j]++;
            if (cnt_j[i][j])
            {
                ans = max(ans, cnt_i[i][j] - cnt_j[i][j] + (is_first_j[i][j] && cnt_j[i][j] != 1) );
            }
            if ( cnt_i[i][j] - cnt_j[i][j] + (is_first_j[i][j] && cnt_j[i][j] != 1) < 0 )
            {
                cnt_i[i][j] = 0;
                cnt_j[i][j] = 1;
                is_first_j[i][j] = true;
            }
        }
    }
    cout << ans << endl;
}

1 則留言: