Submission #941375

# Submission time Handle Problem Language Result Execution time Memory
941375 2024-03-08T16:18:31 Z LOLOLO Sequence (APIO23_sequence) C++17
12 / 100
2000 ms 54020 KB
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 5e5 + 100;
const int M = 2e6;

struct FT{
    vector <int> f;
    int N;
    FT(int n) {
        N = n;
        f.assign(n + 1, 0);
    }

    void upd(int i, int x) {
        for (; i <= N; i += i & (-i))
            f[i] += x;
    }

    int get(int i) {
        int s = 0;
        for (; i; i -= i & (-i))
            s += f[i];

        return s;
    }
};

struct seg{
    int N;
    vector <int> st;
    seg(int n) {
        N = n;
        st.assign(n * 4 + 1000, 1e9);
    }

    void upd(int id, int l, int r, int p, int v, int is) {
        if (l > p || r < p)
            return;

        if (l == r) {
            if (is) {
                st[id] = v;
            } else {
                st[id] = min(st[id], v);
            }
            return;
        }

        int m = (l + r) / 2;
        upd(id * 2, l, m, p, v, is);
        upd(id * 2 + 1, m + 1, r, p, v, is);
        st[id] = min(st[id * 2], st[id * 2 + 1]);
    }

    int get(int id, int l, int r, int u, int v) {
        if (l > v || r < u)
            return 1e9;

        if (l >= u && r <= v)
            return st[id];

        int m = (l + r) / 2;
        return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
    }
};
 
vector <int> pos[N];
int a[N], n;

void maximize(int &a, int b) {
    if (a < b) {
        a = b;
    }
}

int get(int add) {
    seg seg(n * 2);
    FT P(n + 100);
    for (int i = 1; i <= n; i++) {
        P.upd(i, add);
    }

    int ans = 0, t = 0;

    for (int i = 1; i <= n; i++) {
        for (auto x : pos[i]) {
            P.upd(x, -add);
        }

        vector <pair <int, int>> st;
        for (int j = 0; j < sz(pos[i]); j++) {
            if ((j && pos[i][j] - 1 != pos[i][j - 1]) || (j == 0))
                st.pb({j, P.get(pos[i][j] - 1)});

            st.pb({j + 1, P.get(pos[i][j])});

            if ((j < sz(pos[i]) - 1 && pos[i][j] + 1 != pos[i][j + 1]) || (j == sz(pos[i]) - 1))
                st.pb({j + 1, P.get(pos[i][j] + 1)});                
        }

        st.pb({0, 0});
        st.pb({sz(pos[i]), P.get(n)});
        uniquev(st);

        sort(all(st), [&] (pair <int, int> &A, pair <int, int> &B) {return A.s < B.s;} );

        for (auto x : st) {
            maximize(ans, x.f - seg.get(1, 0, n * 2, x.s - x.f + n, n * 2));
            seg.upd(1, 0, n * 2, x.s - x.f + n, x.f, 0);
        }

        for (auto x : st) {
            seg.upd(1, 0, n * 2, x.s - x.f + n, 1e9, 1);
        }

        for (auto x : pos[i]) {
            P.upd(x, -add);
        }
    }

    return ans;
}

int sequence(int N, vector <int> A) {
    n = N;
    for (int i = 1; i <= n; i++)
        pos[i].clear();

    for (int i = 1; i <= n; i++) {
        a[i] = A[i - 1];
        pos[a[i]].pb(i);
    }

    int ans = max(get(-1), get(1));

    return ans;
}
 
/*int main() {
    cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2}) << '\n';
    cout << sequence(9, {1, 1, 2, 3, 4, 3, 2, 1, 1}) << '\n';
    cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2}) << '\n';
    cout << sequence(3, {3, 3, 3}) << '\n';

    return 0;
}*/

// s[i] - s[j] <= p[i] - p[j]
// s[i] - p[i] <= s[j] - p[j]
// s[i] - i >= s[j] - j

Compilation message

sequence.cpp: In function 'int get(int)':
sequence.cpp:103:18: warning: unused variable 't' [-Wunused-variable]
  103 |     int ans = 0, t = 0;
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13916 KB Output is correct
2 Correct 3 ms 13912 KB Output is correct
3 Correct 3 ms 13912 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Incorrect 3 ms 13916 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13916 KB Output is correct
2 Correct 3 ms 13912 KB Output is correct
3 Correct 3 ms 13912 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Incorrect 3 ms 13916 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13916 KB Output is correct
2 Correct 1520 ms 45592 KB Output is correct
3 Incorrect 1627 ms 45588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 1226 ms 51956 KB Output is correct
3 Correct 1343 ms 45596 KB Output is correct
4 Correct 1327 ms 46708 KB Output is correct
5 Correct 1205 ms 52260 KB Output is correct
6 Correct 1189 ms 54020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2063 ms 51540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13916 KB Output is correct
2 Correct 3 ms 13912 KB Output is correct
3 Correct 3 ms 13912 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Incorrect 3 ms 13916 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13916 KB Output is correct
2 Correct 3 ms 13912 KB Output is correct
3 Correct 3 ms 13912 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Incorrect 3 ms 13916 KB Output isn't correct
6 Halted 0 ms 0 KB -