序列问题II | ||||||
| ||||||
Description | ||||||
给一个长度为n的整数序列A0,A1,......An-1,找出最长的非递增子序列的长度 | ||||||
Input | ||||||
输入第一行为数据组数T(T<=20)。 每组数据的第一行为整数的个数n(2<=n<=1000),第二行为n个绝对值不超过150000的整数。 | ||||||
Output | ||||||
对于每组数据,输出最长的非递增子序列的长度。 | ||||||
Sample Input | ||||||
2 3 1 1 3 4 -1 4 3 2 | ||||||
Sample Output | ||||||
2 3 | ||||||
Author | ||||||
陈禹@HRBUST |
递增子序列问题,就是在当前位子i之前找到一个位子j,并且a[i]>a[j]并且dp【j】是i之前最大的值,也就是说,每一次遍历的时候,都是找到i之前找到一个最长的递增子序列,并且保证a[i]<a[j]。相反的,如果找非递增子序列,我们就可以把限制条件改成:a[i]<=a[j]就可以了
AC代码如下:
/*非递增子序列问题。*/
#include<stdio.h>
#include<string.h>
using namespace std;
int a[1005];
int dp[1005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int output=1;
dp[0]=1;
for(int i=1;i<n;i++)
{
int max=0;
for(int j=0;j<n;j++)
{
if(a[i]<=a[j]&&max<dp[j])max=dp[j];
}
dp[i]=max+1;
if(dp[i]>=output)output=dp[i];
}
printf("%d\n",output);
}
}