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 ...
随机推荐
- 关于使用iframe,父元素无法获得子iframe对的元素
首先确定自己写的方法对不对: $(document.getElementById('iframe的ID').contentWindow.document.body).find("要获得的元素 ...
- css中可以继承的属性
声明 : 本文源于https://www.cnblogs.com/thislbq/p/5882105.html CSS中可以和不可以继承的属性 一.无继承性的属性 1.display:规定元素应该 ...
- iOS开发者证书-详解/生成/使用
本文假设你已经有一些基本的Xcode开发经验, 并注册了iOS开发者账号. 相关基础 加密算法 现代密码学中, 主要有两种加密算法: 对称密钥加密 和 公开密钥加密. 对称密钥加密 对称密钥加密(Sy ...
- Tomcat优化(心得经验)
最近用httpclient做performance testing时,发现当线程加到150时服务端就会抛出socket资源用尽的错误,根本没法再往上加,响应的速度也是相当的慢,后来经过研究,发现在se ...
- SELinux初学者指南
SELinux(Security Enhanced Linux)是美国国家安全局2000年发布的一种高级MAC(Mandatory Access Control,强制访问控制)机制,用来预防恶意入侵. ...
- Oracle——系统数据字典常用命令(查看表所属空间层目录等)
发生背景: 项目前后台交互对接时候,经常存在对底层表蒙圈情况尤其是oracle数据库,所在层级不同会导致操作对象直接的改变,从而发生意向不到的事情:很多时候需要了解我们所操作对象所处的层级等相关信息, ...
- Codeforces #123D: 后缀数组+单调栈
D. String You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤ ...
- c语言单向链表逆转实现方法
自己理解的思路如下所示: 从第二个节点开始,先记录下一个节点,把第二个节点移到头节点之前,头节点变为移动的这个节点之前记录的节点变为接下来要移动的节点用for循环重复最后把原来头节点变成尾节点(*ne ...
- 20155211 2016-2017-2《Java程序设计》课程总结
20155211 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:对师生关系的理解 预备作业2:熟能生巧及学习c语言的心的 预备作业3:关于假期 ...
- Spring SimpleJdbcOperations 批量更新
1.控制台代码 import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowM ...