http://acm.hdu.edu.cn/showproblem.php?pid=5773

The All-purpose Zero

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.

 
题意:在一个序列里面求最长严格上升子序列,其中0可以任意变换为其他数。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 100005 int dp[N];
int num[N];
/*
0可以转化成任意整数,包括负数,
显然求LIS时尽量把0都放进去必定是正确的。
因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,
统计结果的时候再算上0的数量。为了保证严格递增,
我们可以将每个权值S[i]减去i前面0的个数,再做LIS,
就能保证结果是严格递增的。
*/
int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
int n;
scanf("%d", &n);
int cnt = ;
int zero = ;
for(int i = ; i <= n; i++){
int a;
scanf("%d", &a);
if(a == ) zero++;
else {
num[++cnt] = a - zero;
}
}
if(cnt == ) {
printf("Case #%d: %d\n", cas, n);
continue;
}
memset(dp, , sizeof(dp));
int tot = ;
dp[] = num[];
for(int i = ; i <= cnt; i++) {
if(num[i] > dp[tot]) dp[++tot] = num[i];
else {
int pos = lower_bound(dp + , dp + tot, num[i]) - dp;
dp[pos] = num[i];
}
}
printf("Case #%d: %d\n", cas, tot + zero);
}
return ;
}

HDU 5773:The All-purpose Zero(贪心+LIS)的更多相关文章

  1. hdu 5773 The All-purpose Zero 最长上升子序列+树状数组

    题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nl ...

  2. HDU 1257 最少拦截系统(贪心 or LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)   ...

  3. HDU 5773 The All-purpose Zero(O(nlgn)求LIS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...

  4. hdu 1257 最少拦截系统【贪心 || DP——LIS】

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

  5. HDU 5773 The All-purpose Zero (变形LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 0可以改变成任何数,问你严格递增的子序列最长是多少. 猜测0一定在最长上升子序列中用到,比如2 ...

  6. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

  7. HDU 5773 The All-purpose Zero 求LIS

    求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以 ...

  8. HDU 5773 The All-purpose Zero 脑洞LIS

    给定一个序列,里面的0是可以任变的.问变化后最长的LIS的长度 首先,0全部选上是不亏的.这个不知道怎么说,YY一下吧. 最关键的就是解决2 0 0 3 这种问题了. 注意到这个序列的LIS应该是3 ...

  9. hdu 5773 最长递增子序列 (nlogn)+贪心

    The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. Delphi Android ActivityManager(提供了接口, 利用它可以方便的对Memory, Processes, Task, Service 等进行管)

    ActivityManager:   对Activity交互提供了接口, 利用它可以方便的对Memory, Processes, Task, Service 等进行管理,. 这里对Delphi接口进行 ...

  2. C# 金额转为大写金额

    /// <summary> /// 金额转为大写金额 /// </summary> public class MoneyConvertChinese { /// <sum ...

  3. 使用Notepad++远程编辑Ubuntu上的源码

    简单搭建了在Windows上远程编辑Ubuntu Server 14.04上面源代码的环境,记录一下,给需要的人. Notepad++安装NppFTP 从插件菜单打开PluginManager,选中N ...

  4. Android 联系人导入导出(VCard格式)

    之前在Android Contact 导入导出 vcf格式(不依赖第三方库)记录了一下依赖Android sdk中的功能导入导出联系人(第一次做java项目内容,有些地方的记录是否正确,暂时我也不知道 ...

  5. 零元学Expression Blend 4 - Chapter 30 8个Expression Blend4的快捷

    原文:零元学Expression Blend 4 - Chapter 30 8个Expression Blend4的快捷 我针对工作区跟视窗的快捷键整理了八个Expression Blend4的快捷, ...

  6. 正则表达式-Csharp

    原文:正则表达式-Csharp 学习笔记:正则表达式 一. 正则表达式 正则表达式(Regex)是用来进行文本处理的技术,是语言无关的,在几乎所有语言中都有实现. 一个正则表达式就是由普通的字符及特殊 ...

  7. 搭建svn服务器&服务器客户端使用笔记

    参考借鉴文章:http://www.cnblogs.com/vijayfly/p/5711962.html 之前尝试着用git管理公司代码,但被一个问题困惑了很久无法解决,那就是git该如何只pull ...

  8. DBLINK学习

    1.连接本地scott用户查看拥有的表 [oracle@ORADG ~]$ sqlplus scott/tiger SQL> select * from tab; TNAME           ...

  9. UWP项目生成安装包远程安装在树莓派上

    原文: UWP项目生成安装包远程安装在树莓派上 哎,好纠结啊!如果这个名字写的太长,会显得太繁琐,如果写的短又好像说不清楚,我这语言表达水平实在是令人担忧啊!不过应该能够明白啥意思吧!因为对这个感兴趣 ...

  10. docker启动命令,docker重启命令,docker关闭命令

    启动        systemctl start docker 守护进程重启   sudo systemctl daemon-reload 重启docker服务   systemctl restar ...