This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 3e5+10;
int fa[maxn];
int getf(int x) {
if (fa[x]==x) return x;
else return fa[x] = getf(fa[x]);
}
int n,m;
bool chosen[maxn];
int a[maxn];
int r[maxn];
set <int> pos;
vector <pii> b;
int sz[maxn];
int tree[maxn*4],f[maxn];
void merge(int x,int y) {
int fx = getf(x),fy = getf(y);
fa[fy] = fx;
for (int i = max(1,m-sz[fy]);i<=sz[fx] and i<m;i++) {
pos.insert(fx+sz[fx]-i);
}
sz[fx] += sz[fy];
}
void add(int tmp) {
chosen[tmp]=1;
if (m==1) pos.insert(tmp);
if (tmp!=0 and chosen[tmp-1]) merge(tmp-1,tmp);
if (tmp+1<n and chosen[tmp+1]) merge(tmp,tmp+1);
}
void update(int c,int cl,int cr,int pos) {
if (cl==cr) tree[c] = f[pos];
else {
int mid=cl+cr>>1;
if (pos<=mid) update(c<<1,cl,mid,pos);
else update(c<<1|1,mid+1,cr,pos);
tree[c] = max(tree[c<<1],tree[c<<1|1]);
}
}
int query(int c,int cl,int cr,int l ,int r) {
if (l<=cl and cr<=r) return tree[c];
int mid=cl+cr>>1;
int ret = 0;
if (l<=mid) ret = query(c<<1,cl,mid,l,r);
if (r>mid) ret = max(ret,query(c<<1|1,mid+1,cr,l,r));
return ret;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,n) cin>>a[i],b.pb({a[i],i}),fa[i]=i,sz[i] = 1;
sort(ALL(b));
reverse(ALL(b));
int cur = 0;
rep(i,0,n) {
while (cur<n and b[cur].fi>b[i].fi) add(b[cur++].se);
auto it = upper_bound(ALL(pos),b[i].se);
int tmp;
if (it==pos.end()) tmp = n-m;
else tmp = *it;
r[b[i].se] = tmp+m-1;
}
//rep(i,0,n) debug(r[i]);
cur = 0;
int ans = 0;
rep(i,0,n) {
while (cur<n and b[cur].fi>b[i].fi) update(1,0,n-1,b[cur++].se);
int val = 1;
val += query(1,0,n-1,b[i].se,r[b[i].se]);
f[b[i].se] = val;
//cout<<b[i].se<<" "<<rend[b[i].se]<<" "<<val<<endl;
ans = max(ans,val);
}
cout<<ans;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void update(int, int, int, int)':
Main.cpp:62:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int mid=cl+cr>>1;
| ~~^~~
Main.cpp: In function 'int query(int, int, int, int, int)':
Main.cpp:71:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid=cl+cr>>1;
| ~~^~~
# | 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... |