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 ...
随机推荐
- JS中Math函数基础
Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’
- Java二维码打印
http://blog.csdn.net/OnePersonTZ/article/details/66560513
- Xdoclet + Ant自己主动生成Hibernate配置文件
在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题. 近期接触了Xdoclet这个工具. 它实际上就是一个自己主动代码生成的工具.Xdoclet不能单独执行,必须搭配 ...
- Linux多线程实践(四 )线程的特定数据
在单线程程序中.我们常常要用到"全局变量"以实现多个函数间共享数据, 然而在多线程环境下.因为数据空间是共享的.因此全局变量也为全部线程所共同拥有.但有时应用程序设计中有必要提供线 ...
- 2 怎样解析XML文件或字符串
1 引用XML文件 2 使用XMLReader解析文本字符串 3 使用XMLReader方法读取XML数据 详细代码实现例如以下: //初始化一个XML字符串 String xmlString = @ ...
- 模拟实现Spring IoC功能
为了加深理解Spring 今天自己写了一个模拟的Spring.... 步骤: 1.利用jdom解析bean.xml(pull,sax也能够,我这里用了jdom) 2.先解析全部的<bean/&g ...
- zzulioj--1712--神秘的数列(水题)
1712: 神密的数列 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 122 Solved: 92 SubmitStatusWeb Board De ...
- Ubuntu下使用Deepin-wine的移植版安装qq微信等
title: Ubuntu下使用Deepin-wine的移植版安装qq微信等 toc: false date: 2018-09-18 16:12:49 categories: methods tags ...
- echarts中国地图
echarts中国地图效果图: =================== 需要引入echarts的js文件:(1.echarts.min.js:2.china.js) 下载地址: echarts.min ...
- WPF XAML
xmlns 在xml中专门用于声明名字控件, xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 是 ...