HDU 5200 Trees 二分
题目链接:
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 二分的更多相关文章
- hdu 5200 Trees [ 排序 离线 2指针 ]
传送门 Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- hdu 2413(最大匹配+二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
- HDU 1025 DP + 二分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...
- hdu 2289 要二分的杯子
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 大意是 一个Cup,圆台形,给你它的顶部圆的半径,底部圆的半径,杯子的高度,和此时里面装的水的体 ...
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
随机推荐
- golang总结-Redis整合
目录 1. 基本用法 2. Redis连接池 go get github.com/gomodule/redigo/redis 1. 基本用法 获取连接 package conn import ( &q ...
- 你不知道的css之 width “继承”篇。
众所周知,css的三大特性分别是 继承性,层叠性,和优先级. 那么这里就详细说一下css中width的继承性及其特殊情况. 继承性概念详解:css的继承性指的被包在内部的标签拥有外部标签的样式性,子元 ...
- swiper 导航有多个,被点击的项居中显示。
<div class="swiper-container"> <div class="swiper-wrapper"> <div ...
- Redis底层数据类型
Redis主要数据结构:简单动态字符串(SDS).双端链表.字典.跳跃表.整数集合.压缩列表和快速列表: 一.简单动态字符串(SDS): Redis没有直接使用C语言中的传统的字节数组保存字符串,而是 ...
- laychat聊天功能
windows版本:1.直接下载laychat聊天室压缩包,并解压到PHPstudy本地PHP环境中去:2.进入E:\PHPTutorial\WWW\laychat-master\vendor\Wor ...
- 【python3】——centos7下安装
centos7下安装python3总步骤分三步: 一.依赖解决: 1.安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-d ...
- SSIS平台下的对象和概念
包即经检索.执行和保存的工作单元,是最重要的 Integration Services 对象. 控制流元素(任务和容器),用于在包中生成控制流.控制流元素准备或复制数据,与其他进程进行交互,或实现重复 ...
- Find the Duplicate Number (寻找重复数字)
对于一个长度为n+1的数组,其中每一个值的取值范围是[1,n],可以证明的是必然存在一个重复数字(抽屉原理),假设仅存在一个重复数字,找到他. 举例:输入:[1,3,4,2,1],输出:1 自己做的时 ...
- ubuntu配置机器学习环境(二) cuda 和cudnn 安装
Nvidia CUDA Toolkit的安装(cuda) PS:特别推荐*.deb的方法,目前已提供离线版的deb文件,该方法比较简单,不需要切换到tty模式,因此不再提供原来的*.run安装方法,这 ...
- 自定义对象存入Redis
package com.cms.common; import com.alibaba.fastjson.JSON; import com.qiyi.tvguo.cms.common.utils.Obj ...