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

Problem Description
In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. 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.

Input
The first line contains a single integer T, indicating the number of test cases.

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

Output
For each test case, output the length of the second longest increasing subsequence.
Sample Input
3 2 1 1 4 1 2 3 4 5 1 1 2 2 2
Sample Output
1 3 2

Hint

For 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.

Source
BestCoder Round #16
#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;
}

posted on 2017-06-07 21:24 mthoutai 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/6959130.html

HDOJ 5087 Revenge of LIS II DP相关推荐

  1. BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

  2. Longest Increasing Subsequence(LIS入门dp)

    http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K D ...

  3. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  4. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  5. 一中OJ #1438 航线设计 | LIS 序列DP | 解题报告

    一中OJ | #1438 航线设计 时限 1000MS/Case 内存 64MB/Case 题目描述 有一个国家被一条河划分为南北两部分,在南岸和北岸总共有N对城镇,每一城镇在对岸都有唯一的友好城镇. ...

  6. #1560 : H国的身份证号码II(dp+矩阵快速幂)

    #1560 : H国的身份证号码II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 H国的身份证号码是一个N位的正整数(首位不能是0).此外,由于防伪需要,一个N位正整 ...

  7. P2782友好城市(LIS,动态规划dp)C语言版

    题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.每对友好城市都向政府申请在河上开辟一条直线航 ...

  8. 重复T次的LIS的dp Codeforces Round #323 (Div. 2) D

    http://codeforces.com/contest/583/problem/D 原题:You are given an array of positive integers a1, a2, . ...

  9. HDOJ 1002 A + B Problem II 解题报告

    今天研究 1002,,,搞得半死,虽说是一道简单的题目,真正做起来绝不是很容易的... A + B Problem II Time Limit: 2000/1000 MS (Java/Others)  ...

最新文章

  1. ScottGu之博客翻译-LINQ to SQL第四部分,更新数据库 LINQ to SQL (Part 4 - Updating our Database)...
  2. android 读取manifest的数据
  3. 【计算机图形学】实验:VB.net环境下的综合绘图与交互技术案例教程
  4. 程序员吐槽_产品经理吐槽大会,程序员勿入
  5. hdu 1564 Play a game
  6. 数据结构相关代码-简介
  7. 织梦生成的html路径,织梦dedecms安装在子目录网页生成在根目录如何设置
  8. [转载]C#获取DLL的目录
  9. Kernel Samepage Merging | 同页合并
  10. DockPanel的使用
  11. socket与http的区别
  12. 统计与分布之伯努利分布与二项分布
  13. 虚拟主机共享IP地址
  14. vue Elementui 引入第三方icon(阿里矢量库)
  15. 两年卖超2000万支,80%增长来自用户口碑,口腔护理品牌“贝医生”做了什么?
  16. c语言排序算法插入法,C语言中冒泡法、选择法、插入法三种常见排序算法分析.doc...
  17. php 微信全员分销_php筹建微信企业分销网站常见功能
  18. c语言中if( k1)的含义,C语言:我的按键程序K1键按下没有反应,其他两个都有反应...
  19. mysql全文查询中文_MySQL-中文全文检索
  20. aloge alogw alogi alogd alogv

热门文章

  1. php 在模板中赋值数组变量,PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例...
  2. androidstudio 评论栏_android-studio 添加没有菜单栏和actionBar的模板
  3. visual studio 自带的 宏 没反应_横山宏科幻动漫模型:插图造型怪异的胡佛战车欣赏...
  4. IBM MQ的常用命令
  5. 分布式大数据多维数据分析(olap)引擎kylin[转]
  6. Delphi 7生成XML
  7. ERC223以太坊通证标准
  8. 17. Gradle编译其他应用代码流程(五) - 设置Task过程
  9. 基于Linux的嵌入式文件系统构建与设计
  10. 《随机出题软件》《随机分队软件》源码(Windows API)