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 ...
随机推荐
- winform —— listview创建表及简单的增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Extjs 6 MVC开发模式(一)
1.Extjs就绪函数 1)导入Extjs的CSS <link rel="stylesheet" type="text/css" href="r ...
- Oracle如何只显示重复数据,或不显示重复数据
思路: 一.对所有字段进行分组并计数 二.计数大于1的就显示 select * from 表名 group by 字段1,字段2 having count(*)>1 (显示重复)
- java面对对象 关键字this super
this:this是指向对象本身的一个指针,成员函数内部指向当前类的对象 其实this主要要三种用法: 1.表示对当前对象的引用! 2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是 ...
- mysql三张表关联查询
三张表,需要得到的数据是标红色部分的.sql如下: select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b. ...
- Java开发23中设计模式
设计模式(Design Patterns) 设计模式是一套被反复使用,多数人知晓的,经过分类编目的,代码设计经验的总结.使用设计模式是为了可重用代码,让代码更容易被他人理解,保证代码的可靠性.毫无疑问 ...
- 如何处理JS与smarty标签的冲突
smarty的默认标记符是大括号:{}, 假如我们页面上有JS且定义了函数或者对象,或者有CSS定义了样式,就会出现大括号, smary引擎就会把这些大括号当作smarty语法来解释, 很明显,这些C ...
- Yii console 创建命令行应用
大家都知道PHP的程序没有进程概念,而且生命周期极短,无法实现一些定时计划或者是计划任务,今天我们看看在YII框架中如何使用计划任务创建命令行应用. 1.在 console/controllers 文 ...
- ecshop物料库存管理
1.创建物流库存表.sql语句: CREATE TABLE IF NOT EXISTS `emws_materials` (`id` mediumint(8) unsigned NOT NULL au ...
- FMDB将对象放进数据库[一]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...