HDU 5773 The All-purpose Zero(树状数组)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5773
【题目大意】
给出一个非负整数序列,其中的0可以替换成任意整数,问替换后的最长严格上升序列长度。
【题解】
由于0的任意性,因此,最后的答案,将0全部选上,是最优的,因此我们只需知道在0之间可以插入几个其余的数,记s为0的前缀和,a为数列中的数,当两个数在这个答案序列中递增当且仅当ai-si>aj-sj。所以,我们用树状数组维护ai-si最长子序列,结果加上0的个数就是答案。
【代码】
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int base=100000,M=2000005;
int cnt,Cas=1,n,c[2000005],x,a[1000005],ans;
int add(int x,int num){while(x<M)c[x]=max(c[x],num),x+=x&-x;}
int sum(int x){int s=0;while(x>0)s=max(c[x],s),x-=x&-x;return s;}
int main(){
scanf("%d",&n);
while(~scanf("%d",&n)){
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++){scanf("%d",&a[i]);}
printf("Case #%d:",Cas++);ans=cnt=0;
for(int i=1;i<=n;i++){
if(a[i]==0){cnt++;continue;}
a[i]=a[i]-cnt+base;
int t=sum(a[i]-1)+1;
ans=max(ans,t);
add(a[i],t);
}printf(" %d\n",ans+cnt);
}return 0;
}
HDU 5773 The All-purpose Zero(树状数组)的更多相关文章
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
- hdu 1394 Minimum Inversion Number(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
- hdu 4911 求逆序对数+树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
随机推荐
- Linux 学习之防火墙配置
1.安装iptables防火墙 yum install iptables 2. 清除已有的iptables规则 iptables -F iptables -X iptables -Z 3.显 ...
- OC语法3——点语法,self关键字
点语法: 为了给程序员提供便捷,OC中也引入了点语法.不过它和Java中点语法的意义是完全不同的. 在Java中无论调用任何方法,还是访问public类型的成员变量都是用点语法(.号). 而在OC ...
- day10_python学习笔记_chapter13_面向对象编程
1. class NewClass(parent): def .... 如果没有父类, 则默认继承object类 2. 类属性访问(类似java中的静态属性和方法)直接用类名.属性名, 在python ...
- Apache新版配置虚拟主机的注意事项
1.关于没有默认索引文件(index.php或者index.html)时,列出目录:需要开启模块 LoadModule autoindex_module modules/mod_autoindex.s ...
- jQuery报错:
jQuery报错:Uncaught ReferenceError: $ is not defined 在使用jQuery的时候,发现有如下报错: Uncaught ReferenceError: $ ...
- 几家SIEM
HP Arcsight Imperva is a HP Business Partner. HP is the world's largest IT company, providing infras ...
- C++实现Http Post请求
参考资料: http://apps.hi.baidu.com/share/detail/39003388 http://blog.csdn.net/yc0188/article/details/474 ...
- python 读取utf8文件
有时候默认是gbk编码,但是要读取utf8文件,所以会出现decode 错误. 使用codecs模块: import codecs file = codecs.open('filename','r', ...
- 关于hasOwnProperty()方法的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ 1077 HDU 1043 Eight (IDA*)
题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...