hdu 5087 Revenge of LIS II(LIS,思路)
来源:互联网 发布:将mac照片导入ipad 编辑:程序博客网 时间:2024/05/12 16:16
Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1419 Accepted Submission(s): 486
Problem Description
In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
---Wikipedia
Today, LIS takes revenge on you, again. You mission is not calculating the length of longest increasing subsequence, but the length of the second longest increasing subsequence.
Two subsequence is different if and only they have different length, or have at least one different element index in the same place. And second longest increasing subsequence of sequence S indicates the second largest one while sorting all the increasing subsequences of S by its length.
---Wikipedia
Today, LIS takes revenge on you, again. You mission is not calculating the length of longest increasing subsequence, but the length of the second longest increasing subsequence.
Two subsequence is different if and only they have different length, or have at least one different element index in the same place. And second longest increasing subsequence of sequence S indicates the second largest one while sorting all the increasing subsequences of S by its length.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 100
2. 2 <= N <= 1000
3. 1 <= Ai <= 1 000 000 000
Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 100
2. 2 <= N <= 1000
3. 1 <= Ai <= 1 000 000 000
Output
For each test case, output the length of the second longest increasing subsequence.
Sample Input
321 141 2 3 451 1 2 2 2
Sample Output
132HintFor the first sequence, there are two increasing subsequence: [1], [1]. So the length of the second longest increasing subsequence is also 1, same with the length of LIS.
思路:若最长上升子序列唯一,则为LIS,否则为LIS-1
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 1010int a[N];int dp[N],num[N];int main(){ int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&a[i]); num[i]=1; dp[i]=1; } int maxn=1; for(int i=1; i<n; i++) { for(int j=0; j<i; j++) { if(a[i]<a[j]) continue; if(a[i]==a[j]) { dp[i]=max(dp[i],dp[j]); num[i]=max(num[i],num[j]); continue; } if(dp[i]<dp[j]+1) { dp[i]=dp[j]+1; num[i]=num[j]; } else if(dp[i]==dp[j]+1) num[i]+=num[j]; } maxn=max(maxn,dp[i]); } int s=0; for(int i=0; i<n; i++) if(dp[i]==maxn) s+=num[i]; if(s==1) printf("%d\n",maxn-1); else printf("%d\n",maxn); } return 0;}
0 0
- hdu 5087 Revenge of LIS II(LIS,思路)
- Revenge of LIS II (hdu 5087 LIS)
- hdu 5087 Revenge of LIS II(LIS)
- Revenge of LIS II(HDU 5087)
- hdu 5087 Revenge of LIS II dp
- 【DP】 HDU 5087 Revenge of LIS II
- HDU - 5087 Revenge of LIS II
- hdu 5087 Revenge of LIS II
- hdu 5087 Revenge of LIS II
- HDU 5087 Revenge of LIS II
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
- HDU 5078 Revenge of LIS II(dp LIS)
- hdu 5087 Revenge of LIS II(BestCoder Round #16)
- HDU 5087 Revenge of LIS II(次长上升子序列)
- hdu 5087 Revenge of LIS II lcs变形
- HDOJ 5087 Revenge of LIS II DP
- HDOJ-5087-Revenge of LIS II
- HDU5087 Revenge of LIS II (LIS变形)
- python学习笔记(一)
- 比想象中好用的Android单元测试——初探
- 支持向量机(四)
- ASP.NET页面之间传递值的几种方式
- 安装BEPL
- hdu 5087 Revenge of LIS II(LIS,思路)
- Spring Security(20)——整合Cas
- KMP算法深入理解
- C++的类型转换
- vs2010中配置OpenGL
- 支持向量机(五)SMO算法
- python学习笔记(二)
- LeetCode 258. Add Digits
- composer依赖管理工具