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 ...
随机推荐
- 洛谷P1004 方格取数
网络流大法吼 不想用DP的我选择了用网络流-- 建模方法: 从源点向(1,1)连一条容量为2(走两次),费用为0的边 从(n,n)向汇点连一条容量为2,费用为0的边 每个方格向右边和下边的方格连一条容 ...
- React基础知识点全解
• propTypes.defaultProps 作为 properties 定义,也可以在组件外部通过键值对方式进行设置. • 设置组件初始的 state不支持 getIniti ...
- jquery获得url的get参数
只是用了第一种方法,简单好用直接传入想要获取的参数名,即可返回参数值 function GetQueryString(name) { var reg = new RegExp("( ...
- mysql5.7官网直译SQL语句优化--select语句优化
8.2 sql语句优化 大致内容如下: 8.2.1:SELECT语句的优化 8.2.2:优化子查询,派生表和试图引用 8.2.3:优化INFORMATION_SCHEMA查询 8.2.4:优化数据改变 ...
- [using_microsoft_infopath_2010]Chapter3 表单设计基础:使用InfoPath布局,控件,和视图
本章概要 1.使用InfoPath的布局工具构建吸引人的表单 2.使用InfoPath表格工具 3.在表单上添加字段和控件 4.使用section和container组织表单里的控件 5.在一个表单上 ...
- awk双文件互相匹配查找
awk双文件互相匹配查找 要求: 文件a: 10/05766798607,11/20050325191329,29/0.1,14/05766798607 10/05767158557,11/200 ...
- div在父集高度未知的情况下垂直居中的方法
父集高度未知,子集高度已知: 可以使用弹性盒来解决: justify-content属性定义了项目在主轴上的对齐方式. align-items属性定义项目在交叉轴上如何对齐.
- sicily 1342 开心的金明 (动规)
刷一下简单的背包问题 以下为代码: //1342. 开心的金明 #include <iostream> using namespace std; #define MAX(a,b) a> ...
- POJ 1948 DP
题意:给你n个木棍(n<=40)每个木棍长度<=40,问用上所有的木棍拼成的三角形的面积的最大值,并输出面积*100的值(不四舍五入) 如果没有解,输出-1. 思路: 背包判断可达性. f ...
- navigator.clipboard 浏览器原生剪贴板
浏览器原生剪贴板 navigator.clipboard 写入 navigator.clipboard.writeText navigator.clipboard.writeText('Linr Te ...