题目链接:

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. VM上Hadoop3.1伪分布式模式搭建

    https://www.cnblogs.com/asker009/p/9126354.html 最近要搭建一个Hadoop做实验,因为版本的问题遇到不少的坑,本文记录VM上搭建的CentOS7.0+H ...

  2. 面试被问到SPI总结

    SPI驱动框架 枚举过程 drivers/spi/spi.c: spi_register_board_info /* 对于每一个spi_master,调用spi_match_master_to_boa ...

  3. 鼠标移动在屏幕上显示温度Tip提示功能-CToolTipCtrl类的使用

    初学VC++,太多知识不懂,需要不断的查找资料,想通过记录让自己有所积累,主要是怕以后会很快忘记.最近在做一个在屏幕上显示鼠标移动位置的温度值,我利用先缓存一帧图像的温度值,然后,通过鼠标移动消息相应 ...

  4. 记账APP(4)

    依旧是一个表格类型的增删改查,但是呢,在用节点做,有点懵,明天加油

  5. Hadoop安装配置

    1.集群部署介绍 1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesy ...

  6. 20155310马英林 实验2 Windows口令破解

    实 验 报 告 实验名称: 实验二 口令破解 姓名:马英林 学号: 20155310 班级: 1553 日期: 2017.10.24 一. 实验环境 •系统环境:Windows •网络环境:交换网络结 ...

  7. 【BZOJ3142】[HNOI2013]数列

    [BZOJ3142][HNOI2013]数列 题面 洛谷 bzoj 题解 设第\(i\)天的股价为\(a_i\),记差分数组\(c_i=a_{i+1}-a_i\) 则 \[ Ans=\sum_{c_1 ...

  8. 【LG3723】[AHOI2017/HNOI2017]礼物

    [LG3723][AHOI2017/HNOI2017]礼物 题面 洛谷 题解 首先我们将\(c\)看作一个可以为负的整数,那么我们就可以省去讨论在哪个手环加\(c\)的繁琐步骤了 设我们当前已经选好了 ...

  9. STM8在IAR中Printf的整形长度问题

    //ld是32位的 printf("up_intval:%ld\r\n",device_set.upload_tem); //d是16位的 printf("up_intv ...

  10. 【python笔记】python中的list、tuple、set、dict用法简析

    list list是一种有序的集合(或称作列表),可以很方便地添加和删除其中的元素. >>> classmates = ['Michael', 'Bob', 'Tracy'] 可通过 ...