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
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.
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
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
1
3
2HintFor 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n;
int a[1100],len[1100];
bool db[1100]; int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
memset(len,0,sizeof(len));
memset(db,false,sizeof(db)); scanf("%d",&n);
int LIS=-1;
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
len[i]=1;
int mxlen=-1,mxp=-1;
for(int j=0;j<i;j++)
{
if(a[j]<a[i])
{
if(len[j]+1>mxlen)
{
mxlen=len[j]+1; mxp=j;
}
}
}
len[i]=max(len[i],mxlen);
int c1=0;
for(int j=0;j<i;j++)
{
if(a[j]>=a[i]) continue;
if(len[j]+1==len[i])
{
c1++;
if(db[j]==true)
{
db[i]=true;
}
}
}
if(c1>=2)
{
db[i]=true;
}
}
for(int i=0;i<n;i++)
{
if(LIS<len[i])
{
LIS=len[i];
}
}
bool flag=false;
int c1=0;
for(int i=0;i<n&&flag==false;i++)
{
if(LIS==len[i])
{
if(db[i]==true) flag=true;
c1++;
}
}
if(c1>=2||flag)
{
printf("%d\n",LIS);
}
else printf("%d\n",max(1,LIS-1));
}
return 0;
}
HDOJ 5087 Revenge of LIS II DP的更多相关文章
- hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
- hdu 5087 Revenge of LIS II (DP)
题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 0 ...
- hdu 5087 Revenge of LIS II
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
- hdu5087 Revenge of LIS II (dp)
只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- HDU5087——Revenge of LIS II(BestCoder Round #16)
Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...
- hdu5087——Revenge of LIS II
Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDOJ 5088 Revenge of Nim II 位运算
位运算.. .. Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
随机推荐
- b.WHERE使用中多行子查询(适用于in,any,all条件)
b.多行子查询(适用于in,any,all条件) //查询与SCOTT和MARTIN在同一个部门的同事的编号和名称 select empno,ename from emp where ...
- 创业笔记-Node.js入门之一个完整的基于Node.js的web应用
用例 我们来把目标设定得简单点,不过也要够实际才行: 用户可以通过浏览器使用我们的应用. 当用户请求http://domain/start时,可以看到一个欢迎页面,页面上有一个文件上传的表单. 用户可 ...
- ASP.NET-ActionResutlt
@RenderPage("Page_part1"); 上面的这种写法是错误的应该是 @RenderPage("Page_part1.cshtml"); // 要 ...
- maven常用插件功能
maven-dependency-plugin http://maven.apache.org/plugins/maven-dependency-plugin/ maven-dependency-pl ...
- 前端project师养成记:开发环境搭建(Sublime Text必备插件推荐)
为了让自己更像一个前端project师,决定从开发环境開始武装自己. 本文将介绍前段project师开发的一些利器的安装步骤,主要包含了: 1.Node.js的安装 2.Grunt的安装及经常使用插件 ...
- 11.使用boostregex遭遇无法打开libboost_regex-vc120-mt-sgd-1_62.lib的问题
通过Boost库可以在C++项目中使用正则表达式,配置好环境后链接过程出现”无法打开libboost_regex-vc120-mt-sgd-1_62.lib”的错误. 原因是按照官方生成lib的方法 ...
- asp.net mvc5 文件下载上传
下载:是通过点击a标签直接下载的方式,没有其他任何要求,在服务器上存在实体文件,不需要请求后台控制层 前段js: <a id="NF-DownLoad" authorize= ...
- Microsoft Edge 针对 Web 开发人员更新日志
Windows 10 build16215 之 Edge 新功能 新功能: 增加了对高级事件监听器的支持(“once”和“passive”)via 增加了对CSS object-fit/object- ...
- PostgreSQL 事务管理的MVCC
PostgreSQL的并发控制机制同时实现了多版本控制MVCC协议和两阶段封锁协议.实际采用哪种协议取决于所执行的语句类型. DML语句的并发控制将使用MVCC协议: DDL语句的并发控制基于标准的两 ...
- Linux 安装软件的几种方式
目录 几种安装方式 源代码编译安装 借助软件包管理器安装 二进制格式安装 总结 参考 几种安装方式 源代码编译安装 源代码包的安装一般为下载软件源代码,然后编译安装.常见的 C 程序软件的安装步骤是 ...