Submission #707839

#TimeUsernameProblemLanguageResultExecution timeMemory
707839Alihan_8Global Warming (CEOI18_glo)C++17
100 / 100
152 ms15044 KiB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
bool chmax(int &x, int y){
    bool flag = false;
    if ( x < y ){
        x = y; flag |= true;
    }
    return flag;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, x; cin >> n >> x;
    vector <int> p(n), pos;
    for ( auto &i: p ){
        cin >> i;
        pos.pb(i), pos.pb(i+x);
    }
    sort(all(pos));
    pos.resize(unique(all(pos))-pos.begin());
    auto f = [&](int x){
        return lower_bound(all(pos), x)-pos.begin()+1;
    };
    vector <int> dp, L(n), R(n);
    for ( int i = 0; i < n; i++ ){
        auto it = lower_bound(all(dp), p[i])-dp.begin();
        L[i] = it+1;
        if ( it == (int)dp.size() ) dp.pb(p[i]);
        else dp[it] = p[i];
    }
    dp.clear();
    for ( int i = n-1; i >= 0; i-- ){
        auto it = lower_bound(all(dp), -p[i])-dp.begin();
        R[i] = it+1;
        if ( it == (int)dp.size() ) dp.pb(-p[i]);
        else dp[it] = -p[i];
    }
    int sz = (int)pos.size();
    vector <int> bit(sz+1);
    auto upd = [&](int i, int val){
        for (; i <= sz; i += i&-i ) chmax(bit[i], val);
    };
    auto get = [&](int i){
        int Mx = 0;
        for (; i > 0; i -= i&-i ) chmax(Mx, bit[i]);
        return Mx;
    };
    int Mx = 0;
    for ( int i = 0; i < n; i++ ){
        chmax(Mx, get(f(p[i]+x)-1)+R[i]);
        upd(f(p[i]), L[i]);
    }
    cout << Mx;

    cout << '\n';
}

Compilation message (stderr)

glo.cpp: In function 'void IO(std::string)':
glo.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"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...