답안 #871673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
871673 2023-11-11T09:04:24 Z vjudge1 Difference (POI11_roz) C++17
100 / 100
920 ms 25636 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

const int maxn = 1e6+55;
const int inf = 1e9+55;

typedef pair <int,int> pii;

int a[maxn], b[maxn];
int cnt[maxn], n;
int pref[maxn], cntPref[maxn];
vector <int> group[33];

int brute()
{
    int best = 0;
    for (int i = 1; i <= n; i ++)
    {
        multiset <int> st;
        for (int c = 0; c < 26; c ++) cnt[c] = 0;

        int mx = 0;
        for (int j = i; j <= n; j ++)
        {
            int c = a[j];
            if (st.find(cnt[c]) != st.end()) st.erase(st.find(cnt[c]));
            cnt[c] ++;
            st.insert(cnt[c]);

            int delta = abs(*(st.begin()) - *(--st.end()));
            best = max(best, delta);
        }
    }

    return best;
}

int cal(int c1, int c2)
{
    vector <int> rem;
    rem.push_back(0);
    int i = 0, j = 0;
    while (i < group[c1].size() && j < group[c2].size())
    {
        int u = group[c1][i], v = group[c2][j];
        if (group[c1][i] < group[c2][j])
        {
            rem.push_back(1);
            i ++;
        }
        else if (group[c1][i] > group[c2][j])
        {
            rem.push_back(-1);
            j ++;
        }
    }
    while (i < group[c1].size()) {
        rem.push_back(1);
        i ++;
    }
    while (j < group[c2].size()) {
        rem.push_back(-1);
        j ++;
    }


    int m = rem.size() - 1;
    for (int i = 1; i <= m; i ++) cntPref[i] = cntPref[i-1] + (rem[i] == -1);
    for (int i = 1; i <= m; i ++) pref[i] = pref[i-1] + rem[i];

    j = 0;
    int mn = inf, best = 0;
    for (int i = 1; i <= m; i ++)
    {
        while (j < i && cntPref[i] - cntPref[j] > 0)
        {
            mn = min(mn, pref[j]);
            j ++;
        }
        best = max(best, pref[i] - mn);
    }
    return best;
}

void process()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        char x;
        cin >> x;
        a[i] = x - 'a';
    }

    for (int i = 1; i <= n; i ++) group[a[i]].push_back(i);

    int best = 0;
    for (int c1 = 0; c1 < 26; c1 ++) for (int c2 = 0; c2 < 26; c2 ++) if (c1 != c2)
    {
        best = max(best, cal(c1, c2));
    }
//    cout << brute() << endl;
    cout << best;
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int ntest = 1;
    while (ntest --) process();

    return 0;
}

Compilation message

roz.cpp: In function 'int brute()':
roz.cpp:24:13: warning: unused variable 'mx' [-Wunused-variable]
   24 |         int mx = 0;
      |             ^~
roz.cpp: In function 'int cal(int, int)':
roz.cpp:45:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     while (i < group[c1].size() && j < group[c2].size())
      |            ~~^~~~~~~~~~~~~~~~~~
roz.cpp:45:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     while (i < group[c1].size() && j < group[c2].size())
      |                                    ~~^~~~~~~~~~~~~~~~~~
roz.cpp:47:13: warning: unused variable 'u' [-Wunused-variable]
   47 |         int u = group[c1][i], v = group[c2][j];
      |             ^
roz.cpp:47:31: warning: unused variable 'v' [-Wunused-variable]
   47 |         int u = group[c1][i], v = group[c2][j];
      |                               ^
roz.cpp:59:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     while (i < group[c1].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~
roz.cpp:63:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     while (j < group[c2].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 4956 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 9 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 918 ms 15352 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 622 ms 16048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 917 ms 15516 KB Output is correct
2 Correct 706 ms 13492 KB Output is correct
3 Correct 417 ms 16668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 920 ms 15512 KB Output is correct
2 Correct 460 ms 23660 KB Output is correct
3 Correct 447 ms 18272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 918 ms 15400 KB Output is correct
2 Correct 477 ms 25636 KB Output is correct
3 Correct 483 ms 17824 KB Output is correct