Marmot found a row with n pillars. The i-th pillar has the height of hi meters. Starting from one pillar i1, Marmot wants to jump on the pillars i2, ..., ik. (1 ≤ i1 < i2 < ... < ik ≤ n). From a pillar i Marmot can jump on a pillar j only if i < j and |hi - hj| ≥ d, where |x| is the absolute value of the number x.
Now Marmot is asking you find out a jump sequence with maximal length and print it.
The first line contains two integers n and d (1 ≤ n ≤ 105, 0 ≤ d ≤ 109).
The second line contains n numbers h1, h2, ..., hn (1 ≤ hi ≤ 1015).
The first line should contain one integer k, the maximal length of a jump sequence.
The second line should contain k integers i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n), representing the pillars' indices from the maximal length jump sequence.
If there is more than one maximal length jump sequence, print any.
5 2
1 3 6 7 4
4
1 2 3 5
10 3
2 1 3 6 9 11 7 3 20 18
6
1 4 6 7 8 9
In the first example Marmot chooses the pillars 1, 2, 3, 5 with the heights 1, 3, 6, 4. Another jump sequence of length 4 is 1, 2, 4, 5.
题意:
题解:
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <bits/stdc++.h> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <iostream> 6 #include <cstdlib> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath> 10 #include <cctype> 11 #include <map> 12 #include <set> 13 #include <queue> 14 #include <bitset> 15 #include <string> 16 #include <complex> 17 #define LL long long 18 #define mod 1000000007 19 using namespace std; 20 #define N 200005 21 #define M 2000006 22 #define INF 1e18 23 struct SGT 24 { 25 int maxx[N<<2]; 26 void build(int l,int r,int pos) 27 { 28 memset(maxx,0,sizeof(maxx)); 29 } 30 void update(int p,int c,int l,int r,int pos) 31 { 32 if(l==r) 33 { 34 maxx[pos]=c; 35 return; 36 } 37 int mid=(l+r)>>1; 38 if(p<=mid)update(p,c,l,mid,pos<<1); 39 else update(p,c,mid+1,r,pos<<1|1); 40 maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]); 41 } 42 int query(int L,int R,int l,int r,int pos) 43 { 44 if(L<=l&&r<=R)return maxx[pos]; 45 int mid=(l+r)>>1; 46 int ans=0; 47 if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<1)); 48 if(R>mid)ans=max(ans,query(L,R,mid+1,r,pos<<1|1)); 49 return ans; 50 } 51 }tree; 52 LL a[N],b[N]; 53 int n; 54 LL k; 55 int big(LL x) 56 { 57 int pos=lower_bound(b,b+2+n,x)-b; 58 return pos; 59 } 60 int low(LL x) 61 { 62 int pos=upper_bound(b,b+2+n,x)-b-1; 63 return pos; 64 } 65 int dp[N]; 66 vector<int>ans; 67 int main() 68 { 69 scanf("%d%lld",&n,&k); 70 for(int i=1;i<=n;i++) 71 { 72 scanf("%lld",&a[i]); 73 b[i]=a[i]; 74 } 75 sort(b+1,b+1+n); 76 b[n+1]=INF; 77 b[0]=-INF; 78 for(int i=1;i<=n;i++) 79 { 80 LL pre=a[i]-k; 81 LL nex=a[i]+k; 82 int pos1=low(pre); 83 int pos2=big(nex); 84 int maxx=0; 85 if(pos1>=1)maxx=max(maxx,tree.query(1,pos1,1,n,1)); 86 if(pos2<=n)maxx=max(maxx,tree.query(pos2,n,1,n,1)); 87 dp[i]=maxx+1; 88 tree.update(low(a[i]),maxx+1,1,n,1); 89 } 90 int maxx=0,pre=-1; 91 for(int i=1;i<=n;i++){ 92 if(dp[i]>maxx){ 93 maxx=dp[i]; 94 pre=i; 95 } 96 } 97 ans.push_back(pre); 98 for(int i=pre-1;i>=1;i--){ 99 if(abs(a[i]-a[pre])>=k&&dp[pre]==dp[i]+1){ 100 pre=i; 101 ans.push_back(pre); 102 } 103 } 104 printf("%d ",maxx); 105 for(int i=maxx-1;i>=0;i--) 106 printf("%d ",ans[i]); 107 return 0; 108 }