Submission #1291538

#TimeUsernameProblemLanguageResultExecution timeMemory
1291538thaibeo123Global Warming (CEOI18_glo)C++20
100 / 100
101 ms6040 KiB
#include <bits/stdc++.h>
using namespace std;

#define NAME "A"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define MASK(x) (1ll << (x))
#define BIT(x, i) (((x) >> (i)) & 1)

const int N = 2e5 + 5;

struct FenwickTree {
    int n;
    vector<int> fen;

    FenwickTree(int n): n(n), fen(n + 1) {}

    void update(int x, int v) {
        for (int i = x; i <= n; i += i & -i) {
            fen[i] = max(fen[i], v);
        }
    }

    int get(int x) {
        int res = 0;
        for (int i = x; i > 0; i -= i & -i) {
            res = max(res, fen[i]);
        }
        return res;
    }
};

int n, x;
int a[N];
vector<int> vals;

int getid(int x) {
    return upper_bound(all(vals), x) - vals.begin();
}

void input() {
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        vals.pb(a[i]);
        vals.pb(a[i] - x);
    }
    sort(all(vals));
    vals.erase(unique(all(vals)), vals.end());
}

void solve() {
    FenwickTree one(vals.size()), two(vals.size());
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int cur = max(one.get(getid(a[i]) - 1), two.get(getid(a[i]) - 1)) + 1;
        ans = max(ans, cur);
        two.update(getid(a[i]), cur);
        cur = one.get(getid(a[i] - x) - 1) + 1;
        one.update(getid(a[i] - x), cur);
        ans = max(ans, cur);
    }
    cout << ans;
}

signed main() {
    if (fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    int t = 1;
    //cin >> t;
    while (t--) {
        input();
        solve();
    }

    return 0;
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(NAME".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen(NAME".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...