Submission #960318

# Submission time Handle Problem Language Result Execution time Memory
960318 2024-04-10T09:00:13 Z tvladm2009 Sequence (APIO23_sequence) C++17
7 / 100
887 ms 65928 KB
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
 
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double; 
const string FILENAME = "input";
const int MAXN = 500228;
 
vector<int> pos[MAXN];
pair<int, int> tree[4 * MAXN];
int lazy[4 * MAXN];
 
pair<int, int> join(pair<int, int> a, pair<int, int> b) {
    return mp(min(a.first, b.first), max(a.second, b.second));
}
 
void push(int v, int tl, int tr) {
    if (tl < tr) {
        lazy[2 * v] += lazy[v];
        lazy[2 * v + 1] += lazy[v];
    }
    tree[v].first += lazy[v];
    tree[v].second += lazy[v];
    lazy[v] = 0;
}
 
void add(int v, int tl, int tr, int l, int r, int x) {
    push(v, tl, tr);
    if (l <= tl && tr <= r) {
        lazy[v] += x;
        push(v, tl, tr);
        return;
    }
    int tm = (tl + tr) / 2;
    if (l <= tm) {
        add(2 * v, tl, tm, l, min(tm, r), x);
    }
    if (tm + 1 <= r) {
        add(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);
    }
    push(2 * v, tl, tm);
    push(2 * v + 1, tm + 1, tr);
    tree[v] = join(tree[2 * v], tree[2 * v + 1]);
}
 
pair<int, int> get(int v, int tl, int tr, int l, int r) {
    push(v, tl, tr);
    if (l <= tl && tr <= r) {
        return tree[v];
    }
    int tm = (tl + tr) / 2;
    if (l <= tm && r <= tm) {
        return get(2 * v, tl, tm, l, r);
    } else if (tm + 1 <= l && tm + 1 <= r) {
        return get(2 * v + 1, tm + 1, tr, l, r);
    } else {
        return join(get(2 * v, tl, tm, l, tm), get(2 * v + 1, tm + 1, tr, tm + 1, r));
    }
}
 
int sequence(int n, vector<int> a) {
    for (int i = 0; i < n; i++) {
        a[i]--;
        pos[a[i]].pb(i);
    }
    for (int i = 0; i < n; i++) {
        add(1, 0, n - 1, i, n - 1, 1);
    }
    int ret = 1;
    vector<int> pref_min1(n), pref_min2(n), pref_max1(n), pref_max2(n), suff_min1(n), suff_min2(n), suff_max1(n), suff_max2(n);
    for (int val = 0; val < n; val++) {
        for (int i = 0; i < sz(pos[val]); i++) {
            pair<int, int> res = get(1, 0, n - 1, 0, pos[val][i]);
            pref_min1[i] = res.first;
            pref_max1[i] = res.second;
            res = get(1, 0, n - 1, pos[val][i], n - 1);
            suff_min1[i] = res.first;
            suff_max1[i] = res.second;
        }
        for (auto p: pos[val]) {
            add(1, 0, n - 1, p, n - 1, -2);
        }
        for (int i = 0; i < sz(pos[val]); i++) {
            pair<int, int> res = get(1, 0, n - 1, 0, pos[val][i]);
            pref_min2[i] = res.first;
            pref_max2[i] = res.second;
            res = get(1, 0, n - 1, pos[val][i], n - 1);
            suff_min2[i] = res.first;
            suff_max2[i] = res.second;
        }
        int l = 1, r = sz(pos[val]), sol = 0;
        while (l <= r) {
            int m = (l + r) / 2;
            bool ok = false;
            for (int i = 0; i + m - 1 < sz(pos[val]); i++) {
                if (pref_min1[i] <= suff_max1[i + m - 1] && suff_min1[i + m - 1] <= pref_max1[i]) {
                    ok = true;
                } else if (pref_min2[i] <= suff_max2[i + m - 1] && suff_min2[i + m - 1] <= pref_max2[i]) {
                    ok = true;
                } else if (pref_max1[i] <= suff_min1[i + m - 1] && suff_max2[i + m - 1] <= pref_min2[i]) {
                    ok = true;
                } else if (suff_max1[i] <= pref_min1[i + m - 1] && pref_max2[i + m - 1] <= suff_min2[i]) {
                    ok = true;
                }
            }
            if (ok == true) {
                l = m + 1;
                sol = m;
            } else {
                r = m - 1;
            }
        }
        chkmax(ret, sol);
    }
    return ret;
}
 
/**
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //read(FILENAME);
    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;
}
**/
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14940 KB Output is correct
2 Incorrect 3 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14940 KB Output is correct
2 Incorrect 3 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14940 KB Output is correct
2 Correct 759 ms 60304 KB Output is correct
3 Correct 742 ms 60292 KB Output is correct
4 Correct 706 ms 50176 KB Output is correct
5 Correct 753 ms 59476 KB Output is correct
6 Correct 745 ms 59264 KB Output is correct
7 Correct 706 ms 50768 KB Output is correct
8 Correct 712 ms 51024 KB Output is correct
9 Correct 708 ms 50180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 14936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 882 ms 65876 KB Output is correct
2 Correct 881 ms 65928 KB Output is correct
3 Correct 887 ms 65620 KB Output is correct
4 Correct 883 ms 65228 KB Output is correct
5 Incorrect 794 ms 62128 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14940 KB Output is correct
2 Incorrect 3 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14940 KB Output is correct
2 Incorrect 3 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -