HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法
请输出最少需要修改多少个元素。
每一组数据:
第一行输入一个N(1≤N≤105),表示数列的长度
第二行输入N个数A1,A2,...,An。
每一个数列中的元素都是正整数而且不超过106。
Case #i:
然后输出最少需要修改多少个元素。
2
1 10
3
2 5 4
#include <cstdio>
#include <cstring>
using namespace std;
int c[],n,t,len,a;
int find(int l,int r,int num){
while(l<=r){ int mid=(l+r)>>; if(c[mid]<=num) l=mid+; else r=mid-; }
return l;
}
int main(){
scanf("%d",&t);
for(int k=;k<=t;k++){
printf("Case #%d:\n",k);
scanf("%d",&n);
len=; memset(c,,sizeof(c));
for(int i=;i<=n;i++){
scanf("%d",&a);
if(i==){ c[len]=a-i; continue; }
int pos=find(,len,a-i);
c[pos]=a-i;
if(len<pos) len=pos;
} printf("%d\n",n-len);
} return ;
}
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 1000000+5
int c[N],a[N],b[N],n,t,nmax;
void modify(int x,int num){while(x<=nmax)c[x]=max(c[x],num),x+=x&-x;}
int query(int x){int s=;while(x>)s=max(c[x],s),x-=x&-x;return s;}
int main(){
scanf("%d",&t);
for(int k=;k<=t;k++){
printf("Case #%d:\n",k);
scanf("%d",&n);
memset(c,,sizeof(c)); nmax=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
a[i]-=i; b[i]=a[i];
}
sort(b,b+n);
int size=unique(b,b+n)-b;//离散化
for(int i=;i<n;i++){
a[i]=lower_bound(b,b+size,a[i])-b+;
nmax=max(a[i],nmax);//离散化以后再去标记最大值
}
for(int i=;i<n;i++)modify(a[i],query(a[i])+);//前缀最大值+1
printf("%d\n",n-query(nmax));
} return ;
}
HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法的更多相关文章
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- HDU 6348 序列计数 (树状数组 + DP)
序列计数 Time Limit: 4500/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
随机推荐
- Oracle运维必修内功:前瞻性运维理念
数据库是商业的灵魂和大脑,作为核心IT业务模块,数据库的重要性毋庸置疑.管理数据库在做好系统前期部署后,在系统持续运行中,需要时刻关注系统自身和环境等对于运行可能产生的潜在影响和可能的危险,并及时予以 ...
- (转)Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds
仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 S ...
- 统计字符 比如aaabbcca----3a2b1c1a
package Demo; import java.util.Scanner; /** * Created by chengpeng on 16/11/3. */ /* idea command+al ...
- R - 一只小蜜蜂...(第二季水)
Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. ...
- Java并发编程之volatile变量
volatile提供了弱同步机制,用来确保将变量更新通知到其它线程.volatile变量不会被缓存在寄存器中或者对其它处理器不可见的地方,因此在读取volatile变量时总会返回最新写入的值.可以想象 ...
- jQuery的事件和动画
1.animate的应用 animate(params,[duration],[easing],[callback]) 参数实例: params: {width:"20%" ...
- 模拟Spring依赖注入
通过读取xml文件,利用反射动态加载类和方法,其实就是spring的注入机制模拟,可以清晰的看出整个运行流程 1.配置文件 applicationContext.xml <beans> & ...
- spring postconstruct
package com.jdw.service.impl; import java.util.List; import javax.annotation.PostConstruct; import o ...
- VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE)
VC (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE) 问题记录 VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DE ...
- session_cache_limiter 及 session 常见问题
我点击后退按钮,为什么之前填写的东西不见 这是因为你使用了session. 解决办法: PHP代码:-------------------------------------------------- ...