题目链接:

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)。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn = ;
  8. const int mod = 1e9 + ;
  9. typedef long long LL;
  10.  
  11. struct Node {
  12. int id, h;
  13. bool operator < (const Node& rhm) const {
  14. return h<rhm.h;
  15. }
  16. }nds[maxn];
  17.  
  18. int n, q;
  19. int ans[maxn];
  20. bool cuted[maxn];
  21. // int fa[maxn];
  22.  
  23. void init() {
  24. // for(int i=0;i<maxn;i++) fa[i]=i;
  25. memset(cuted, , sizeof(cuted));
  26. cuted[] = cuted[n + ] = ;
  27. }
  28.  
  29. int solve(int x) {
  30. //超出去的部分要先处理掉
  31. if (x<nds[].h) return ;
  32. if (x >= nds[n - ].h) return ;
  33. int low = , hig = n;
  34. while (low + <hig) {
  35. int mid = low + (hig - low) / ;
  36. if (nds[mid].h <= x) low = mid;
  37. else hig = mid;
  38. }
  39. return ans[low];
  40. }
  41.  
  42. int main() {
  43. while (scanf("%d%d", &n, &q) == && n) {
  44. init();
  45. for (int i = ; i<n; i++) {
  46. scanf("%d", &nds[i].h);
  47. nds[i].id = i + ;
  48. }
  49. sort(nds, nds + n);
  50. int num = ;
  51. for (int i = ; i<n; i++) {
  52. int id = nds[i].id;
  53. if (!cuted[id - ] && !cuted[id + ]) {
  54. num++;
  55. }
  56. else if (cuted[id - ] && cuted[id + ]) {
  57. num--;
  58. }
  59. cuted[id] = ;
  60. ans[i] = num;
  61. }
  62. while (q--) {
  63. int x;
  64. scanf("%d", &x);
  65. printf("%d\n", solve(x));
  66. }
  67. }
  68. return ;
  69. }
  70. /*
  71. 1 100
  72. 1
  73. 0
  74. 2
  75. */

直接用upper_bound():

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn = ;
  8. const int mod = 1e9 + ;
  9. typedef long long LL;
  10.  
  11. struct Node {
  12. int id, h;
  13. Node(int id, int h) :id(id), h(h) {}
  14. Node() {}
  15. bool operator < (const Node& rhm) const {
  16. return h<rhm.h;
  17. }
  18. }nds[maxn];
  19.  
  20. int n, q;
  21. int ans[maxn];
  22. bool cuted[maxn];
  23. // int fa[maxn];
  24.  
  25. void init() {
  26. // for(int i=0;i<maxn;i++) fa[i]=i;
  27. memset(cuted, , sizeof(cuted));
  28. cuted[] = cuted[n + ] = ;
  29. }
  30.  
  31. int solve(int x) {
  32. //超出去的部分要先处理掉
  33. if (x<nds[].h) return ;
  34. if (x >= nds[n - ].h) return ;
  35. //upper_bound找到的是x后面的一个数,所以要减一
  36. return ans[upper_bound(nds, nds + n, Node(,x))-nds-];
  37. }
  38.  
  39. int main() {
  40. while (scanf("%d%d", &n, &q) == && n) {
  41. init();
  42. for (int i = ; i<n; i++) {
  43. scanf("%d", &nds[i].h);
  44. nds[i].id = i + ;
  45. }
  46. sort(nds, nds + n);
  47. int num = ;
  48. for (int i = ; i<n; i++) {
  49. int id = nds[i].id;
  50. if (!cuted[id - ] && !cuted[id + ]) {
  51. num++;
  52. }
  53. else if (cuted[id - ] && cuted[id + ]) {
  54. num--;
  55. }
  56. cuted[id] = ;
  57. ans[i] = num;
  58. }
  59. while (q--) {
  60. int x;
  61. scanf("%d", &x);
  62. printf("%d\n", solve(x));
  63. }
  64. }
  65. return ;
  66. }
  67. /*
  68. 1 100
  69. 1
  70. 0
  71. 2
  72. */

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. golang总结-Redis整合

    目录 1. 基本用法 2. Redis连接池 go get github.com/gomodule/redigo/redis 1. 基本用法 获取连接 package conn import ( &q ...

  2. 你不知道的css之 width “继承”篇。

    众所周知,css的三大特性分别是 继承性,层叠性,和优先级. 那么这里就详细说一下css中width的继承性及其特殊情况. 继承性概念详解:css的继承性指的被包在内部的标签拥有外部标签的样式性,子元 ...

  3. swiper 导航有多个,被点击的项居中显示。

    <div class="swiper-container"> <div class="swiper-wrapper"> <div ...

  4. Redis底层数据类型

    Redis主要数据结构:简单动态字符串(SDS).双端链表.字典.跳跃表.整数集合.压缩列表和快速列表: 一.简单动态字符串(SDS): Redis没有直接使用C语言中的传统的字节数组保存字符串,而是 ...

  5. laychat聊天功能

    windows版本:1.直接下载laychat聊天室压缩包,并解压到PHPstudy本地PHP环境中去:2.进入E:\PHPTutorial\WWW\laychat-master\vendor\Wor ...

  6. 【python3】——centos7下安装

    centos7下安装python3总步骤分三步: 一.依赖解决: 1.安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-d ...

  7. SSIS平台下的对象和概念

    包即经检索.执行和保存的工作单元,是最重要的 Integration Services 对象. 控制流元素(任务和容器),用于在包中生成控制流.控制流元素准备或复制数据,与其他进程进行交互,或实现重复 ...

  8. Find the Duplicate Number (寻找重复数字)

    对于一个长度为n+1的数组,其中每一个值的取值范围是[1,n],可以证明的是必然存在一个重复数字(抽屉原理),假设仅存在一个重复数字,找到他. 举例:输入:[1,3,4,2,1],输出:1 自己做的时 ...

  9. ubuntu配置机器学习环境(二) cuda 和cudnn 安装

    Nvidia CUDA Toolkit的安装(cuda) PS:特别推荐*.deb的方法,目前已提供离线版的deb文件,该方法比较简单,不需要切换到tty模式,因此不再提供原来的*.run安装方法,这 ...

  10. 自定义对象存入Redis

    package com.cms.common; import com.alibaba.fastjson.JSON; import com.qiyi.tvguo.cms.common.utils.Obj ...