This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 300005
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int br;
map<int,int> sl;
int n,d;
int niz[maxn];
vector<int> poz[maxn];
int dp[maxn];
int desno[maxn];
int seg[4 * maxn];
void update(int node, int l, int r, int poz, int val){
if(l == r){
seg[node] = val;
return;
}
int mid = (l + r) / 2;
if(poz <= mid)update(node * 2, l, mid, poz, val);
else update(node * 2 + 1, mid + 1, r, poz, val);
seg[node] = max(seg[node * 2], seg[node * 2 + 1]);
}
int query(int node, int l, int r, int levo, int desno){
if(l > r || levo > desno || levo > r || l > desno)return 0;
if(l >= levo && r <= desno)return seg[node];
int mid = (l + r) / 2;
return max(query(node * 2, l, mid, levo, desno), query(node * 2 + 1, mid + 1, r, levo, desno));
}
set<int> s1;
set<int> s2;
int resi(int x){
int ppp = s1.size();
if(ppp == 0)return 1 + query(1,1,n,1,x - 1);
auto it = s1.lower_bound(x);
if(it == s1.begin())return 1 + query(1,1,n,1,x - 1);
it--;
if((*it)+d > x){
if(it == s1.begin())return 1 + query(1,1,n,1,x - 1);
it--;
}
if((*it)+d > x){
if(it == s1.begin())return 1 + query(1,1,n,1,x - 1);
it--;
}
return 1 + query(1,1,n,min((*it)+d-1, x-1), x-1);
}
void dodaj(int x){
auto it = s1.lower_bound(x);
if(it == s1.begin() && (*it) != x)return;
if((*it) != x)it--;
auto it2 = s2.lower_bound(x);
s2.insert(x);
int p1 = *it;
int p2 = *it2;
if(x - p1 < d)s1.erase(p1);
if(p2 - (x+1) >= d)s1.insert(x+1);
return;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> d;
s2.insert(0);
s2.insert(n+1);
vector<int> svi;
ff(i,1,n){
cin >> niz[i];
svi.pb(niz[i]);
}
sort(svi.begin(), svi.end());
int last = -1;
br = 1;
for(auto c:svi){
if(c == last)continue;
sl[c] = br;
br++;
last = c;
}
ff(i,1,n)niz[i] = sl[niz[i]];
ff(i,1,n){
poz[niz[i]].pb(i);
}
s1.insert(1);
ff(i,1,br-1){
for(auto c:poz[i]){
dp[c] = resi(c);
}
for(auto c:poz[i]){
dodaj(c);
update(1,1,n,c,dp[c]);
}
}
int ans = 0;
ff(i,1,n)ans = max(ans, dp[i]);
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
Main.cpp:102:5: note: in expansion of macro 'ff'
102 | ff(i,1,n){
| ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
Main.cpp:115:5: note: in expansion of macro 'ff'
115 | ff(i,1,n)niz[i] = sl[niz[i]];
| ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
Main.cpp:116:5: note: in expansion of macro 'ff'
116 | ff(i,1,n){
| ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
Main.cpp:123:5: note: in expansion of macro 'ff'
123 | ff(i,1,br-1){
| ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
Main.cpp:133:5: note: in expansion of macro 'ff'
133 | ff(i,1,n)ans = max(ans, dp[i]);
| ^~
# | 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... |