Submission #980750

# Submission time Handle Problem Language Result Execution time Memory
980750 2024-05-12T11:14:43 Z davitmarg Sequence (APIO23_sequence) C++17
13 / 100
2000 ms 48972 KB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#include <fstream>
#define fastIO                   \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 500005;

int n, a[N], ans;
vector<int> pos[N];
pair<int, int> t[N * 4];
int d[N * 4];

pair<int, int> merge(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 l, int r)
{
    if (d[v] == 0)
        return;
    if (l != r)
    {
        d[v * 2] += d[v];
        d[v * 2 + 1] += d[v];
    }
    t[v] = MP(t[v].first + d[v], t[v].second + d[v]);
    d[v] = 0;
}

void add(int v, int l, int r, int i, int j, int val)
{
    push(v, l, r);
    if (i > j)
        return;
    if (i == l && j == r)
    {
        d[v] += val;
        push(v, l, r);
        return;
    }

    int m = (l + r) / 2;
    add(v * 2, l, m, i, min(m, j), val);
    add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
    t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
    push(v, l, r);
    if (i > j)
        return MP(mod, -mod);
    if (i == l && j == r)
        return t[v];

    int m = (l + r) / 2;
    return merge(
        get(v * 2, l, m, i, min(m, j)),
        get(v * 2 + 1, m + 1, r, max(m + 1, i), j)
    );
}




int sequence(int nn, vector<int> aa)
{
    n = nn;
    for (int i = 1; i <= n; i++)
        a[i] = aa[i - 1];


    for (int i = 1; i <= n; i++)
        pos[a[i]].push_back(i);

    for (int i = 1; i <= n; i++)
        add(1, 0, n, i, n, 1);


    for (int x = 1; x <= n; x++)
    {
        for (int p = 0; p < pos[x].size(); p++)
        {
            int i = pos[x][p];
            add(1, 0, n, i, n, -1);
        }


        for (int p = 0; p < pos[x].size(); p++)
        {
            int i = pos[x][p];

            int l = 0;
            int r = p;

            while (l <= r)
            {
                int m = (l + r) / 2;

                int j = pos[x][m];
                int len = p - m + 1;

                pair<int, int> dr = get(1, 0, n, i, n);
                pair<int, int> dl = get(1, 0, n, 0, j - 1);
                pair<int, int> d = MP(dr.first - dl.second, dr.second - dl.first);


                if ((d.second < 0 && len >= -d.second) || (d.first > 0 && len >= d.first) || (d.first <= 0 && d.second >= 0))
                {
                    ans = max(ans, len);
                    r = m - 1;
                }
                else
                    l = m + 1;
            }
        }

        for (int p = 0; p < pos[x].size(); p++)
        {
            int i = pos[x][p];
            add(1, 0, n, i, n, -1);
        }
    }


    return ans;
}

#ifdef death



void solve()
{
    int nn;
    vector<int> aa;
    cin >> nn;
    for (int i = 0; i < nn; i++)
    {
        int x;
        cin >> x;
        aa.push_back(x);
    }

    cout << sequence(nn, aa) << endl;
}

int main()
{
    fastIO;
    int T = 1;
    //cin >> T;
    while (T--)
        solve();
    return 0;
}
#endif // death

/*

9
1  1  2  3  4  3  2  1  1


14
2 6 2 5 3 4 2 1 4 3 5 6 3 2

*/

Compilation message

sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:109:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |         for (int p = 0; p < pos[x].size(); p++)
      |                         ~~^~~~~~~~~~~~~~~
sequence.cpp:116:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |         for (int p = 0; p < pos[x].size(); p++)
      |                         ~~^~~~~~~~~~~~~~~
sequence.cpp:145:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |         for (int p = 0; p < pos[x].size(); p++)
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 4 ms 16832 KB Output is correct
4 Correct 3 ms 16744 KB Output is correct
5 Incorrect 3 ms 16836 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 4 ms 16832 KB Output is correct
4 Correct 3 ms 16744 KB Output is correct
5 Incorrect 3 ms 16836 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 681 ms 43248 KB Output is correct
3 Correct 676 ms 43136 KB Output is correct
4 Execution timed out 2081 ms 35212 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16980 KB Output is correct
2 Execution timed out 2057 ms 35372 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 788 ms 48972 KB Output is correct
2 Correct 838 ms 48924 KB Output is correct
3 Correct 818 ms 48392 KB Output is correct
4 Correct 806 ms 48400 KB Output is correct
5 Correct 804 ms 45064 KB Output is correct
6 Correct 811 ms 45060 KB Output is correct
7 Correct 722 ms 43852 KB Output is correct
8 Correct 672 ms 43588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 4 ms 16832 KB Output is correct
4 Correct 3 ms 16744 KB Output is correct
5 Incorrect 3 ms 16836 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 4 ms 16832 KB Output is correct
4 Correct 3 ms 16744 KB Output is correct
5 Incorrect 3 ms 16836 KB Output isn't correct
6 Halted 0 ms 0 KB -