Problem Description
?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.

Input
The first line contains an interger T,denoting the number of the test cases.(T <= 10)
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.

Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.

Sample Input
2
7
2 0 2 1 2 0 5
6
1 2 3 3 0 0

Sample Output
Case #1: 5
Case #2: 5

Hint
In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.

这道题的意思是,给你一个1到N的非顺序序列,其中的零可以改成任意数,问你,可以得到的最长的上升子序列(LIS)的长度有多长。

实际上,这道题的所有的零都可以用上,但是,能用上的零,必须要有空间给零来改变数值,所以,这是一个变化题。LIS有一种变化,就是说,给你一个序列,让你随便改变数字,得到一个LIS,问你最少能要改变多少个数字,这里的话,在求LIS的时候,需要考虑到要留给需要改变数字的空间,因为,LIS符合这个公式:a[i]-a[j]>=i-j,变换成a[i]-i>=a[j]-j,所以在求LIS之前会把每个数字都减去他的位置数。

而这道题,要预留出来的空间不是给已有的数字的,而是零,零可以改变成任何数字,并且在改变之前都是最小的,因此,这里,要在求LIS的时候,对于每个数,都要减去在他之前的零的个数。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#define maxn 100010
#define maxm 1000010
using namespace std;
int T,n,k,len,ans,cnt,l,counts;
int num[maxn];
int q[maxn];
int f[maxn];
int main()
{scanf("%d",&T);int cas=0;while(T--){scanf("%d",&n);memset(q,sizeof(q),0);memset(f,sizeof(f),0);for(int i=0;i<n;i++)  scanf("%d",&num[i]);for(int i=0;i<=n;i++)  q[i]=maxm;k=0;len=1;counts=0;q[0]=-maxm;for(int i=0;i<n;i++){if(num[i]==0){counts++;continue;}if(num[i]-counts>q[len-1]){q[len]=num[i]-counts;len++;}else{k=upper_bound(q,q+len,num[i]-counts)-q;q[k]=num[i]-counts;}}printf("Case #%d: %d\n",++cas,len+counts-1);}return 0;
}

HDU 5773 The All-purpose Zero(最长上升子序列)相关推荐

  1. HDU 1257 最少拦截系统【最长上升子序列】

    解题思路:可以转化为求最长上升子序列来做,还是可以用an与按升序排列后的an求LCS来做,为防止超时,用滚动数组优化一下就可以了. 最少拦截系统 Time Limit: 2000/1000 MS (J ...

  2. HDU 1087 Super Jumping! Jumping! Jumping! (最长上升子序列的变形,子序列值最大)

    题意 wsw获得了与小姐姐约会的机会,同时也不用担心wls会发现了,可是如何选择和哪些小姐姐约会呢?wsw希望自己可以循序渐进,同时希望挑战自己的极限,我们假定每个小姐姐有一个"攻略难度值& ...

  3. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

  4. HDU 1069 Monkey and Banana 最长上升子序列进阶(动态规划)

    HDU 1069(动态规划) Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  5. hdu 1159(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 思路:dp[i][j]表示s1从0~i-1,s2从0~j-1的最长公共子序列: 递推方程为:dp ...

  6. (动态规划 最长有序子序列)Monkey and Banana --HDU --1069

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1069 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. 最长上升子序列模板 hdu 1087 Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular i ...

  8. 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

    题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. 【HDU - 1257】最少拦截系统 (标解dp,贪心可过,最长上升子序列类问题)

    题干: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来 ...

  10. 三维最长上升子序列问题——HDU 1069 Monkey and Banana

    题目: A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a ...

最新文章

  1. Redis中五大数据结构的底层实现
  2. 软考之运筹学计算-车床问题
  3. web worker技术-js新线程
  4. git 更新_[技术分享T.191212]GitLab使用方法及git命令常见问题(不断更新)
  5. 前端学习(2682):重读vue电商网站2之前台启动
  6. Struts2中Session的使用
  7. 计算机设备 运维,计算机网络设备运维报告
  8. u盘锁电脑_如何给u盘设置密码 给u盘设置密码方法【步骤详解】
  9. Qt信号和槽的连接--发射信号而槽未响应
  10. HMC 命令行登陆设置
  11. [hdu5247]rmq+预处理
  12. 原画插画零基础自学|原画基础入门教程
  13. 瑞星杀毒软件2007 / 瑞星个人防火墙2007 - 免费试用
  14. java 换行符 ascii码_java换行怎么写
  15. Linux上搭建ElasticSearch-8.x集群以及安装Kibana(保姆级安装教程)
  16. phalcon 自动加载_Phalcon自动加载(PHP自动加载),phalcon加载php_PHP教程
  17. 威斯康星大学硕士计算机科学,2020年威斯康星大学密尔沃基分校排名TFE Times美国最佳计算机科学硕士专业排名第90...
  18. nodejs模块加载的猜想
  19. ie中的html分页控制,IE中HTML打印时实现每页都有的表头和打印分页
  20. 怎么用cmd强制修改密码

热门文章

  1. MATLAB 读取和显示 bin 文件数据
  2. JAVA并发编程的书籍及资料
  3. 还在用ListView?!OutMan!RecyclerView!
  4. 阙值,阈值,阀值,傻傻分不清
  5. python中的单引号和双引号有什么区别_python中单引号和双引号有什么区别
  6. linux下解压iso文件
  7. java中cleanup的使用_【Lombok注解】@Cleanup 自动资源管理:安全无困扰地调用close方法...
  8. 枕头里面加进这些小宝贝能治高血压!快告知你父母~
  9. Android动态logo,Android模仿拉勾网logo动画效果
  10. Express 介绍