#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];
int tree2[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;
}
void upd2(int v, int val){
v += N;
tree2[v] = val;
v /= 2;
while(v){
tree2[v] = max(tree2[v * 2], tree2[2 * v + 1]);
v /= 2;
}
}
int query2(int l, int r){
l += N; r += N;
int wyn = 0;
while(l <= r){
if(l % 2 == 1){
wyn = max(wyn, tree2[l]);
l++;
}
if(r % 2 == 0){
wyn = max(wyn, tree2[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], dq2[n + 7];
vect start(n + 1);
vector<pii> dp(n + 1);
vect dp2(n + 1);
dp[0] = {0, 0};
dp2[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);
int w3 = max(query2(1, a[i] - 1) + 1, query2(a[i], a[i]));
dp2[i] = max(w1.f, w3);
w1.f = max(w1.f, w3);
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(!dq2[a[i]].empty() && dp2[dq2[a[i]].back()] <= dp2[i]){
dq2[a[i]].pop_back();
}
dq2[a[i]].pb(i);
upd(maxi, dp[dq[maxi].front()].f);
upd2(a[i], dp2[dq2[a[i]].front()]);
if(i - d > 0){
maxi = dp[i - d].s;
while(!dq[maxi].empty() && dq[maxi].front() <= i - d){
dq[maxi].pop_front();
}
while(!dq2[a[i - d]].empty() && dq2[a[i - d]].front() <= i - d){
dq2[a[i - d]].pop_front();
}
if(!dq[maxi].empty()){
upd(maxi, dp[dq[maxi].front()].f);
}else{
upd(maxi, 0);
}
if(!dq2[a[i - d]].empty()){
upd2(a[i - d], dp2[dq2[a[i - d]].front()]);
}else{
upd2(a[i - d], 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... |