hdu5087 Revenge of LIS II (dp)
只要理解了LIS,这道题稍微搞一下就行了。
求LIS(最长上升子序列)有两种方法:
1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度。dp[i]最少也得是1,就初始化为1,则dp[i]=max(dp[i],dp[j]+1)(其中j<i且a[j]<a[i])。
int gao()
{
int ans=;
for(int i=;i<n;i++)
{
dp[i]=;
for(int j=;j<i;j++)
{
if(a[j]<a[i])
{
dp[i]=max(dp[i],dp[j]+);
}
}
ans=max(ans,dp[i]);
}
return ans;
}
O(n^2)
2.O(n*lgn)算法:有这样一个性质:如果上升子序列长度相同,那么最末尾最小的那种在之后的比较中最有优势(有点贪心的味道)。根据这个,设dp[i]为长度为i+1的上升子序列中末尾元素的最小值。开始dp全初始化为INF,每次更新时前面的dp都是排好序的,所以就可以二分来找。
int gao()
{
fill(dp,dp+n,INF);
for(int i=;i<n;i++)
{
*lower_bound(dp,dp+n,a[i])=a[i];
}
ans=lower_bound(dp,dp+n,INF)-dp;
}
O(n*lgn)
对于本题,要求输出第二长子序列的长度。那么就找LIS,如果LIS只有一种情况可以得到,那么输出LIS-1,否则输出LIS。
用num数组来记录能达到当前长度的方法数。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
const LL mod=;
int T,n,a[maxn],dp[maxn],num[maxn];
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int ans=;
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<n; i++)
{
dp[i]=;
num[i]=;
for(int j=; j<i; j++)
{
if(a[j]<a[i])
{
if(dp[j]+>dp[i])
{
num[i]=num[j];
dp[i]=dp[j]+;
}
else if(dp[j]+==dp[i])
{
num[i]+=num[j];
}
}
}
if(dp[i]>ans)
{
ans=dp[i];
}
}
int num1=;
for(int i=;i<n;i++)
{
if(dp[i]==ans)
{
num1+=num[i];
}
}
if(num1==)
{
printf("%d\n",ans-);
}
else
{
printf("%d\n",ans);
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
hdu5087 Revenge of LIS II (dp)的更多相关文章
- 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 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU5087 Revenge of LIS II (LIS变形)
题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...
- hdu 5087 Revenge of LIS II (DP)
题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 0 ...
- hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- hdu 5087 Revenge of LIS II
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
随机推荐
- PyQt4 UI设计和调用 使用eric6
使用工具eric6 安装包地址: http://eric-ide.python-projects.org/eric-download.html 1.需要安装python和pyqt为前提,然后安装eri ...
- C#中字符串的内存分配与驻留池
完全引用http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html 驻留池:是一张记录了所有在代码中使用字面量声明的字符串实例的引用 ...
- 流量监控系统---storm集群配置
1.集群部署的基本流程 集群部署的流程:下载安装包.解压安装包.修改配置文件.分发安装包.启动集群 注意: 所有的集群上都需要配置hosts vi /etc/hosts 192.168.223.20 ...
- pyhton3 configparser模块
1 #!/usr/bin/env python 2 # coding=utf-8 3 __author__ = 'Luzhuo' 4 __date__ = '2017/5/26' 5 # config ...
- MongoDB环境配置
在官网上下载MongoDB可执行文件安装在电脑上后,想要运行需先安装路径下新建一个data文件夹,再在里面新建db文件夹用户存放数据库文件和相关配置. 在bin目录里面运行命令行: 这样MongoDB ...
- python的常用的内置函数
使用内置函数的好处:简单,快速. 1.zip():以多个序列为参数,返回元祖列表. 长度:在多个序列长度不一时,以最短的为准. 常见用途:构建多参数列表,构建字典. 2.map():在python2旧 ...
- 1.mysql导论
虽然之前用过mysql一年多,但大多只是会用,深入了解的不多.所以想利用平时时间 系统的总结总结. 一.什么是数据库:(数据库软件) 1).什么是数据库(软件):数据库(DB:DataBase ...
- 【P2774】方格取数问题(贪心+最大流,洛谷)
首先,我们要读懂这道题,否则你会和我一开始产生一样的疑问,把所有的数都取走剩下一个最小的不就可以了么???然后我们发现样例完全不是这么回事.题目中所说的使相邻的两个数没有公共边,是指你去走的数,也就是 ...
- java深入探究08-连接池,分页
1.连接池 1)自定义连接池 思路:定义一个类Pool->添加4个属性(最大连接数,初始化连接数,当前连接数,用来存放连接对象的LinkList集合对象)->定义一个createConne ...
- webpack 从0 手动配置
1. npm init 2. npm install -D webpack webpack-cli 3. 创建webpack入口文件( 默认 webpack.config.js 可以通过 webpac ...