HDU 5078 Revenge of LIS II(dp LIS)
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.
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.
參考链接: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;
}
HDU 5078 Revenge of LIS II(dp LIS)的更多相关文章
- HDU 1002 A + B Problem II(AC代码)
#include <stdio.h> #include <string.h> #define MAX 1009 int main() { },b[MAX]={}; ,z=,r= ...
- HDU 1087 Super Jumping! Jumping! Jumping! (DP+LIS)
题意:给定一个长度为n的序列,让你求一个和最大递增序列. 析:一看,是不是很像LIS啊,这基本就是一样的,只不过改一下而已,d(i)表示前i个数中,最大的和并且是递增的, 如果 d(j) + a[i] ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- LIS问题(DP解法)---poj1631
题目链接:http://poj.org/problem?id=1631 这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence. 以下介绍LIS的解法 ...
- HDU——1027Ignatius and the Princess II(next_permutation函数)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU 4669 Mutiples on a circle (DP , 统计)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个环,每个点是一个数字,取一个子串,使 ...
- HDU 1207 汉诺塔II (递推)
经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...
- Unique Paths II (dp题)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- HDU 1002 A + B Problem II (大数加法)
题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...
随机推荐
- Eclipse开启代码自动提示功能
Eclipse代码里面的代码提示功能默认是关闭的,只有输入“.”的时候才会提示功能,用vs的用户可能不太习惯 这种,vs是输入任何字母都会提示,下面说一下如何修改eclipse配置,开启代码自动提示功 ...
- oc14--匿名对象
// // main.m // 匿名对象 #import <Foundation/Foundation.h> #import "Person.h" #import &q ...
- 理解和配置 Linux 下的 OOM Killer【转】
本文转载自:http://www.vpsee.com/2013/10/how-to-configure-the-linux-oom-killer/ 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉 ...
- 【POJ 3696】 The Luckiest number
[题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...
- Node.js:Buffer
ylbtech-Node.js:Buffer 1.返回顶部 1. Node.js Buffer(缓冲区) JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文 ...
- C# Log4Net简单使用方法
log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 使用log4net,一个很明显的好处 ...
- springboot与mybatis集成
1)添加依赖 <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId& ...
- Python3没有dict.has_key方法
最近开始学习Python,安装上最新的Python3.3.3照书敲了一个小程序结果报错 'dict' object has no attribute 'has_key' 上网查也找不到解决办法,后来发 ...
- Static variables in JavaScript
function MyClass () { // constructor function var privateVariable = "foo"; //NO:obj.privat ...
- svn SSL 错误:Key usage violation in certificate has been detected
CentOS/RHEL yum 安装的 subversion 是 1.6.11 版本,连VisualSVN服务器时会有"Key usage violation"的错误 将subve ...