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;
}
解题思路:这道题其实仔细想想很简单,次长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;
}
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题