Submission #956986

# Submission time Handle Problem Language Result Execution time Memory
956986 2024-04-02T19:06:12 Z Mher777 Sequence (APIO23_sequence) C++17
0 / 100
209 ms 19940 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];
vi g[5];
int n, m, ans = 1;

int sequence(int N, std::vector<int> A) {
    n = N;
    g[1] = { 0 };
    g[2] = { 0 };
    g[3] = { 0 };
    for (int i = 1; i <= n; ++i) {
        a[i] = A[i - 1];
        g[a[i]].pub(i);
        pref[i] = pref[i - 1];
        if (a[i] > 1) ++pref[i];
        else --pref[i];
    }
    mii mp;
    m = (int)g[1].size() - 1;
    vector<pii> v;
    for (int i = 1; i <= m; ++i) {
        mp[pref[g[1][i]]] = i;
    }
    int index = 0;
    for (auto& elem : mp) {
        index = max(index, elem.ss);
        v.pub({ elem.ff,index });
    }
    for (int i = 1; i <= m; ++i) {
        int val = pref[g[1][i] - 1], ind = g[1][i];
        index = lower_bound(all(v), mpr(val + 1, 0)) - v.begin() - 1;
        if (index == -1) continue;
        ans = max(ans, v[index].ss - i + 1);
    }
    swap(g[1], g[3]);
    m = (int)g[1].size() - 1;
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        if (a[i] < 3) ++pref[i];
        else --pref[i];
    }
    v.clear();
    mp.clear();
    for (int i = 1; i <= m; ++i) {
        mp[pref[g[1][i]]] = i;
    }
    index = 0;
    for (auto& elem : mp) {
        index = max(index, elem.ss);
        v.pub({ elem.ff,index });
    }
    for (int i = 1; i <= m; ++i) {
        int val = pref[g[1][i] - 1], ind = g[1][i];
        index = lower_bound(all(v), mpr(val + 1, 0)) - v.begin() - 1;
        if (index == -1) continue;
        ans = max(ans, v[index].ss - i + 1);
    }
    m = (int)g[2].size() - 1;
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        if (a[i] == 1) --pref[i];
        else if (a[i] == 3) ++pref[i];
    }
    mp.clear();
    for (int i = 1; i <= m; i += 2) {
        mp[pref[g[2][i]]] = i;
    }
    for (int i = 1; i <= m; ++i) {
        int val = pref[g[2][i] - 1], ind = i;
        if (mp[val - 1]) ind = max(ind, mp[val - 1]);
        if (mp[val]) ind = max(ind, mp[val]);
        if (mp[val + 1]) ind = max(ind, mp[val + 1]);
        if (!(i % 2) && mp[val - 2]) ind = max(ind, mp[val - 2]);
        if (!(i % 2) && mp[val + 2]) ind = max(ind, mp[val + 2]);
        ans = max(ans, ind - i + 1);
    }
    mp.clear();
    for (int i = 2; i <= m; i += 2) {
        mp[pref[g[2][i]]] = i;
    }
    for (int i = 1; i <= m; ++i) {
        int val = pref[g[2][i] - 1], ind = i;
        if (mp[val - 1]) ind = max(ind, mp[val - 1]);
        if (mp[val]) ind = max(ind, mp[val]);
        if (mp[val + 1]) ind = max(ind, mp[val + 1]);
        if (i % 2 && mp[val - 2]) ind = max(ind, mp[val - 2]);
        if (i % 2 && mp[val + 2]) ind = max(ind, mp[val + 2]);
        ans = max(ans, ind - i + 1);
    }
    return ans;
}

/*
7
3 2 1 3 2 3 1
*/

Compilation message

sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:82:38: warning: unused variable 'ind' [-Wunused-variable]
   82 |         int val = pref[g[1][i] - 1], ind = g[1][i];
      |                                      ^~~
sequence.cpp:105:38: warning: unused variable 'ind' [-Wunused-variable]
  105 |         int val = pref[g[1][i] - 1], ind = g[1][i];
      |                                      ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Runtime error 2 ms 4700 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Runtime error 2 ms 4700 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Runtime error 44 ms 13164 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 209 ms 19940 KB Output is correct
3 Incorrect 143 ms 15548 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 41 ms 12636 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Runtime error 2 ms 4700 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Runtime error 2 ms 4700 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -