HDU5087 Revenge of LIS II (LIS变形)
题目链接: pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087
题意:
求第二长的最长递增序列的长度
分析:
用step[i]表示以i结尾的最长上升序列的长度,dp[i]表示到i的不同的最长的子序列的个数
然后最后推断最长的子序列的个数是否大于1是的话输出Max,否则输出Max-1
代码例如以下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int step[maxn],dp[maxn], num[maxn];
int main ()
{
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
memset(step, 0, sizeof(step));
dp[0] = 1;
for(int i = 1; i <= n; i++)
scanf("%d", &num[i]);
int Max = -1;
for(int i = 1; i <= n; i++){
for(int j = 0; j < i; j++){
if(num[i] > num[j]){
if(step[j]+1 > step[i]){
step[i] = step[j] + 1;
dp[i] = dp[j];
}
else if(step[j] + 1 == step[i])
dp[i] += dp[j];
}
}
Max = max(Max, step[i]);
}
int flag = 0;
int ok = 1;
for(int i = 1; i <= n; i++){
if(step[i] == Max){
if(dp[i] > 1) ok = 0;
else{
if(flag) ok = 0;
flag = 1;
}
}
}
printf("%d\n", Max - ok );
}
return 0;
}
HDU5087 Revenge of LIS II (LIS变形)的更多相关文章
- 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 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
- HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)
Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players tak ...
- HDOJ 5088 Revenge of Nim II 位运算
位运算.. .. Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu5087 Revenge of LIS II (dp)
只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
随机推荐
- Django基础 - 修改默认SQLite3数据库连接为MySQL
Django数据库连接默认为SQLite3,打开setting.py可以看到数据库部分的配置如下: DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...
- new[] 到底做了什么?
#include<iostream> #include<cstdlib> using std::cout; using std::endl; using std::hex; c ...
- 2017-2018-2 20165301 实验四《Java面向对象程序设计》实验报告
2017-2018-2 20165301 实验四<Java面向对象程序设计>实验报告 一.Android Stuidio的安装测试 实验要求: 参考<Java和Android开发学习 ...
- grail开发环境的搭建
本文参考:Grails入门指南(第二版) 1. 下载jdk和Grail http://www.oracle.com/technetwork/java/javase/downloads/ http:// ...
- EVA:自定义字段实现
原文链接:http://blog.csdn.net/ytangdigl/article/details/70145910 前言 自定义字段又叫做“开放模型”,用户可以根据自已的需求,添加需要的字段,实 ...
- CVE-2012-0003 Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞 分析
[CNNVD]Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞(CNNVD-201201-110) Microsoft Wi ...
- C++有super关键字么?
很多人在学习Java之后,看到Java里面有super关键字,用来表示父类,那么C++里面有super关键字么? 答案是否定的.这也很容易理解,C++由于支持多继承,所以假设存在super关键字,那么 ...
- Java小实验之数据转换
看到有人问如图的程序,就去写了几行代码,顺便复习一下条件语句和ASCII码 import java.util.Scanner; public class test1 { public static v ...
- day5模块学习--shutil模块
shutil模块 高级的 文件.文件夹.压缩包 处理模块 os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说:绝对路径,父目录…… 但是,os文件的操作 ...
- 机器学习:KNN-近邻算法
一.理论知识 1.K近邻(k-Nearest Neighbor,简称KNN)学习是一种常用的监督学习. 工作机制:给定测试样本,基于某种距离度量找出训练集中与其最靠近的k个训练样本,然后基于这k个的信 ...