제출 #974384

#제출 시각아이디문제언어결과실행 시간메모리
974384isirazeev서열 (APIO23_sequence)C++17
11 / 100
2062 ms27800 KiB
#include <bits/stdc++.h>

using namespace std;

int sequence(int n, vector<int> a) {
    int l_max[n], l_min[n], r_max[n], r_min[n];
    for (int i = 0; i < n; i++) {
        l_max[i] = l_min[i] = r_max[i] = r_min[i] = i;
        int c1 = 0, c2 = 0;
        int Lmax = 0, Lmin = 0, Rmax = 0, Rmin = 0;
        for (int j = i - 1; j >= 0; j--) {
            if (a[j] == a[i]) break;
            c1 += (a[j] < a[i]), c2 += (a[j] > a[i]);
            if (c1 - c2 > Lmax) Lmax = c1 - c2, l_max[i] = j;
            if (c1 - c2 < Lmin) Lmin = c1 - c2, l_min[i] = j;
        }
        int c3 = 0, c4 = 0;
        for (int j = i + 1; j < n; j++) {
            if (a[j] == a[i]) break;
            c3 += (a[j] < a[i]), c4 += (a[j] > a[i]);
            if (c3 - c4 > Rmax) Rmax = c3 - c4, r_max[i] = j;
            if (c3 - c4 < Rmin) Rmin = c3 - c4, r_min[i] = j;
        }
    }
    vector<int> pos[n + 1];
    for (int i = 0; i < n; i++) pos[a[i]].emplace_back(i);
    int res = 0;
    for (int val = 1; val <= n; val++) {
        for (int L = 0; L < n; L++) {
            int c1 = 0, c2 = 0;
            for (int R = L; R < n; R++) {
                c1 += (a[R] < val), c2 += (a[R] > val);
                if (abs(c1 - c2) <= (R - L + 1) - (c1 + c2) && (R - L + 1) > c1 + c2) {
                    res = max(res, R - L + 1 - c1 - c2);
                }
            }
        }
    }
    for (int m = n; m >= 1; m--) {
        for (int val = 1; val <= n; val++) {
            for (int ind = 0; ind + m - 1 < (int) pos[val].size(); ind++) {
                int LL = pos[val][ind], RR = pos[val][ind + m - 1], L, R;
                for (int t1 = 0; t1 < 3; t1++) {
                    if (t1 == 0) L = LL;
                    else if (t1 == 1) L = l_max[LL];
                    else L = l_min[LL];
                    for (int t2 = 0; t2 < 3; t2++) {
                        if (t2 == 0) R = RR;
                        else if (t2 == 1) R = r_max[RR];
                        else R = r_min[RR];
                        int c1 = 0, c2 = 0;
                        for (int i = L; i <= R; i++) c1 += (a[i] < val), c2 += (a[i] > val);
                        if (abs(c1 - c2) <= m) {
                            if(m > res) exit(-1);
                            return m;
                        }
                    }
                }
            }
        }
    }
}

//int main() {
//    ios_base::sync_with_stdio(false);
//    cin.tie(nullptr), cout.tie(nullptr);
//    int N;
//    cin >> N;
//    vector<int> A(N);
//    for (int i = 0; i < N; i++) cin >> A[i];
//    cout << sequence(N, A);
//    return 0;
//}

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:62:1: warning: control reaches end of non-void function [-Wreturn-type]
   62 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...