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

參考链接: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;
}

最新文章

  1. E-Business Suite 12.2 startCD 50 Install Fails with Fatal Error: TXK Install Service oracle.apps.fnd.txk.config.ProcessStateException: OUI process failed Cannot install Web Tier Utilities
  2. jacon
  3. for嵌套:1.兔子生兔子问题 2.打印菱形 3.求100以内质数的和
  4. linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程
  5. Android开源项目分类汇总【畜生级别】[转]
  6. mybatis0204 一对多查询
  7. python多线程机制
  8. AngularJS html5Mode与ASP.NET MVC路由共存
  9. AngularJS复习-----内置过滤器和内置服务
  10. 自托管websocket和webapi部署云服务器域名及远程访问
  11. Nuke Python module的使用
  12. indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
  13. 【Spring】详解spring事务属性
  14. MATLAB 地图工具箱 m_map 的安装和入门技巧(转)
  15. Android ProgressBar的使用
  16. c# net 使用反射为对象赋值
  17. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案
  18. 利用 TestNG 并行执行用例
  19. java基础06 switch
  20. Python学习进程(15)常用内置函数

热门文章

  1. Connect the Cities--hdoj
  2. HUdson2092整数解
  3. Windows下配置SVN服务器
  4. Hadoop MapReduce编程 API入门系列之统计学生成绩版本2(十八)
  5. kindeditor文本编辑器乱码中乱码问题解决办法
  6. BZOJ1060: [ZJOI2007]时态同步(树形dp 贪心)
  7. deepin下jdk和tomcat的安装教程
  8. Winform开发 如何为dataGridView 添加CheckBox列,并获取选中行
  9. ubuntu 安装 OpenCV-CUDA
  10. HTML5中新增加Input 的种类