题目链接:

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

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=574&pid=1003

题解:

把输入按照高度排序,离线处理出所有高度的答案,每次查询的时候二分查找(upper_bound)。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ;
const int mod = 1e9 + ;
typedef long long LL; struct Node {
int id, h;
bool operator < (const Node& rhm) const {
return h<rhm.h;
}
}nds[maxn]; int n, q;
int ans[maxn];
bool cuted[maxn];
// int fa[maxn]; void init() {
// for(int i=0;i<maxn;i++) fa[i]=i;
memset(cuted, , sizeof(cuted));
cuted[] = cuted[n + ] = ;
} int solve(int x) {
//超出去的部分要先处理掉
if (x<nds[].h) return ;
if (x >= nds[n - ].h) return ;
int low = , hig = n;
while (low + <hig) {
int mid = low + (hig - low) / ;
if (nds[mid].h <= x) low = mid;
else hig = mid;
}
return ans[low];
} int main() {
while (scanf("%d%d", &n, &q) == && n) {
init();
for (int i = ; i<n; i++) {
scanf("%d", &nds[i].h);
nds[i].id = i + ;
}
sort(nds, nds + n);
int num = ;
for (int i = ; i<n; i++) {
int id = nds[i].id;
if (!cuted[id - ] && !cuted[id + ]) {
num++;
}
else if (cuted[id - ] && cuted[id + ]) {
num--;
}
cuted[id] = ;
ans[i] = num;
}
while (q--) {
int x;
scanf("%d", &x);
printf("%d\n", solve(x));
}
}
return ;
}
/*
1 100
1
0
2
*/

直接用upper_bound():

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ;
const int mod = 1e9 + ;
typedef long long LL; struct Node {
int id, h;
Node(int id, int h) :id(id), h(h) {}
Node() {}
bool operator < (const Node& rhm) const {
return h<rhm.h;
}
}nds[maxn]; int n, q;
int ans[maxn];
bool cuted[maxn];
// int fa[maxn]; void init() {
// for(int i=0;i<maxn;i++) fa[i]=i;
memset(cuted, , sizeof(cuted));
cuted[] = cuted[n + ] = ;
} int solve(int x) {
//超出去的部分要先处理掉
if (x<nds[].h) return ;
if (x >= nds[n - ].h) return ;
//upper_bound找到的是x后面的一个数,所以要减一
return ans[upper_bound(nds, nds + n, Node(,x))-nds-];
} int main() {
while (scanf("%d%d", &n, &q) == && n) {
init();
for (int i = ; i<n; i++) {
scanf("%d", &nds[i].h);
nds[i].id = i + ;
}
sort(nds, nds + n);
int num = ;
for (int i = ; i<n; i++) {
int id = nds[i].id;
if (!cuted[id - ] && !cuted[id + ]) {
num++;
}
else if (cuted[id - ] && cuted[id + ]) {
num--;
}
cuted[id] = ;
ans[i] = num;
}
while (q--) {
int x;
scanf("%d", &x);
printf("%d\n", solve(x));
}
}
return ;
}
/*
1 100
1
0
2
*/

HDU 5200 Trees 二分的更多相关文章

  1. hdu 5200 Trees [ 排序 离线 2指针 ]

    传送门 Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

  2. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  3. hdu 2413(最大匹配+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...

  4. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  5. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

  6. HDU 1025 DP + 二分

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...

  7. hdu 2289 要二分的杯子

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 大意是 一个Cup,圆台形,给你它的顶部圆的半径,底部圆的半径,杯子的高度,和此时里面装的水的体 ...

  8. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  9. hdu 2962 Trucking (二分+最短路Spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...

随机推荐

  1. 使同一个server上不同port的django应用可在同一个浏览器上打开

    如果我们有两个django应用site1和site2同时跑在同一个server的不同端口,同时我们在同一个浏览器的不同tab登录.那么这时就出出现这种情况,当我们登录site2时就会将site1上登录 ...

  2. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...

  3. 用GO写一个连接比特币JSONRPC接口的程序

    比特币钱包默认是不开启JSONRPC接口的,要在比特币区块文件夹下新建bitcoin.conf这个文件,并写入以下内容 server=1  rpcuser=xxmm  rpcpassword=1234 ...

  4. fdisk -l不显示磁盘信息的问题

    新建了个ubuntu虚拟机,进去后使用fdisk查看磁盘信息居然没反应,嗯?这是怎么回事 别急,这是因为fdisk命令在/sbin目录下,使用root权限才可以运行. 由于是新建的虚拟机,按以下方法设 ...

  5. 20155222 2016-2017-2 《Java程序设计》第3周学习总结

    20155222 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 要产生对象必须先定义类,类是对象的设计图,对象是类的实例. 数组一旦建立,长度就固定了. 字 ...

  6. 10.11课后练习——MyOD系统调用版本

    MyOD系统调用版本 题目要求 参考教材<深入理解计算机(第三版)>第十章内容 用Linux IO相关系统调用编写myod.c 用myod XXX实现Linux下od -tx -tc XX ...

  7. 20155305乔磊2016-2017-2《Java程序设计》第三周学习总结

    20155305乔磊 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 对象(Object):存在的具体实体,具有明确的状态和行为 类(Class):具有相同属 ...

  8. # 学号20155308 2006-2007-2 《Java程序设计》第4周学习总结

    学号20155308 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 6.1 何谓继承 继承基本上就是避免多个类间重复定义共同行为. 许多类之间具有相同的属性 ...

  9. 20145234黄斐《java程序设计》第二周

    教材学习内容总结 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Type),其中类类型也叫参考类型(Reference Type). 字节类型,也叫byte类型, ...

  10. struts2官方 中文教程 系列四:Action

    先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列四:Action  即 http://www.cnblogs.com/linghaoxinpian/p/6905521.html 下载 ...