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 ...
随机推荐
- leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...
- sersync+inotify实时备份数据
Sersync项目简介与框架 简介 Sersync项目利用inotify与rsync技术实现对服务器数据实时同步的解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rs ...
- FAQ:注册表_键值类型
在注册表中,“键值项数据”可分为下面三种类型. 字符串值(REG_SZ) 该值一般用来作为文件描述和硬件标志,可以是字母.数字,也可以是汉字,但它是长度固定的文本字符串,最大长度不能超过255个字符. ...
- ArcEngine栅格和矢量渲染(含可视化颜色带)
使用ArcEngine9.3开发的栅格和矢量的渲染. 开发环境:ArcEngine9.3,VS2008. 功能:栅格(拉伸和分级)和矢量(简单.唯一值.分级.比例)渲染. 开发界面如图所示. 图1 主 ...
- javascript - 工作笔记 (事件绑定二)
在上篇中的事件绑定方法,相信大家都看到过了. 不过这里有点小小的问题,这些方法,变量都是全局的,难免会与其他的库或者方法发生冲突而被覆盖,因此,我在这里做了小小的封装,如下: JavaScript ...
- javascript抽象工厂模式
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- 截取字符串一之slice
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Rikka with Graph(联通图取边,暴力)
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Mayor's posters(离散化线段树)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 54067 Accepted: 15713 ...
- Android的应用程序的异常处理2
1.自定义一个类(MaApp)继承Application 2.在清单文件中的Application选项菜单对应的name属性中添加MyApp 3.重写application中的onCreate方法 4 ...