Submission #1096314

#TimeUsernameProblemLanguageResultExecution timeMemory
1096314WewooGlobal Warming (CEOI18_glo)C++17
100 / 100
63 ms10068 KiB
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int MAXN = 2e5 + 105;
typedef pair <int, int> ii;

int n, x;
int a[2 * MAXN + 10];
ii b[2 * MAXN + 10];

namespace Sub1
{
    int lis[MAXN + 5];
    void Solve()
    {
        int len = 0;
        for (int i = 1; i <= n; ++i)
        {
            int idx = lower_bound(lis + 1, lis + len + 1, a[i]) - lis;
            lis[idx] = a[i];
            len = max(len, idx);
        }

        cout << len;
    }
}

namespace Sub2
{
    int bitMax[2][2 * MAXN + 10];
    void Update(int bitMax[], int idx, int val)
    {
        for (int i = idx; i <= 2 * n; i += i & (-i))
            bitMax[i] = max(bitMax[i], val);
    }

    int getMax(int bitMax[], int idx)
    {
        int ans = 0;
        for (int i = idx; 0 < i; i -= i & (-i))
            ans = max(ans, bitMax[i]);
        return ans;
    }

    void Solve()
    {
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
            b[i].F = a[i]; b[i].S = i;
            b[n + i].F = a[i] + x, b[n + i].S = n + i;
        }

        sort(b + 1, b + 2 * n + 1);
        for (int i = 1; i <= 2 * n; ++i)
        {
            a[b[i].S] = a[b[i - 1].S] + (b[i - 1].F < b[i].F);
        }

        int res = 0;
        for (int i = 1; i <= n; ++i)
        {
            int val1 = getMax(bitMax[0], a[i] - 1) + 1;
            int val2 = getMax(bitMax[0], a[n + i] - 1) + 1;
            int val3 = getMax(bitMax[1], a[i] - 1) + 1;

            Update(bitMax[0], a[i], val1);
            Update(bitMax[1], a[i], max(val2, val3));

            res = max(max(res, val1), max(val2, val3));
        }

        cout << res;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> x;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    if (x == 0) Sub1::Solve(); else Sub2::Solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...