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. WPF实现选项卡效果(1)——使用AvalonDock

    原文:WPF实现选项卡效果(1)--使用AvalonDock 简介 公司最近一个项目,软件采用WPF开发,需要实现类似于VS的选项卡(或者是浏览器的选项卡)效果.搜寻诸多资料后,发现很多同仁推荐Ava ...

  2. 如果你说最近在看《诛仙》,平时喜欢玩LOL,你就是在把自己往悬崖上推

    面试官可能会问你一些和技术看上去没有任何关系的问题,比如问你最近在看什么书,学习之余喜欢做什么,常去哪些网站之类的.如果你说最近在看<诛仙>,平时喜欢玩LOL,你就是在把自己往悬崖上推.实 ...

  3. byte[] 左移和右移

    public static class ex { public static byte[] RightShift(this byte[] ba, int n) { ) { return ba.Left ...

  4. 给Delphi程序添加版本信息(EXE和Dll)

    我们在用Delphi编译完程序,准备发布产品时,总希望随产品发布个性信息以标示产品的来源以及开发者等信息,就像windows的程序一样,使我们一看属性就知道他是微软的产品,这些在Delphi中是如何实 ...

  5. C# 利用 OpenCV 进行视频捕获 (笔记)

    原文:C# 利用 OpenCV 进行视频捕获 (笔记) 简介 这个项目是关于如何从网络摄像头或者视频文件(*.AVI)中捕获视频的,这个项目是用C#和OPENCV编写的. 这将有助于那些喜欢C#和Op ...

  6. tf.nn.softmax & tf.nn.reduce_sum & tf.nn.softmax_cross_entropy_with_logits

    tf.nn.softmax softmax是神经网络的最后一层将实数空间映射到概率空间的常用方法,公式如下: \[ softmax(x)_i=\frac{exp(x_i)}{\sum_jexp(x_j ...

  7. Raknet是一个基于UDP网络传输协议的C++网络库(还有一些其它库,比如nanomsg,fastsocket等等)

    Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务.通常情况下用于游戏,但也可以用于其它项目. Raknet有以下好处: 高性能 在同一台计算机 ...

  8. 长江存储32层3D NAND今年底准备好,预计2020年赶上世界前沿(有些ppt很精彩)

    集微网消息(文/刘洋)2017年1月14日,首届IC咖啡国际智慧科技产业峰会暨ICTech Summit 2017在上海隆重举行.本次峰会以“匠心独运 卓越创‘芯’”为主题,集结了ICT产业领袖与行业 ...

  9. [書訊]《.NET 依賴注入》 (2014-12-08 更新)

    书 名:.NET 依赖注入 页 数:235 格 式:PDF.EPUB.MOBI难易度:中阶至进阶出版日期:2014 年 12 月 8 日ISBN:9789574320684 简介 本书内容是关于 .N ...

  10. 初探 C# 8 的 Nullable Reference Types

    溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...