The All-purpose Zero

题目连接:

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

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.

Hint

题意

给你n个数,其中0可以变成任何数,问你最长上升子序列可以是多少

题解:

考虑dp[i]表示长度为i的lis的最后一个数是多少。

遇到0的时候,就相当于dp[i]原来等于v,现在dp[i+1]=v+1了,这样的转移。

其实就相当于整体向右平移。

这个我们就在线段树上预先留很多个位置,让起点向左边平移就好了嘛,嘿嘿嘿。

代码

#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 1e5 + 15;
const int inf = 0x3f3f3f3f;
int a[maxn],len[maxn],N,tot,C,Midpoint; struct Sgtree{
struct node{
int l , r , maxv , lazy; void Update( int x ){
lazy += x;
maxv += x;
}
}tree[maxn << 3]; void ReleaseLabel( int o ){
if( tree[o].lazy ){
tree[o << 1].Update( tree[o].lazy );
tree[o << 1 | 1].Update( tree[o].lazy );
tree[o].lazy = 0 ;
}
} void Maintain( int o ){
tree[o].maxv = max( tree[o << 1].maxv , tree[o << 1 | 1 ].maxv );
} void Build(int l , int r , int o){
tree[o].l = l , tree[o].r = r , tree[o].maxv = inf , tree[o].lazy = 0;
if( r > l ){
int mid = l + r >> 1;
Build( l , mid , o << 1 );
Build( mid + 1 , r , o << 1 | 1 );
Maintain( o );
}else if( l <= Midpoint ) tree[o].maxv = -5*N - 5;
} void Modify( int ql , int qr , int v , int o ){
int l = tree[o].l , r = tree[o].r;
if( ql <= l && r <= qr ) tree[o].Update( v );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( ql <= mid ) Modify( ql , qr , v , o << 1 );
if( qr > mid ) Modify( ql , qr , v , o << 1 | 1 );
Maintain( o );
}
} void Change( int x , int v , int o ){
int l = tree[o].l , r = tree[o].r;
if( l == r ) tree[o].maxv = min( tree[o].maxv , v );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( x <= mid ) Change( x, v , o << 1 );
else Change( x, v , o << 1 | 1 );
Maintain( o );
}
} void Search( int v , int o ){
int l = tree[o].l , r = tree[o].r;
if( l == r ) tree[o].maxv = min( tree[o].maxv , v );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( tree[o << 1].maxv >= v ) Search( v , o << 1 );
else Search( v , o << 1 | 1 );
Maintain( o );
}
} int Ask( int x , int o ){
int l = tree[o].l , r = tree[o].r;
if( l == r ) return tree[o].maxv;
else{
int mid = l + r >> 1;
ReleaseLabel( o );
int v;
if( x <= mid ) v = Ask( x , o << 1 );
else v = Ask( x , o << 1 | 1 );
Maintain( o );
return v;
}
} }Sgtree; void solve( int cas ){
Midpoint = N + 5;
Sgtree.Build( 1 , N * 2 + 500 , 1 );
int extra = 0;
rep(i,1,N){
int x = a[i];
if( x == 0 ){
++ Sgtree.tree[1].lazy; // 右移一位
Sgtree.Change( -- Midpoint , -5*N - 5 , 1 );
++ extra;
//cout << "i is " << i << endl;
//rep(j,0,N+extra-1) pf("len[%d] is %d\n" , j , Sgtree.Ask(Midpoint+j,1));
//cout << "---------" << endl;
}else Sgtree.Search( x , 1 );
}
int mx = 0;
rep(i,0,N+extra-1) if( Sgtree.Ask( Midpoint + i , 1 ) <= 10000000 ) mx = max( mx , i );
pf("Case #%d: %d\n",cas,mx);
} int main(int argc,char *argv[]){
int T=read(),cas=0;
while(T--){
N=read();
rep(i,1,N) a[i] = read();
solve( ++ cas );
}
return 0;
}

hdu 5773 The All-purpose Zero 线段树 dp的更多相关文章

  1. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  5. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  6. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  7. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  10. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

随机推荐

  1. History of Monte Carlo Methods - Part 1

    History of Monte Carlo Methods - Part 1 Some time ago in June 2013 I gave a lab tutorial on Monte Ca ...

  2. 浅谈 js 对象 toJSON 方法

    前些天在<浅谈 JSON.stringify 方法>说了他的正确使用姿势,今天来说下 toJSON 方法吧.其实我觉得这货跟 toString 一个道理,他是给 stringify 方法字 ...

  3. ASP.NET自定义服务器控件

    本文通过创建一个最简单的服务器控件,演示开发服务器端控件的流程. 文章内容整理自MSDN的编程指南,原文地址在文章末尾的资源中. 本文创建一个简单的服务器控件,名为 RedLabel. 它的使用方式为 ...

  4. iOS 提交应用过程出现的错误及#解决方案#images can't contain alpha channels or transparencies

        本文永久地址为http://www.cnblogs.com/ChenYilong/p/3977542.html ,转载请注明出处.    当你试图通过<预览>"导出&qu ...

  5. win8开wifi共享无法使用的问题解决办法

    相信现在不少人都安装了windows8操作系统,因为windows8这个全新的操作系统用起来 确实挺强大,包括漂亮的开始屏,但是不得不说这个系统的兼容性还是有待提高,所以win8我的 装了又卸,卸了又 ...

  6. JDK1.8源码hashMap

    一.概念 允许key为null,value为null:线程不安全:不保证映射的顺序:迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数) ...

  7. iis6 和iis7s上整个网站重定向

    iis6 和iis7s上整个网站重定向   重定向作用: 重定向(Redirect)就是通过各种方法将各种网络请求重新定个方向转到其它位置.举例说明:就像我XX公司,之前用的网络域名是“www.bb. ...

  8. 证书(Certificate)与描述文件(Provisioning Profiles)

    在使用脚本xcodebuild自动打包的时候,会用到签名证书和描述文件,我在编译的时候搞了好长时间才搞明白,下面介绍如何得到正确配置. 证书:证书是用来给应用程序签名的,只有经过签名的应用程序才能保证 ...

  9. 洛谷P3383线性筛素数

    传送门 代码中体现在那个 $ break $ $ prime $ 数组 中的素数是递增的,当 $ i $ 能整除 $ prime[j ] $ ,那么 $ iprime[j+1] $ 这个合数肯定被 $ ...

  10. unmappable character for US-ASCII

    编码错误编译时加-encoding UTF-8即可 :javac -encoding UTF- file.java //