水太......
1 second
256 megabytes
standard input
standard output
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 pillarsi2,
..., 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; typedef long long int LL; const int maxn=100100; int n,K;
LL h[maxn],sum[maxn],tr[maxn];
vector<LL> road; int main()
{
cin>>n>>K;
sum[1]=1; int maxl=1;
for(int i=1;i<=n;i++)
{
cin>>h[i];
sum[i]=1;
for(int j=max(1,i-500);j<i;j++)
{
if(abs(h[j]-h[i])>=K&&sum[j]+1>sum[i])
{
sum[i]=sum[j]+1;
tr[i]=j;
}
if(sum[i]>sum[maxl])
{
maxl=i;
}
}
}
cout<<sum[maxl]<<endl;
int T=tr[maxl];
road.push_back(maxl);
while(T)
{
road.push_back(T);
T=tr[T];
}
while(road.size())
{
cout<<road.back()<<" ";
road.pop_back();
}
return 0;
}
版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss