HDU5087——Revenge of LIS II(BestCoder Round #16)

Revenge of LIS II

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
Hint
For 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.

题目大意:

    求第二长的绝对递增子序列的长度。

解题思路:

    错误思路:

        求出用于求最长绝对递增子序列的dp数组,sort之后输出dp[N-1]。

        未考虑到dp[N]可以有多种方式构成。eg:1 1 2 就应该输出2。

    正确思路:

        每次求dp[i]的时候,用c[i]记录有多少种情况来构成此最优解。

        求出ans=max(dp[1],dp[2]...dp[N]).

        在求出 sum=sum{ c[i] | dp[i]==ans }

        若sum!=1 说明最优解有多种可能的构成方式。输出ans即可。

        若sum==1 输出ans-1

Code:

 1 /*************************************************************************
 2     > File Name: BestCode#16_1002.cpp
 3     > Author: Enumz
 4     > Mail: [email protected]
 5     > Created Time: 2014年11月01日 星期六 17时44分05秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 5000
23 using namespace std;
24 int dp[MAXN];
25 long long a[MAXN];
26 int flag[MAXN];
27 int main()
28 {
29     int T;
30     cin>>T;
31     while (T--)
32     {
33         int N;
34         cin>>N;
35         for (int i=1;i<=N;i++){
36             scanf("%I64d",&a[i]);
37             dp[i]=1,flag[i]=1;
38         }
39         int ans=0;
40         for (int i=1;i<=N;i++)
41         {
42             for (int j=1;j<i;j++)
43                 if (a[j]<a[i])
44                 {
45                     if (dp[i]<dp[j]+1)
46                         dp[i]=dp[j]+1,flag[i]=flag[j];
47                     else if (dp[i]==dp[j]+1)
48                         flag[i]=2;
49                 }
50             if (ans<dp[i]) ans=dp[i];
51         }
52         int sum=0;
53         for (int i=1;i<=N;i++)
54             if (ans==dp[i]) sum+=flag[i];
55         if (sum>1)
56             printf("%d\n",ans);
57         else
58             printf("%d\n",ans-1);
59     }
60     return 0;
61 }
时间: 2024-10-29 10:46:20

HDU5087——Revenge of LIS II(BestCoder Round #16)的相关文章

hdu5087——Revenge of LIS II

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 444    Accepted Submission(s): 143 Problem Description In computer science, the longest increasing subsequence problem is to fi

HDU5086——Revenge of Segment Tree(BestCoder Round #16)

Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure

hdu 5086 Revenge of Segment Tree(BestCoder Round #16)

Revenge of Segment Tree                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 420    Accepted Submission(s): 180 Problem Description In comput

hdu5087 Revenge of LIS II (dp)

只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则dp[i]=max(dp[i],dp[j]+1)(其中j<i且a[j]<a[i]). int gao() { int ans=0; for(int i=0;i<n;i++) { dp[i]=1; for(int j=0;j<i;j++) { if(a[j]<a[i]) { dp[

HDU5087 Revenge of LIS II (LIS变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i]表示以i结尾的最长上升序列的长度,dp[i]表示到i的不同的最长的子序列的个数 然后最后判断最长的子序列的个数是否大于1是的话输出Max,否则输出Max-1 代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespac

HDOJ 5087 Revenge of LIS II DP

DP的时候记录下是否可以从两个位置转移过来.... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 393    Accepted Submission(s): 116 Problem Description In computer science, the longest increasing su

HDOJ 题目5087 Revenge of LIS II(第二长LIS)

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1195    Accepted Submission(s): 400 Problem Description In computer science, the longest increasing subsequence problem is to f

HDU 5087 Revenge of LIS II(次大递增子序列)

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1258    Accepted Submission(s): 423 Problem Description In computer science, the longest increasing subsequence problem is to f

HDU 5078 Revenge of LIS II(dp LIS)

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 po