HDU 5773:The All-purpose Zero(贪心+LIS)
http://acm.hdu.edu.cn/showproblem.php?pid=5773
The All-purpose Zero
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.
In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
#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)的更多相关文章
- hdu 5773 The All-purpose Zero 最长上升子序列+树状数组
题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nl ...
- HDU 1257 最少拦截系统(贪心 or LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5773 The All-purpose Zero(O(nlgn)求LIS)
http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 5773 The All-purpose Zero (变形LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 0可以改变成任何数,问你严格递增的子序列最长是多少. 猜测0一定在最长上升子序列中用到,比如2 ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- 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]) 用二分可以 ...
- HDU 5773 The All-purpose Zero 脑洞LIS
给定一个序列,里面的0是可以任变的.问变化后最长的LIS的长度 首先,0全部选上是不亏的.这个不知道怎么说,YY一下吧. 最关键的就是解决2 0 0 3 这种问题了. 注意到这个序列的LIS应该是3 ...
- hdu 5773 最长递增子序列 (nlogn)+贪心
The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- python3 无法使用flask.ext.* 报错的解决方法
python3 使用flask的一些扩展功能的导入方法是 from flask_bootstrap import Bootstrapfrom flask_moment import Momentfro ...
- 编解码TIFF图像
解码: // Open a Stream and decode a TIFF image Stream imageStreamSource = new FileStream("tulipfa ...
- 第一次做C++项目的一点小总结
本人原本一菜鸟java程序员,但业余涉猎还算广泛,稍微了解些C++内容.主要之前曾爱好过汇编语言.所以对于C语言能从汇编层面加以理解,C++则是在此基础上又加了面向对象.stl等一类东西嘛. 这次做了 ...
- 有未经处理的异常(在 xx.exe 中): 堆栈 Cookie 检测代码检测到基于堆栈的缓冲区溢出。
一般这个问题是数组越界. 我产生这个异常的代码是这句:memcpy(tmp_cert.byKey, m_row[2], 255); 255的长度超过了char数组tmp_cert.byKey的长度.
- 青云QingCloud宣布完成C轮融资,金额1亿美元
本轮融资由两家人民币基金领投,蓝驰创投跟投. 企业级基础云服务商青云QingCloud正式宣布完成金额为1亿美元的C轮融资,本轮融资由两家人民币基金领投,蓝驰创投跟投. 青云QingCloud公司成立 ...
- Using 3D engines with Qt(可以整合到Qt里,不影响)
A number of popular 3D engines can be integrated with Qt: Contents [hide] 1 Ogre 2 Irrlicht 3 OpenS ...
- SpringMVC与uploadify结合进行上传
uploadify是一个第三方js插件,支持多文件上传,拥有较为强大的上传功能 1.uploadify实现 下载其flash版本 http://www.uploadify.com/ 解压后将其内容区 ...
- Qt使用MinGW编译,如何忽略警告
Qt编译时经常出现以下警告: warning: unused parameter 'arg1' [-Wunused-parameter] warning: unused variable 'i' [- ...
- python中的基本数据类型之列表,元组
一.列表 1.什么是列表. 列表是python的基本数据类型之一,用[]来表示,可以存放各种数据类型(什么都能装,能装对象的对象) 列表相比于字符串,不仅可以存放不同类型的数据,而且可以存放大量的数据 ...