您的位置:首页 > 其它

hdu 5087(LIS变形)

2016-06-27 08:54 288 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087

解题思路:这道题其实仔细想想很简单,次长LIS只有两种可能,一种就是等于LIS-1,一种就是LIS,什么时候就是等于LIS呢?肯定LIS的个数不唯一。转化到这里,这道题就比较简单了。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int step[maxn],dp[maxn], num[maxn];
int main ()
{
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
memset(step, 0, sizeof(step));
dp[0] = 1;
for(int i = 1; i <= n; i++)
scanf("%d", &num[i]);
int Max = -1;
for(int i = 1; i <= n; i++){
for(int j = 0; j < i; j++){
if(num[i] > num[j]){
if(step[j]+1 > step[i]){
step[i] = step[j] + 1;
dp[i] = dp[j];
}
else if(step[j] + 1 == step[i])
dp[i] += dp[j];
}
}
Max = max(Max, step[i]);
}
int flag = 0;
int ok = 1;
for(int i = 1; i <= n; i++){
if(step[i] == Max){
if(dp[i] > 1) ok = 0;
else{
if(flag) ok = 0;
flag = 1;
}
}
}
printf("%d\n", Max - ok );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp