---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.
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
參考链接:http://blog.csdn.net/acvay/article/details/40686171
比赛时没有读懂题目開始做结果被hack了,后来一直想nlogn的方法,无果。以后应该会想出来,以后再贴那种方法代码
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #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;j<i;j++) { if(a[i]<=a[j]) continue; if(dp[j]+1>dp[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; }