이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=3e5+5;
const int inf=(int)1e18+9;
int t[MAX*2];
int a[MAX];
int p[MAX];
int minid[MAX];
void update(int u,int c){
for (t[u+=MAX]=c;u>1;u>>=1)t[u>>1]=max(t[u],t[u^1]);
}
int query(int u,int v){
int maxi=0;
for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1){
if (u&1)maxi=max(maxi,t[u++]);
if (v&1)maxi=max(maxi,t[--v]);
}
return maxi;
}
bool cmp(pii a,pii b){
if (a.st!=b.st)return a.st<b.st;
return a.nd>b.nd;
}
set<int>checked;
int Find(int u){
if (p[u]!=u)p[u]=Find(p[u]);
return p[u];
}
void Union(int u,int v){
minid[Find(v)]=min(minid[Find(v)],minid[Find(u)]);
p[Find(u)]=Find(v);
}
int32_t main(){
BOOST;
int n,d;
cin>>n>>d;
vector<pii>pom;
for (int i=1;i<=n;i++){
cin>>a[i];
pom.pb(mp(a[i],i));
}
for (int i=1;i<=n;i++)p[i]=i,minid[i]=i;
checked.ins(-1);
checked.ins(n+1);
int ans=0;
sort(pom.begin(),pom.end(),cmp);
for (auto iter:pom){
int id=iter.nd;
auto it=*(--checked.lower_bound(id));
if (it!=-1 && id-it<=d)Union(id,it);
it=*checked.upper_bound(id);
if (it!=n+1 && it-id<=d)Union(id,it);
int l=minid[Find(id)];
int r=id;
int dpek=query(l,r)+1;
update(id,dpek);
ans=max(ans,dpek);
checked.ins(id);
}
cout<<ans;
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... |