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为 ...
随机推荐
- Python之简单函数练习(Day30)
1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作 def modify_file(filename,old,new): import os with open(filen ...
- 使用新浪IP库获取IP详细地址
使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...
- PAT 天梯赛 L1-028. 判断素数 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...
- swift的值类型和引用类型
前言 最近在学设计模式中,发现 Swift 中的 struct,class 以及 enum 在一般的使用中能够做到互相替换,因此探究其背后的逻辑就十分有必要.而这一问题又引出了 Swift 中的值类型 ...
- OpenGL学习进程(10)第七课:四边形绘制与动画基础
本节是OpenGL学习的第七个课时,下面以四边形为例介绍绘制OpenGL动画的相关知识: (1)绘制几种不同的四边形: 1)四边形(GL_QUADS) OpenGL的GL_QUADS图 ...
- 20145210姚思羽《网络对抗》MSF基础应用实验
20145210姚思羽<网络对抗>MSF基础应用实验 实验后回答问题 1.用自己的话解释什么是exploit,payload,encode. exploit就是进行攻击的那一步 paylo ...
- 在IOS开发中,项目的目录结构如何搭建?
网上有很多关于IOS开发的学习资料.然而却很少有关于设计一个项目时,如何设计其目录结构?这对于自学IOS的程序猿们,无疑有诸多不利.接下来,我就简单的谈下真正在公司中,项目的目录结构如何搭建: 以上为 ...
- QGIS 编译
QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...
- Fatal error: cannot create 'R_TempDir'
[user@mgmt dir]$ R Fatal error: cannot create 'R_TempDir' [user@mgmt dir]$ ll -ad /tmp drwxrwxrwt. 2 ...
- web容器调用Filter和Servlet顺序学习
web容器调用Filter和Servlet顺序学习 一直对Filter和Servlet在哪里被web容器调用迷惑,后查看tomcat源码,揭开了其面纱.1. 下面是一个简单的时序图: 2. 对上 ...