题目描述
In the fantasy world of ICPC there are magical beasts. As they grow, these beasts can change form, and every time they do they become more powerful. A beast cannot change form completely arbitrarily though. In each form a beast has n eyes and k horns, and these affect the changes it can make.
A beast can only change to a form with more horns than it currently has.
A beast can only change to a form that has a difference of at most w eyes. So, if the beast currently has n eyes it can change to a form with eyes in range [n - w, n + w].
A beast has one form for every number of eyes between 1 and N, and these forms will also have an associated number of horns. A beast can be born in any form. The question is, how powerful can one of these beasts become? In other words, how many times can a beast change form before it runs out of possibilities?
输入
The first line contains two integers, N and w, that indicate, respectively, the maximum eye number, and the maximum eye difference allowed in a change (1 ≤ N ≤ 5000; 0 ≤ w ≤ N).
The next line contains N integers which represent the number of horns in each form. I.e. the ith number, h(i), is the number of horns the form with i eyes has (1 ≤ h(i) ≤ 1 000 000).
输出
For each test case, display one line containing the maximum possible number of changes.
样例输入
5 5
5 3 2 1 4
样例输出
4
提示
Start with 1 horn and 4 eyes, and it can change 4 times: (1 horn 4 eyes) -> (2 horns 3 eyes) -> (3 horns 2 eyes) -> (4 horns 5 eyes) -> (5 horns 1 eye).
思路
先将野兽的形态根据horns排序,再用最大上升子序列的想法,找符合要求的eye,注意horns相同的情况,最后输出最大的序列长度
代码实现
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N =5005;
struct node
{
int h,e;
}ani[N];
bool cmp(node a,node b)
{
if(a.h!=b.h) return a.h>b.h;
return a.e>b.e;
}
int n,w;
int dp[N];
int ans;
int main()
{
scanf("%d%d",&n,&w);
for(int i=0;i<n;i++)
{
scanf("%d",&ani[i].h);
ani[i].e=i+1;
}
sort(ani,ani+n,cmp);
for(int i=1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(ani[i].e>=ani[j].e-w && ani[i].e<=ani[j].e+w)
{
if(ani[j].h>ani[i].h) dp[i]=max(dp[i],dp[j]+1);
}
}
ans=max(dp[i],ans);
}
printf("%d\n",ans);
return 0;
}