题目链接:

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. js(jQuery)tips

    一:页面加上$(function(){***内容***})与不加的区别 1.这个是DOM加载完之后再加载JS代码,你的JS如果放在文档后面可能一样,但是如果你要是把JS放在head里面就有差别了(放在 ...

  2. Homebrew(brew)安装MySQL成功后无法登录

    Homebrew简称brew,OSX上的软件包管理工具,在Mac终端可以通过brew安装.更新.卸载各种软件,(简直就是神器级武器). 废话不多说,没安装brew自己去百度学习安装,这里就不多说了. ...

  3. NOR Flash的原理与操作

    学习目标: 1.了解nor flash存储芯片的概念和特性 2.掌握使用s3c2440芯片对外挂的nor flash进行读写擦除操作 1.NOR Flash的简单介绍 NOR Flash最早是由Int ...

  4. devise定义多个authentication_keys

    在你的model中加入 def self.find_for_database_authentication(warden_conditions) conditions = warden_conditi ...

  5. STM32F103C8T6、STM32F103ZET6工程模板

    STM32F103C8T6工程模板,推荐使用以下最新版本 最终版 2018 7 16  https://pan.baidu.com/s/1lIdZ2awus_quVu332RvJ6Q https:// ...

  6. Mac os安装golang开发环境

    为了能够愉快地进行golang编程,我们需要安装以下几样东西: 包管理Homebrew 语言环境golang 版本管理git 虚拟器docker 编译器Goland 我将按照这个顺序叙述整个安装过程 ...

  7. 20145310 《Java程序设计》第10周学习总结

    20145310 <Java程序设计>第10周学习总结 教材学习内容总结 网络概述 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置 ...

  8. 2017-2018-1 20155321 《信息安全系统设计基础》课堂实践——实现mypwd

    2017-2018-1 20155321 <信息安全系统设计基础>课堂实践--实现mypwd 学习pwd命令 pwd命令:输出当前工作目录的绝对路径 还可通过man pwd具体查看pwd的 ...

  9. 读懂UML类图

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  10. phpcms 的模板更换

    刚安装完成后的phpcms ,自带的默认模板样式一般,可以自己换模板 首先打开phpcms文件夹,按照下图路径打开 default是存模板的文件夹 在里面新建一个文件夹 在default中主要用到的是 ...