Submission #1002300

# Submission time Handle Problem Language Result Execution time Memory
1002300 2024-06-19T12:13:52 Z votranngocvy Difference (POI11_roz) C++14
100 / 100
627 ms 18260 KB
#include <bits/stdc++.h>
using namespace std;
 
#define NAME "task"
const int N = 1e6 + 5;
const int inf = 0x3f3f3f3f;
vector<int>pos[30];
int n,ans,Min[N];
char s[N];
 
void calc(int a,int b) {
    vector<int>vec;
    int i = 0,j = 0;
    while (i < (int)pos[a].size() && j < (int)pos[b].size()) {
        if (pos[a][i] < pos[b][j]) {
            vec.push_back(1);
            i++;
        }
        else {
            vec.push_back(-1);
            j++;
        }
    }
    while (i < (int)pos[a].size()) {
        vec.push_back(1);
        i++;
    }
    while (j < (int)pos[b].size()) {
        vec.push_back(-1);
        j++;
    }
    int m = (int)vec.size();
    Min[0] = 0;
    int sum = 0; int last = -1;
    for (int i = 1; i <= m; i++) {
        if (vec[i - 1] == -1) last = i - 1;
        sum += vec[i - 1];
        if (last != -1) ans = max(ans,sum - Min[last]);
        Min[i] = min(Min[i - 1],sum);
    }
}
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen(NAME".inp","r",stdin);
    //freopen(NAME".out","w",stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        pos[s[i] - 'a'].push_back(i);
    }
    for (int i = 0; i < 26; i++)
        for (int j = 0; j < 26; j++) 
            if (i != j && pos[i].size() && pos[j].size()) calc(i,j);
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 1116 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 8004 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 359 ms 6780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 624 ms 8312 KB Output is correct
2 Correct 484 ms 6592 KB Output is correct
3 Correct 161 ms 7796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 599 ms 8056 KB Output is correct
2 Correct 74 ms 15656 KB Output is correct
3 Correct 110 ms 9180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 627 ms 7980 KB Output is correct
2 Correct 42 ms 18260 KB Output is correct
3 Correct 204 ms 8796 KB Output is correct