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 ...
随机推荐
- 如何从SAP ECC中抽取簇表数据
打开SAP 客户端工具 ABAP 中 创建包(SE80) 创建函数组 展开ABAP 工作台,双击ABAP Dictionary 字典: 选择第三个data type,输入数据结构名称ZSQL_CLAU ...
- DP_最长公共子序列/动规入门
学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...
- 20155211 2016-2017-2《Java程序设计》课程总结
20155211 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:对师生关系的理解 预备作业2:熟能生巧及学习c语言的心的 预备作业3:关于假期 ...
- 实验楼学习linux第一章第四节linux目录结构及文件基本操作
linux目录结构及文件基本操作 常用命令 切换目录 cd 当前目录 . 上一级目录 .. (.和..开头的都是隐藏文件) 查看隐藏文件 ls -a 上一级所在目录 - 当前用户home目录 ~ 获取 ...
- STM32L431驱动带UC1698芯片调试记录
1, 数据线连接方式,这次使用的是8080格式的接口,如下 2. 主要是信号和数据引脚 DATA0-DATA7 并口的数据 RST 复位信号 WR 写信号 RD 读信号 C/D 数据还是命令 CS片 ...
- 使用GUI工具高效构建你自己的Nuget包
写这篇文章的原因是我在学习构建nuget包的时候,发现了一个官方推荐的GUI工具,而官方的工具介绍文章已经过时,一些地方和现在最新版本的工具有些差异,所以特意利用假期最后一个下午写下来,希望能帮助更多 ...
- Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用
Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Object ...
- Karma与TSLint
TSLint TSLint是一个可扩展的静态分析工具,用于检查TypeScript代码的可读性,可维护性和功能性错误.收到现代编辑和构建系统的广泛支持,并且可以使用您自己的路由,配置和格式化. 安装 ...
- ABP 框架集成EF批量增加、删除、修改只针对使用mmsql的
AppService 层使用nuget 添加 EFCore.BulkExtensions 引用 using Abp.Application.Services.Dto; using Abp.Domain ...
- Maven学习(七)-----Maven添加远程仓库
Maven添加远程仓库 默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资 ...