E.XKC's basketball team
ps:
During the game misread the title, but have not found (a pain). That the number required to meet the requirements, there has been no idea, I did not expect is for the most long-distance, it is very easy.
solution:
Greedy from the back maintains a monotonically increasing sequence, because the role for i <j, a [i] <= a [j], then for i number before a [j] can completely replace a [i] is.
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=5e5+5; 4 typedef long long ll; 5 6 inline int read() 7 { 8 char ch=getchar();int x=0,f=0; 9 while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar(); 10 while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); 11 return f?-x:x; 12 } 13 14 15 int a[maxn]; 16 int num[maxn]; 17 vector <int > V; 18 int main(){ 19 int n,m;n=read();m=read(); 20 for(int i=1;i<=n;i++) a[i]=read(); 21 V.push_back(n);num[n]=-1; 22 for(int i=n-1;i>=1;i--){ 23 int tmp=a[i]+m; 24 int l=0;int r=V.size()-1; 25 int ans=-1; 26 while(l<=r){ 27 int mid=(l+r)/2; 28 if(a[V[mid]]>=tmp) 29 { 30 ans=max(ans,V[mid]); 31 r=mid-1; 32 } 33 else{ 34 l=mid+1; 35 } 36 } 37 if(ans==-1) num[i]=ans; 38 else num[i]=ans-i-1; 39 if(a[i]>a[V[V.size()-1]]) V.push_back(i); 40 41 } 42 for(int i=1;i<=n;i++){ 43 if(i==1) cout <<num[i]; 44 else cout <<" "<<num[i]; 45 } 46 return 0; 47 }