hdu 5087 次长上升自序列的长度dp
2014-11-02 09:49
218 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5087
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 483 Accepted Submission(s): 161
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.
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
Output
For each test case, output the length of the second longest increasing subsequence.
Sample Input
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
Sample Output
1
3
2
HintFor 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.
Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 483 Accepted Submission(s): 161
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.
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
Output
For each test case, output the length of the second longest increasing subsequence.
Sample Input
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
Sample Output
1
3
2
HintFor 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.
/** hdu 5087 次长上升子序列的长度 要求:如果两个序列的长度不同或者是至少有一个位置,存在两个不同的数(大小相等的数出现在两位置算作两个不同的数) 解题思路:O(n^2)复杂度求最长上升子序列,并且每一个点都求出可以由它前面的几个点转移而来,用cont[i]记录, k为序列的最后一个数的位置,最后如果cont[k]>1或者有多个k位置,则最长和次长长度一样,否则减1. */ #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> using namespace std; int n; int a[1005],dp[1005],cont[1005]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); memset(cont,0,sizeof(cont)); for(int i=0;i<n;i++) { int maxx=0; dp[i]=1; for(int j=0;j<i;j++) if(maxx<dp[j]&&a[i]>a[j]) maxx=dp[j]; for(int j=0;j<i;j++) if(maxx==dp[j]&&a[i]>a[j]) { if(cont[j]==0) cont[i]++; else cont[i]+=cont[j]; } dp[i]=maxx+1; } /*for(int i=0;i<n;i++) { printf("(%d %d %d)\n",i,dp[i],cont[i]); }*/ int maxx=-1; int k; for(int i=0;i<n;i++) { if(maxx<dp[i]) { maxx=dp[i]; k=i; } } if(cont[k]>1) printf("%d\n",maxx); else { int flag=0; for(int i=0;i<n;i++) { if(dp[i]==maxx&&k!=i) { printf("%d\n",maxx); flag=1; break; } } if(flag==0) printf("%d\n",maxx-1); } } return 0; } /** 99 2 1 1 4 1 2 3 4 5 1 1 2 2 2 5 1 2 3 3 4 1 3 2 4 */
相关文章推荐
- HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?
- HDU 5569 长度为n的上升子序列个数d (大数模板+DP)
- HDU 1087 Super Jumping! Jumping! Jumping!(最长上升子序列,dp)
- HDU 5087 Revenge of LIS II(次长上升子序列)
- HDU 5087(DP 次最长子序列)
- HDU Problem C [ 最长上升子序列 求和]——基础dp模板题
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)
- DP_简单DP最长上升子序列[n*lg(n)](HDU_1025)
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)
- DP 动态规划 Problem B 1002 求最长上升子序列的长度
- HDU-1087 Super Jumping! Jumping! Jumping! (线性dp 上升子序列最大和)
- hdu 5087 次长升序串的长度
- hdu 5125 二分nlogn求最长上升子序列(LIS)+dp
- HDU1087--Super Jumping! Jumping! Jumping! (DP动态规划最大上升子序列的和)
- HDU 5748 最长上升子序列的长度nlogn(固定尾部)
- hdu 5087 次长升序串的长度
- HDU 1160 FatMouse's Speed(最长上升子序列长度及其路径)
- hdu 1087 最大上升子序列的和(dp或线段树)
- HDU 1257 最少拦截系统(最长上升子序列 动态规划(DP))
- hdu 1069 Monkey and Banana(类似最长上升子序列,dp)