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. ef core 数据类型 && 表字段名设置

    HasColumnType HasColumnType是指定字段类型 [Column(TypeName = "decimal(18, 2)")] public decimal Mo ...

  2. GlyphRun 对象和 Glyphs 元素简介

    原文 GlyphRun 对象和 Glyphs 元素简介 GlyphRun 简介 Windows Presentation Foundation (WPF) 提供高级的文本支持包括直接访问的标志符号级标 ...

  3. JS window下面的对象

    ) •Js脚本一执行就会访问服务器.超链接诶还需要点击. getElementById(), (非常常用),根据元素的Id获得对象,网页中id不能重复.也可以直接通过元素的id来引用元素,但是有有效范 ...

  4. 一个让业务开发效率提高10倍的golang库

    一个让业务开发效率提高10倍的golang库 此文除了是标题党,没有什么其他问题. 这篇文章推荐一个库,https://github.com/jianfengye/collection. 这个库是我在 ...

  5. WPF中的资源(一) - 静态资源和动态资源

    原文:WPF中的资源(一) - 静态资源和动态资源 WPF中,每个界面元素都含有一个名为Resources的属性,其存储的是以"键-值"对形式存在的资源,而其子级元素在使用这些资源 ...

  6. web-dev-server配置

    webpack-dev-server是一个小型的Node.js Express服务器,它使用webpack-dev-middleware来服务于webpack的包,除此自外,它还有一个通过Sock.j ...

  7. CLSRSC-400: A system reboot is required to continue installing.

    I try to install oracle database 12c RAC on the RedHat 7.3,when I execute the script '/u01/app/12.2. ...

  8. ML:吴恩达 机器学习 课程笔记(Week5~6)

    Neural Networks: Learning Advice for Applying Machine Learning Machine Learning System Design

  9. Qt 设置背景图片3种方法(三种方法:QPalette调色板,paintEvent,QSS)

    方法1. setStylSheet{"QDialog{background-image:url()"}}  //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...

  10. QtStaticBuildScript(会有这么容易)

    https://github.com/dankrusi/QtStaticBuildScript