Submission #981129

# Submission time Handle Problem Language Result Execution time Memory
981129 2024-05-12T21:28:47 Z Mher777 Sequence (APIO23_sequence) C++17
0 / 100
515 ms 26772 KB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
#include "sequence.h"
using namespace std;
mt19937 rnd(7069);
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;

const int N = 500005;
int a[N], pref[N], dif[N], tp[N * 4], tn[N * 4], par[N];
int n, ans = 1;
vector<pii> v;

void bld(int l = 0, int r = n, int pos = 1) {
    tn[pos] = tp[pos] = MAX;
    if (l == r) {
        par[l] = pos;
        return;
    }
    int mid = (l + r) / 2;
    bld(l, mid, 2 * pos);
    bld(mid + 1, r, 2 * pos + 1);
}

int qry(int l, int r, int type, int tl = 0, int tr = n, int pos = 1) {
    if (r < l) return MAX;
    if (l == tl && r == tr) {
        return (type == 1 ? tn[pos] : tp[pos]);
    }
    int mid = (tl + tr) / 2;
    if (mid >= r) {
        return qry(l, r, type, tl, mid, 2 * pos);
    }
    if (mid < l) {
        return qry(l, r, type, mid + 1, tr, 2 * pos + 1);
    }
    return min(qry(l, mid, type, tl, mid, 2 * pos), qry(mid + 1, r, type, mid + 1, tr, 2 * pos + 1));
}

void upd(int pos, int val, int type) {
    pos = par[pos];
    if (type == 1) tn[pos] = min(tn[pos], val);
    else tp[pos] = min(tp[pos], val);
    pos /= 2;
    while (pos) {
        if (type == 1) tn[pos] = min(tn[2 * pos], tn[2 * pos + 1]);
        else tp[pos] = min(tp[2 * pos], tp[2 * pos + 1]);
        pos /= 2;
    }
}

void func() {
    sort(all(v));
    bld();
    upd(0, 0, 1);
    for (auto elem : v) {
        int d = elem.ff, p = elem.ss, node = p - d;
        if (node <= 0) {
            node *= -1;
            upd(node, p, 1);
            ans = max(ans, p - qry(node, n, 1));
        }
        else {
            upd(node, p, 2);
            ans = max(ans, p - min(qry(0, n, 1), qry(0, node, 2)));
        }
    }
    bld();
    reverse(all(v));
    upd(0, 0, 1);
    for (auto elem : v) {
        int d = elem.ff, p = elem.ss, node = p - d;
        if (node <= 0) {
            node *= -1;
            upd(node, p, 1);
            ans = max(ans, p - qry(node, n, 1));
        }
        else {
            upd(node, p, 2);
            ans = max(ans, p - min(qry(0, n, 1), qry(0, node, 2)));
        }
    }
    v.clear();
}

void solv1() {
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        dif[i] = dif[i - 1];
        if (a[i] == 1) {
            ++pref[i];
        }
        else {
            ++dif[i];
        }
        v.pub({ dif[i], pref[i] });
    }
    func();
}

void solv2() {
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        dif[i] = dif[i - 1];
        if (a[i] == 2) {
            ++pref[i];
        }
        else if (a[i] == 1) {
            --dif[i];
        }
        else {
            ++dif[i];
        }
        v.pub({ dif[i], pref[i] });
    }
    func();
}

void solv3() {
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        dif[i] = dif[i - 1];
        if (a[i] == 3) {
            ++pref[i];
        }
        else {
            --dif[i];
        }
        v.pub({ dif[i], pref[i] });
    }
    func();
}

int sequence(int N, std::vector<int> A) {
    n = N;
    for (int i = 1; i <= n; ++i) {
        a[i] = A[i - 1];
    }
    solv1();
    solv2();
    solv3();
    return ans;
}

/*
8
1 1 2 3 3 2 1 1
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Incorrect 1 ms 6488 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Incorrect 1 ms 6488 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Incorrect 475 ms 26760 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 470 ms 26772 KB Output is correct
3 Incorrect 515 ms 26568 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 489 ms 26756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Incorrect 1 ms 6488 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Incorrect 1 ms 6488 KB Output isn't correct
5 Halted 0 ms 0 KB -