Submission #827893

#TimeUsernameProblemLanguageResultExecution timeMemory
827893BT21tataGlobal Warming (CEOI18_glo)C++17
100 / 100
128 ms9776 KiB
#include<bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // #pragma GCC target ("avx,avx2,fma") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define SPEED ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0) #define rall(v) (v).rbegin(),(v).rend() #define all(v) (v).begin(),(v).end() #define setp fixed<<setprecision #define OK cerr<<"OK"<<endl<<flush #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define F first #define S second #define y0 jahdakdh #define y1 jahsadakdakdh #define endl '\n' const ll MOD=1e9+7; const ll mod=(1ll<<31)-1; const ld eps=1e-8; const ll MAXLONG=9223372036854775807; const ll MINLONG=-9223372036854775807; using namespace std; mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count()); const int N=2e5+5; int n, x, a[N], ans, t[N<<2]; vector<pii>v; vector<int> LIS() { vector<int>v{a[0]}, ret{1}; for(int i=1; i<n; i++) { if(v.back()<a[i]) { v.pb(a[i]); ret.pb(v.size()); } else { int pos=lower_bound(all(v), a[i])-v.begin(); v[pos]=a[i]; ret.pb(pos+1); } } return ret; } void add(int v, int l, int r, int pos, int val) { if(l==r) { t[v]=val; return; } int mid=(l+r)>>1; if(pos<=mid) add(v<<1, l, mid, pos, val); else add(v<<1|1, mid+1, r, pos, val); t[v]=max(t[v<<1], t[v<<1|1]); } int mx(int v, int l, int r, int tl, int tr) { if(tl<=l and r<=tr) return t[v]; if(r<tl or tr<l) return 0; int mid=(l+r)>>1; return max(mx(v<<1, l, mid, tl, tr), mx(v<<1|1, mid+1, r, tl, tr)); } int main() { SPEED; cin>>n>>x; for(int i=0; i<n; i++) cin>>a[i], v.pb({a[i], i}); vector<int>l = LIS(); sort(all(v)); reverse(all(v)); reverse(a, a+n); for(int i=0; i<n; i++) a[i]=-a[i]; vector<int>r = LIS(); reverse(all(r)); reverse(a, a+n); for(int i=0; i<n; i++) a[i]=-a[i]; a[n]=MOD; r.pb(0); int cur=0; for(pii u : v) { //cout<<u.F<<' '<<u.S<<' '<<l[u.S]<< ' '<<cur<<endl; while(cur<n and v[cur].F+x>u.F) add(1, 0, n-1, v[cur].S, r[v[cur].S]), cur++; ans=max(ans, l[u.S]+mx(1, 0, n-1, u.S+1, n-1)); } cout<<ans<<endl; 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...