hdu 5087 Revenge of LIS II
2014-11-02 17:23
447 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test case, output the length of the second longest increasing subsequence.
[align=left]Sample Input[/align]
[align=left]Sample Output [/align]
题意:求次长子序列,LIS算法,dp。
代码:
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test case, output the length of the second longest increasing subsequence.
[align=left]Sample Input[/align]
3 2 1 1 4 1 2 3 4 5 1 1 2 2 2
[align=left]Sample Output [/align]
1 3 2
题意:求次长子序列,LIS算法,dp。
代码:
#include<iostream> #include<cstdlib> #include<stdio.h> #include<cstring> #include<algorithm> #define inf 1<<30 int dp[1100]; int cnt[1100]; int a[1100]; using namespace std; int main() { int t,n,i,j,flag,tmp; cin>>t; while(t--) { tmp=1; for(i=0;i<=1010;i++) { dp[i]=0; cnt[i]=0; } cin>>n; for(i=1;i<=n;i++) scanf("%d",&a[i]); a[n+1]=inf; //LIS for(i=1;i<=n+1;i++) { dp[i]=1; cnt[i]=1; for(j=1;j<i;j++) { if(a[i]>a[j]&&dp[i]<dp[j]+1) { dp[i]=dp[j]+1; cnt[i]=1; } else if(a[i]>a[j]&&dp[i]==dp[j]+1) cnt[i]++; } } //因为计算到n+1,最长子序列长度会多1,减掉 //如果以最后一个结尾的最长子序列不止一个,第二长的长度也是最长子序列长度 if(cnt[n+1]>1) printf("%d\n",dp[n+1]-1); else { flag=n+1; while(flag>0&&cnt[flag]==1) { for(j=flag-1; j>=1; j--) { if(dp[j]+1==dp[flag]&&a[j]<a[flag]) break; } flag=j; } //验证所有节点的cnt是不是都为1,如果都为1,最长子序列只有一个 则次长子序列长度为最长子序列长度-1 if(flag==0) printf("%d\n",dp[n+1]-1-1); else printf("%d\n",dp[n+1]-1); } } return 0; }
相关文章推荐
- HDU 5087 Revenge of LIS II
- hdu 5087 Revenge of LIS II 题解(DP) BestCoder Round #16 1002
- hdu 5087 Revenge of LIS II dp
- HDOJ 5087 Revenge of LIS II DP
- HDOJ-5087-Revenge of LIS II
- HDOJ 5087 Revenge of LIS II DP
- 【HDU 5021】 Revenge of kNN II
- hdu5087——Revenge of LIS II
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
- HDU 5087 Revenge of LIS II(次长上升子序列)
- Revenge of LIS II(HDU 5087)
- HDU - 5088 Revenge of Nim II
- hdu 5088 Revenge of Nim II(高斯消元)
- 【HDU】5088 Revenge of Nim II 高斯消元
- hdu 5088 Revenge of Nim II(高斯消元)
- BC D Revenge of kNN II hdu 5021
- 【DP】 HDU 5087 Revenge of LIS II
- HDU - 5087 Revenge of LIS II
- hdu 5087 Revenge of LIS II (DP)
- HDU 5021 Revenge of kNN II 线段树