제출 #1048332

#제출 시각아이디문제언어결과실행 시간메모리
1048332vako_pGlobal Warming (CEOI18_glo)C++14
0 / 100
216 ms13684 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int mxN = 2e5 + 5; ll n,mx,a[mxN],dp[mxN],dp1[mxN]; pair<ll,ll> b[mxN]; struct segtree{ vector<ll> v; ll sz = 1; void init(){ while(sz < n) sz *= 2; v.assign(2 * sz, 0LL); } void clear(){ for(int i = 0; i < 2 * sz; i++) v[i] = 0LL; } void set(ll val, ll i, ll x, ll l, ll r){ if(r - l == 1){ v[x] = val; return; } ll mid = l + (r - l) / 2; if(i < mid) set(val, i, 2 * x + 1, l, mid); else set(val, i, 2 * x + 2, mid, r); v[x] = max(v[2 * x + 1], v[2 * x + 2]); } void set(ll val, ll i){ set(val, i, 0, 0, sz); } ll find(ll l, ll r, ll x, ll lx, ll rx){ if(lx >= r or rx <= l) return 0LL; if(lx >= l and rx <= r) return v[x]; ll mid = lx + (rx - lx) / 2; return max(find(l, r, 2 * x + 1, lx, mid), find(l, r, 2 * x + 2, mid, rx)); } ll find(ll l, ll r){ return find(l, r, 0, 0, sz); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> mx; for(int i = 0; i < n; i++){ cin >> a[i]; b[i] = {a[i], i}; } segtree s; s.init(); sort(b, b + n); for(int i = 0; i < n; i++){ dp[b[i].second] = s.find(0, b[i].second) + 1; s.set(dp[b[i].second], b[i].second); } s.clear(); reverse(b, b + n); for(int i = 0; i < n; i++){ dp1[b[i].second] = s.find(b[i].second + 1, n) + 1; s.set(dp1[b[i].second], b[i].second); } reverse(b, b + n); s.clear(); ll l = 0,ans = 0; for(int i = 0; i < n; i++){ while(l < n and b[l].first < b[i].first + mx){ s.set(dp[b[l].second], b[l].second); l++; } ans = max(ans, dp1[b[i].second] + s.find(0, b[i].second)); } s.clear(); reverse(b, b + n); l = n - 1; for(int i = 0; i < n; i++){ if(l >= 0) while(b[l].first > b[i].first - mx){ s.set(dp1[b[l].second], b[l].second); l--; if(l < 0) break; } ans = max(ans, dp[b[i].second] + s.find(b[i].second + 1, n)); } cout << ans; }
#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...