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>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
struct Seg {
int siz;
vector<int> v;
void init(int n){
siz = 1;
while(siz < n)siz*=2;
siz*=2;
v.assign(siz * 2 , 0);
}
void set(int i , int val , int x , int lx , int rx){
if(rx - lx == 1){
v[x] = val;
return;
}
int m = (lx + rx)/2;
if(i < m)set(i , val , 2 *x + 1 , lx , m);
else set(i , val , 2 * x + 2 , m , rx);
v[x] = max(v[2 * x + 1] , v[2 * x + 2]);
}
void set(int i , int val){
set(i , val , 0 , 0 , siz);
}
int mx(int l , int r , int x , int lx , int rx){
if(r <= lx || rx <= l)return 0;
if(lx >= l && rx <= r)return v[x];
int m = (lx + rx)/2;
return max(mx(l , r , 2 * x+ 1 , lx , m) , mx(l , r , 2 * x + 2 , m , rx));
}
int mx(int l , int r){
return mx(l , r , 0 , 0 , siz);
}
};
void solve(){
int n;
cin >> n;
int d;
cin >> d;
int arr[n];
for(int i = 0; i < n; i++)cin >> arr[i];
map<int,int> m;
for(int i = 0; i < n; i++){
m[arr[i]]++;
}
int cur = 0;
for(auto it : m){
m[it.vf] = cur;
cur++;
}
for(int i = 0; i < n; i++){
arr[i] = m[arr[i]];
}
set<pair<int,int>> s;
int dp[n];
Seg st;
st.init(n + 5);
for(int i = 0; i < n; i++){
while(s.size() > 0){
int p = (*s.begin()).vs;
p *= -1;
if(p < i - d){
st.set(arr[p] , 0);
s.erase(s.begin());
}
else break;
}
int mx = 0;
if(s.size() == 0){
dp[i] = 1;
st.mx(arr[i],arr[i]+1);
if(dp[i] > mx)st.set(arr[i] , dp[i]);
}
else {
dp[i] = st.mx(0 , arr[i]) + 1;
mx = st.mx(arr[i],arr[i]+1);
if(dp[i] > mx)st.set(arr[i] , dp[i]);
/*auto it = s.end();
it--;
pair<int,int> p = *it;
if(arr1[-p.vs] >= arr[i]){
mx = max(mx , p.vf);
arr1[i]=arr1[-p.vs];
}*/
}
s.insert(mp(arr[i],-i));
}
cout << st.mx(arr[n-1],n + 1) << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
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... |