#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vect;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
#define pb push_back
#define f first
#define s second
const int N = 1024 * 512;
pii tree[2 * N];
bool cmp(pii a, pii b){
if(a.f != b.f) return a.f > b.f;
return a.s < b.s;
}
void upd(int v, int val){
int ind = v;
v += N;
tree[v] = {val, ind};
v /= 2;
while(v){
if(cmp(tree[2 * v], tree[2 * v + 1])){
tree[v] = tree[2 * v];
}else{
tree[v] = tree[2 * v + 1];
}
v /= 2;
}
}
pii query(int l, int r){
l += N; r += N;
pii wyn = {0, 1e9};
while(l <= r){
if(l % 2 == 1){
if(cmp(tree[l], wyn)){
wyn = tree[l];
}
l++;
}
if(r % 2 == 0){
if(cmp(tree[r], wyn)){
wyn = tree[r];
}
r--;
}
l /= 2; r /= 2;
}
return wyn;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, d;
cin >> n >> d;
vect a(n + 1);
a[0] = 0;
for(int i = 0; i < 2 * N; i++){
tree[i].s = 1e9 + 1;
}
for(int i = 1; i<= n; i++){
cin >> a[i];
}
map<int, int> skal;
int it = 1;
for(int i = 1; i <= n; i++){
skal[a[i]] = 1;
}
for(auto& x : skal){
x.s = it++;
}
for(int i = 1; i <= n; i++){
a[i] = skal[a[i]];
}
deque<int> dq[n + 7], mini;
vect start(n + 1);
vector<pii> dp(n + 1);
dp[0] = {0, 0};
upd(0, 0);
for(int i = 1; i <= n; i++){
pii w1 = query(0, a[i] - 1); w1.f++; w1.s = a[i];
pii w2 = query(a[i], it + 7);
if(!mini.empty() && a[mini.front()] < a[i] && w1.f == 1){
w1 = {2, a[i]};
}
if(cmp(w1, w2)){
dp[i] = w1;
}else{
dp[i] = w2;
}
int maxi = dp[i].s;
while(!dq[maxi].empty() && dp[dq[maxi].back()].f <= dp[i].f){
dq[maxi].pop_back();
}
dq[maxi].pb(i);
while(!mini.empty() && a[mini.back()] >= a[i]){
mini.pop_back();
}
mini.pb(i);
upd(maxi, dp[dq[maxi].front()].f);
if(i - d > 0){
maxi = dp[i - d].s;
while(!dq[maxi].empty() && dq[maxi].front() <= i - d){
dq[maxi].pop_front();
}
while(!mini.empty() && mini.front() <= i - d){
mini.pop_front();
}
if(!dq[maxi].empty()){
upd(maxi, dp[dq[maxi].front()].f);
}else{
upd(maxi, 0);
}
}
// cout << "i: " << dp[i].f << " " << dp[i].s << "\n";
}
cout << dp[n].f;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |