# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1002184 | son2008 | Difference (POI11_roz) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for(int i=a; i>=b; --i)
#define rep(i, n) for(int i=0; i<n; ++i)
#define red(i, n) for(int i=n-1; i>=0; --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define task "xaydung"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll LLinf = 1e18 + 27092008;
const int N = 1e6 + 5;
int n;
string s;
vector<int> pos[26];
int maxSub(vector<int> &arr){
int res = 0, Min = 0;
FOR(i,1,(int)arr.size()-1){
arr[i] += arr[i-1];
if (arr[i] != i) res = max(res, arr[i] - Min);
Min = min(Min, arr[i]);
}
return res;
}
void solve(){
cin >> n >> s;
rep(i, n)
pos[s[i] - 'a'].pb(i);
int ans = 0;
rep(i, 26)
FOR(j,i+1,25){
vector<int> arr(1, 0), tmp;
int l=0, r=0;
while(l < pos[i].size() && r < pos[j].size())
if (pos[i][l] < pos[j][r]) arr.pb(+1), l++;
else arr.pb(-1), r++;
while(l < pos[i].size()) arr.pb(+1), l++;
while(r < pos[j].size()) arr.pb(-1), r++;
tmp = arr;
for(int &v : tmp) v = -v;
ans = max({ans, maxSub(arr), maxSub(tmp)});
}
cout << ans;
}