# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
482343 | Blistering_Barnacles | Global Warming (CEOI18_glo) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//apig's property
//Happiness can be found, even in the darkest of times, if one only remembers to turn on the light
//El Pueblo Unido Jamas Sera Vencido
//Do or do not... there is no try
//Billions of bilious blue blistering barnacles in a thundering typhoon!
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
#define F first
#define S second
#define pb push_back
#define vll vector< ll >
#define vi vector< int >
#define pll pair< ll , ll >
#define pi pair< int , int >
#define all(s) s.begin() , s.end()
#define sz(s) s.size()
#define str string
#define md ((s + e) / 2)
#define mid ((l + r) / 2)
#define msdp(dp) memset(dp , -1 , sizeof dp)
#define mscl(dp) memset(dp , 0 , sizeof dp)
#define C continue
#define R return
#define B break
#define lx node * 2
#define rx node * 2 + 1
#define br(o) o ; break
#define co(o) o ; continue
using namespace std;
typedef long long ll;
ll q, suf[555555] , dp1[555555] , dp2[555555] ,tree2[1000005], a[555555] , tree1[1000005], k, l, m, n, o, p;
map < ll , ll > mp;
vll adj[555555];
const ll mod = 1e9+7;
str s;
void update1(ll s , ll e , ll node , ll idx , ll val){
if(s > idx || e < idx)R ;
if(s == e){
tree1[node] = max(tree1[node] , val) ;
R ;
}
update1(s , md , lx , idx , val) , update1(md + 1 , e , rx , idx , val) ;
tree1[node] = max(tree1[lx] , tree1[rx]) ;
}
ll query1(ll s , ll e , ll node , ll l , ll r){
if(s > r || e < l || l > r)R 0 ;
if(l <= s && e <= r)R tree1[node] ;
R max(query1(md + 1 , e , rx , l , r) , query1(s , md , lx , l , r)) ;
}
void update2(ll s , ll e , ll node , ll idx , ll val){
if(s > idx || e < idx)R ;
if(s == e){
tree2[node] = max(tree2[node] , val) ;
R ;
}
update2(s , md , lx , idx , val) , update2(md + 1 , e , rx , idx , val) ;
tree2[node] = max(tree2[lx] , tree2[rx]) ;
}
ll query2(ll s , ll e , ll node , ll l , ll r){
if(s > r || e < l || l > r)R 0 ;
if(l <= s && e <= r)R tree2[node] ;
R max(query2(md + 1 , e , rx , l , r) , query2(s , md , lx , l , r)) ;
}
void solve(){
vll v ;
cin >> n >> k ;
for(ll i = 1 ; i <= n ; i++){
cin >> a[i] ;
v.pb(a[i]) ;
v.pb(a[i] - k + 1)
}
sort(all(v)) ;
m = 0 ;
for(auto u : v){
mp[u] = ++m ;
}
ll ans = 0 ;
for(ll i = n ; i >= 1 ; i--){
dp2[i] = query2(1 , m , 1 , mp[a[i]] + 1 , m) + 1 ;
update2(1 , m , 1 , mp[a[i]] , dp2[i]) ;
}
for(ll i = 1 ; i <= n ; i++){
dp1[i] = query1(1 , m , 1 , 1 , mp[a[i]] - 1) + 1 ;
update1(1 , m , 1 , mp[a[i]] , dp1[i]) ;
}
mscl(tree2) ;
for(ll i = n ; i >= 1 ; i--){
ans = max(ans , dp1[i] + query2(1 , m , 1 , mp[a[i] - k + 1] , mp[a[i]])) ;
update2(1 , m , 1 , mp[a[i]] , dp2[i]);
}
cout << ans << endl ;
}
int main(){
fast ;
q = 1 ;
//cin >> q ;
while(q--){
solve() ;
}
}