Submission #464950

# Submission time Handle Problem Language Result Execution time Memory
464950 2021-08-14T14:34:42 Z SamAnd Exam (eJOI20_exam) C++17
100 / 100
203 ms 98492 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005, N0 = 5003;
int M = 25000007;

bool isprime(int x)
{
    if (x <= 1)
        return false;
    for (int i = 2; i * i <= x; ++i)
    {
        if (x % i == 0)
            return false;
    }
    return true;
}

int n;
int a[N], b[N];

int ul[N], ur[N];
void pre()
{
    stack<int> s;
    for (int i = 1; i <= n; ++i)
    {
        while (!s.empty() && a[s.top()] <= a[i])
            s.pop();
        if (s.empty())
            ul[i] = 0;
        else
            ul[i] = s.top();
        s.push(i);
    }
    while (!s.empty())
        s.pop();
    for (int i = n; i >= 1; --i)
    {
        while (!s.empty() && a[s.top()] <= a[i])
            s.pop();
        if (s.empty())
            ur[i] = n + 1;
        else
            ur[i] = s.top();
        s.push(i);
    }
}

int dp[N0][N0];
int solv6()
{
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (a[i] == b[j] && ul[i] < j && j < ur[i])
            {
                dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + 1);
            }
            dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        }
    }
    for (int i = 1; i <= n + 1; ++i)
        for (int j = 1; j <= n + 1; ++j)
            ans = max(ans, dp[i][j]);
    return ans;
}

int p[N];
int solv2()
{
    for (int i = 1; i <= n; ++i)
    {
        p[i] = p[i - 1];
        if (a[i] == b[i])
            ++p[i];
    }

    ul[0] = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (a[i] > b[i])
        {
            ul[i] = i;
        }
        else
        {
            ul[i] = ul[i - 1];
        }
    }
    ur[n + 1] = n + 1;
    for (int i = n; i >= 1; --i)
    {
        if (a[i] > b[i])
        {
            ur[i] = i;
        }
        else
        {
            ur[i] = ur[i + 1];
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (a[i] <= b[i])
        {
            if (p[ur[i] - 1] - p[ul[i]] > 0)
                ++ans;
        }
    }

    return ans;
}

map<int, int> mp;

int maxu[N * 4];
void ubd(int tl, int tr, int x, int y, int pos)
{
    if (tl == tr)
    {
        maxu[pos] = y;
        return;
    }
    int m = (tl + tr) / 2;
    if (x <= m)
        ubd(tl, m, x, y, pos * 2);
    else
        ubd(m + 1, tr, x, y, pos * 2 + 1);
    maxu[pos] = max(maxu[pos * 2], maxu[pos * 2 + 1]);
}

int qry(int tl, int tr, int l, int r, int pos)
{
    if (l > r)
        return 0;
    if (tl == l && tr == r)
        return maxu[pos];
    int m = (tl + tr) / 2;
    return max(qry(tl, m, l, min(m, r), pos * 2),
               qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}

int solv4()
{
    for (int i = 1; i <= n; ++i)
        mp[a[i]] = i;

    for (int i = 1; i <= n; ++i)
    {
        if (mp.find(b[i]) == mp.end())
            continue;
        int x = mp[b[i]];
        if (ul[x] < i && i < ur[x])
        {
            ubd(1, n, x, qry(1, n, 1, x, 1) + 1, 1);
        }
    }
    return maxu[1];
}

void solv()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= n; ++i)
        cin >> b[i];

    bool z2 = true;
    for (int i = 1; i <= n; ++i)
    {
        if (b[i] != b[1])
        {
            z2 = false;
            break;
        }
    }

    if (z2)
    {
        cout << solv2() << "\n";
        return;
    }

    pre();

    if (n <= 5000)
    {
        cout << solv6() << "\n";
        return;
    }

    cout << solv4() << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    while (!isprime(M))
        ++M;

    /*int st = 100;
    while (st--)
    {
        n = rnf() % 100 + 1;
        for (int i = 1; i <= n; ++i)
        {
            a[i] = rnf() % 100 + 1;
        }
        b[1] = a[rnf() % n + 1];
        for (int i = 2; i <= n; ++i)
            b[i] = b[1];

        if (solv0() != solv2())
        {
            cout << "WA\n";
            return 0;
        }
    }
    cout << "OK\n";
    return 0;*/

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 6 ms 844 KB Output is correct
3 Correct 19 ms 2256 KB Output is correct
4 Correct 17 ms 2508 KB Output is correct
5 Correct 32 ms 2504 KB Output is correct
6 Correct 18 ms 2524 KB Output is correct
7 Correct 18 ms 2508 KB Output is correct
8 Correct 28 ms 2500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 4 ms 3276 KB Output is correct
3 Correct 36 ms 24036 KB Output is correct
4 Correct 170 ms 94408 KB Output is correct
5 Correct 189 ms 98456 KB Output is correct
6 Correct 183 ms 98432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 588 KB Output is correct
2 Correct 36 ms 2932 KB Output is correct
3 Correct 102 ms 6732 KB Output is correct
4 Correct 100 ms 6816 KB Output is correct
5 Correct 128 ms 7568 KB Output is correct
6 Correct 136 ms 7568 KB Output is correct
7 Correct 108 ms 7492 KB Output is correct
8 Correct 93 ms 7116 KB Output is correct
9 Correct 112 ms 7604 KB Output is correct
10 Correct 88 ms 7584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 2 ms 1228 KB Output is correct
11 Correct 1 ms 1228 KB Output is correct
12 Correct 1 ms 1228 KB Output is correct
13 Correct 2 ms 1228 KB Output is correct
14 Correct 1 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 3276 KB Output is correct
9 Correct 36 ms 24036 KB Output is correct
10 Correct 170 ms 94408 KB Output is correct
11 Correct 189 ms 98456 KB Output is correct
12 Correct 183 ms 98432 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 2 ms 1228 KB Output is correct
17 Correct 1 ms 1228 KB Output is correct
18 Correct 1 ms 1228 KB Output is correct
19 Correct 2 ms 1228 KB Output is correct
20 Correct 1 ms 1228 KB Output is correct
21 Correct 1 ms 1228 KB Output is correct
22 Correct 11 ms 8320 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 203 ms 98424 KB Output is correct
25 Correct 185 ms 98420 KB Output is correct
26 Correct 184 ms 98380 KB Output is correct
27 Correct 189 ms 98368 KB Output is correct
28 Correct 181 ms 98464 KB Output is correct
29 Correct 197 ms 98340 KB Output is correct
30 Correct 185 ms 98492 KB Output is correct
31 Correct 182 ms 98304 KB Output is correct
32 Correct 190 ms 98396 KB Output is correct