Submission #749127

# Submission time Handle Problem Language Result Execution time Memory
749127 2023-05-27T11:28:29 Z marvinthang Sequence (APIO23_sequence) C++17
13 / 100
445 ms 45908 KB
#include "sequence.h"
#include <bits/stdc++.h>
 
using namespace std;
 
#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif
 
template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }
template <class InIter, class OutIter>  void compress(InIter first, InIter last, OutIter result) { vector <__typeof(*first)> v(first, last); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); while (first != last) { *result = lower_bound(v.begin(), v.end(), *first) - v.begin(); ++first; ++result; } }

// end of template

const int MAX = 5e5 + 5;

int N, st[MAX << 2];

void update(int i, int l, int r, int p, int v) {
    if (r - l == 1) {
        st[i] = v;
        return;
    }
    int m = l + r >> 1;
    if (p < m) update(i << 1, l, m, p, v);
    else update(i << 1 | 1, m, r, p, v);
    st[i] = max(st[i << 1], st[i << 1 | 1]);
}

int findMax(int val) {
    int i = 1, l = 0, r = N + 1;
    if (st[1] < val) return -1;
    while (true) {
        int m = l + r >> 1;
        if (r - l == 1) return l;
        if (st[i << 1 | 1] >= val) i = i << 1 | 1, l = m;
        else i = i << 1, r = m;
    }  
    return -1;
}

int ma[MAX << 2], lazy[MAX << 2], mi[MAX << 2];

void pushDown(int i) {
    if (!lazy[i]) return;
    ma[i << 1] += lazy[i];
    ma[i << 1 | 1] += lazy[i];
    mi[i << 1] += lazy[i];
    mi[i << 1 | 1] += lazy[i];
    lazy[i << 1] += lazy[i];
    lazy[i << 1 | 1] += lazy[i];
    lazy[i] = 0;
}

void update(int i, int l, int r, int u, int v, int add) {
    if (l >= v || r <= u) return;
    if (u <= l && r <= v) {
        ma[i] += add;
        mi[i] += add;
        lazy[i] += add;
        return;
    }
    pushDown(i);
    int m = l + r >> 1;
    update(i << 1, l, m, u, v, add);
    update(i << 1 | 1, m, r, u, v, add);
    ma[i] = max(ma[i << 1], ma[i << 1 | 1]);
    mi[i] = min(mi[i << 1], mi[i << 1 | 1]);
}

int getMax(int i, int l, int r, int u, int v) {
    if (l >= v || r <= u) return -N - N - N;
    if (u <= l && r <= v) return ma[i];
    pushDown(i);
    int m = l + r >> 1;
    return max(getMax(i << 1, l, m, u, v), getMax(i << 1 | 1, m, r, u, v));
}

int getMin(int i, int l, int r, int u, int v) {
    if (l >= v || r <= u) return N + N + N;
    if (u <= l && r <= v) return mi[i];
    pushDown(i);
    int m = l + r >> 1;
    return min(getMin(i << 1, l, m, u, v), getMin(i << 1 | 1, m, r, u, v));
}

int sequence(int n, vector <int> A) {
    // sub5;
    N = n;
    vector <int> cnt(N + 1);
    REP(i, N) ++cnt[A[i]];
    if (*max_element(ALL(cnt)) <= 2) {
        vector <vector <int>> pos(N + 1);
        REP(i, N) pos[A[i]].push_back(i);
        REP(i, N) update(1, 0, N, i, N, 1);
        REP(i, N + 1) if (!pos[i].empty()) {
            if (pos[i].size() == 2) {
                int a = pos[i][0], b = pos[i][1];
                int pa = getMin(1, 0, N, a, a + 1) - 1;
                int pb = getMin(1, 0, N, b, b + 1) - 2;
                int x = pb - pa;
                if (abs(x) <= 2) return 2;
                if (x > 0) {
                    x -= 2;
                    int v = max(0, getMax(1, 0, N, 0, a));
                    v = pa - v;
                    if (x + v <= 0) return 2;
                    x += v;
                    v = getMin(1, 0, N, b, N);
                    v = v - pb - 2;
                    if (x + v <= 0) return 2;
                } else {
                    x += 2;
                    int v = min(0, getMin(1, 0, N, 0, a));
                    v = pa - v;
                    if (x + v >= 0) return 2;
                    x += v;
                    v = getMax(1, 0, N, b, N);
                    v = v - pb - 2;
                    if (x + v >= 0) return 2;
                }
            }

            for (int x: pos[i]) update(1, 0, N, x, N, -2);
        }
        return 1;
    }
    return 0;
}

#ifdef LOCAL
#include "sequence.h"

#include <cassert>
#include <cstdio>

#include <vector>

int main() {
    file("sequence");
  int N;
  assert(1 == scanf("%d", &N));

  std::vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    assert(1 == scanf("%d", &A[i]));
  }

  int result = sequence(N, A);
  printf("%d\n", result);
  return 0;
}
#endif

Compilation message

sequence.cpp: In function 'void update(int, int, int, int, int)':
sequence.cpp:53:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |     int m = l + r >> 1;
      |             ~~^~~
sequence.cpp: In function 'int findMax(int)':
sequence.cpp:63:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   63 |         int m = l + r >> 1;
      |                 ~~^~~
sequence.cpp: In function 'void update(int, int, int, int, int, int)':
sequence.cpp:93:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   93 |     int m = l + r >> 1;
      |             ~~^~~
sequence.cpp: In function 'int getMax(int, int, int, int, int)':
sequence.cpp:104:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  104 |     int m = l + r >> 1;
      |             ~~^~~
sequence.cpp: In function 'int getMin(int, int, int, int, int)':
sequence.cpp:112:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  112 |     int m = l + r >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 438 ms 45908 KB Output is correct
2 Correct 444 ms 45884 KB Output is correct
3 Correct 258 ms 45280 KB Output is correct
4 Correct 269 ms 45280 KB Output is correct
5 Correct 364 ms 42096 KB Output is correct
6 Correct 445 ms 41952 KB Output is correct
7 Correct 405 ms 40792 KB Output is correct
8 Correct 326 ms 40544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -