程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 5078 Revenge of LIS II(dp LIS)

HDU 5078 Revenge of LIS II(dp LIS)

編輯:C++入門知識

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

Source BestCoder Round #16
Recommend

參考鏈接:http://blog.csdn.net/acvay/article/details/40686171


比賽時沒有讀懂題目開始做結果被hack了,後來一直想nlogn的方法,無果,以後應該會想出來,以後再貼那種方法代碼


#include
#include
#include
#include
#include
#include
#include
#include

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 10005

int a[N],dp[N],c[N];
int n;

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);

        int ans=0;
        for(i=1;i<=n;i++)
		{
			dp[i]=1;
			c[i]=1;
			for(j=1;jdp[i])
			   {
			   	 dp[i]=dp[j]+1;
			   	 c[i]=c[j];
			   }
			   else
				if(dp[j]+1==dp[i])
				 c[i]=2;
			}
			if(dp[i]>ans)
				ans=dp[i];
		}
		j=0;
		for(i=1;i<=n;i++)
			if(dp[i]==ans)
		{
			j+=c[i];
			if(j>1)
				break;
		}
		if(j>1)
			printf("%d\n",ans);
		else
			printf("%d\n",ans-1);
	}
	return 0;
}




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved