답안 #951340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951340 2024-03-21T15:50:31 Z steveonalex 서열 (APIO23_sequence) C++17
40 / 100
172 ms 24304 KB
#include <bits/stdc++.h>
#include "sequence.h"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T& container, char separator = ' ', char finish = '\n'){
        for(auto item: container) cout << item << separator;
        cout << finish;
    }


template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

namespace Sub2{
    struct FenwickTree{
        int n;
        vector<int> a;

        FenwickTree(int _n){
            n = _n;
            a.resize(n+1);
        }

        void update(int i, int v){
            while(i <= n){
                a[i] += v;
                i += LASTBIT(i);
            }
        }

        int get(int i){
            int ans = 0;
            while(i > 0){
                ans += a[i];
                i -= LASTBIT(i);
            }
            return ans;
        }

        int binaryLift(int v){
            int idx = 0, p = MASK(logOf(n));
            while(p > 0){
                if (idx + p <= n && v - a[idx + p] > 0){
                    idx += p;
                    v -= a[idx];
                }
                p >>= 1;
            }
            return idx + 1;
        }
    };

    int solve(int n, vector<int> a){
        int ans = 0;
        for(int i = 0; i<n; ++i){
            FenwickTree bit(n);
            vector<int> cnt(n+1);
            for(int j = i; j<n; ++j){
                bit.update(a[j], 1);
                cnt[a[j]]++;
                int mid = (j - i) / 2 + 1;
                maximize(ans, cnt[bit.binaryLift(mid)]);
                if (mid * 2 == (j - i + 1)){
                    maximize(ans, cnt[bit.binaryLift(mid + 1)]);
                }
            }
        }

        return ans;
    }
}


namespace Sub3{
    const int INF = 1e9 + 69;
    struct FenwickTree{
        int n;
        vector<int> a;

        FenwickTree(int _n){
            n = _n;
            a.resize(n+1, INF);
        }

        void update(int i, int v){
            while(i <= n){
                minimize(a[i], v);
                i += LASTBIT(i);
            }
        }

        int get(int i){
            int ans = INF;
            while(i > 0){
                minimize(ans, a[i]);
                i -= LASTBIT(i);
            }
            return ans;
        }
    };

    bool check(vector<int> a){
        for(int i: a) if (i > 3) return 0;
        return 1;
    }

    int edging(int n, vector<int> a){
        int ans = 0;
        vector<array<int, 3>> pref(n+1, {{0, n+1, n+1}});
        a.insert(a.begin(), -1);

        for(int i = 1; i<=n; ++i){
            pref[i] = pref[i-1];
            if (a[i] == 1) pref[i][0]++;

            if (a[i] < 1) pref[i][1]--;
            else pref[i][1]++;
            if (a[i] <= 1) pref[i][2]++;
            else pref[i][2]--;
        }

        sort(ALL(pref), [] (array<int, 3> a, array<int, 3> b){
            if (a[2] != b[2]) return a[2] < b[2];
            return a[1] < b[1];
        });

        FenwickTree bit(n * 2 + 2);
        for(auto i: pref){
            maximize(ans, i[0] - bit.get(i[1] - 1));
            bit.update(i[1], i[0]);
        }

        return ans;
    }

    int solve(int n, vector<int> a){
        int ans = 0;
        for(int i = 1; i<=3; ++i){
            vector<int> b = a;
            for(int &j: b){
                if (j < i) j = 0;
                else if (j == i) j = 1;
                else j = 2;
            }
            maximize(ans, edging(n, b));
        }

        return ans;
    }
}

int sequence(int n, vector<int> a) {
    if (Sub3::check(a)) return Sub3::solve(n, a);
    if (n <= 5000) return Sub2::solve(n, a);
    return 0;
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     int n; cin >> n;
//     vector<int> a(n);
//     for(int i = 0; i<n; ++i) cin >> a[i];

//     cout << sequence(n, a) << "\n";

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 122 ms 348 KB Output is correct
14 Correct 130 ms 452 KB Output is correct
15 Correct 107 ms 348 KB Output is correct
16 Correct 119 ms 448 KB Output is correct
17 Correct 111 ms 460 KB Output is correct
18 Correct 96 ms 348 KB Output is correct
19 Correct 108 ms 596 KB Output is correct
20 Correct 104 ms 452 KB Output is correct
21 Correct 102 ms 348 KB Output is correct
22 Correct 100 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 36 ms 6236 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 154 ms 23468 KB Output is correct
3 Correct 172 ms 24304 KB Output is correct
4 Correct 155 ms 23892 KB Output is correct
5 Correct 160 ms 23824 KB Output is correct
6 Correct 131 ms 23216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 6236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 122 ms 348 KB Output is correct
14 Correct 130 ms 452 KB Output is correct
15 Correct 107 ms 348 KB Output is correct
16 Correct 119 ms 448 KB Output is correct
17 Correct 111 ms 460 KB Output is correct
18 Correct 96 ms 348 KB Output is correct
19 Correct 108 ms 596 KB Output is correct
20 Correct 104 ms 452 KB Output is correct
21 Correct 102 ms 348 KB Output is correct
22 Correct 100 ms 344 KB Output is correct
23 Incorrect 9 ms 1368 KB Output isn't correct
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 122 ms 348 KB Output is correct
14 Correct 130 ms 452 KB Output is correct
15 Correct 107 ms 348 KB Output is correct
16 Correct 119 ms 448 KB Output is correct
17 Correct 111 ms 460 KB Output is correct
18 Correct 96 ms 348 KB Output is correct
19 Correct 108 ms 596 KB Output is correct
20 Correct 104 ms 452 KB Output is correct
21 Correct 102 ms 348 KB Output is correct
22 Correct 100 ms 344 KB Output is correct
23 Incorrect 36 ms 6236 KB Output isn't correct
24 Halted 0 ms 0 KB -