HDU - 5200 - Trees (upper_bound)

Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 205    Accepted Submission(s): 68

Problem Description

Today CodeFamer is going to cut trees.There are N trees
standing in a line. They are numbered from 1 to N.
The tree numbered i has
height hi.
We say that two uncutted trees whose numbers are x and y are
in the same block if and only if they are fitting in one of blow rules:

1)x+1=y or y+1=x;

2)there exists an uncutted tree which is numbered z,
and x is
in the same block with z,
while y is
also in the same block with z.

Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?

Input

Multi test cases (about 15).

For each case, first line contains two integers N and Q separated
by exactly one space, N indicates there are N trees, Q indicates
there are Q queries.

In the following N lines,
there will appear h[1],h[2],h[3],…,h[N] which
indicates the height of the trees.

In the following Q lines,
there will appear q[1],q[2],q[3],…,q[Q] which
indicates CodeFamer’s queries.

Please process to the end of file.

[Technical Specification]

1 \leq N, Q \leq 50000

0≤h[i]≤1000000000(109)

0≤q[i]≤1000000000(109)

Output

For each q[i],
output the number of tree block after CodeFamer cut the trees whose height are not larger than q[i].

Sample Input

3 2
5
2
3
6
2

Sample Output

0
2

Hint

In this test case, there are 3 trees whose heights are 5 2 3.

For the query 6, if CodeFamer cuts the tree whose height is not large than 6, the height form of left trees are -1 -1 -1(-1 means this tree was cut). Thus there is 0 block.

For the query 2, if CodeFamer cuts the tree whose height is not large than 2, the height form of left trees are 5 -1 3(-1 means this tree was cut). Thus there are 2 blocks.

Source

BestCoder Round #36 ($)

STL中关于二分查找的函数有三个lower_bound 、upper_bound 、binary_search 。这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数。

ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。

ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于值val的位置。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 100005;
int n, q;
int h[maxn], ans[maxn], vis[maxn];
pair<int, int> p[maxn];

int main() {
	while(scanf("%d %d", &n, &q) != EOF) {
		for(int i = 1; i <= n; i++) {
			scanf("%d", h + i);
			p[i] = make_pair(-h[i], i);
		}

		memset(vis, 0, sizeof(vis));

		sort(p + 1, p + n + 1);//因为存的是负值,所以是从大到小排序 

		int sum = 0;//记录当前有多少个块
		for(int i = 1; i <= n; i++) {
			int x = p[i].second;
			if(!vis[x - 1] && !vis[x + 1]) sum ++;//多一个块,因为两边都没被访问
			else if(vis[x - 1] && vis[x + 1]) sum --;//合并两个块为一个,因为正好将两边连了起来,其他情况不会影响块的增加与减少
			ans[i] = sum;//这里表示从第i个数据以下(数据是从大到小的)射击能够得到的块数
			vis[x] = 1;
		}

		for(int i = 0; i < q; i++) {
			int t;
			scanf("%d", &t);//upper_bound返回一个非递减序列[first, last)中的第一个大于值val的位置。
			int id = upper_bound(p + 1, p + n + 1, 	make_pair(-t, -1)) - p - 1;
			printf("%d\n", ans[id]);
		}
	}
	return 0;
} 
时间: 2025-01-07 03:57:51

HDU - 5200 - Trees (upper_bound)的相关文章

Hdu 5200 Trees (离线线段树)

题目大意: 校门外栽满了不同高度的树,每一次询问是 如果砍掉所有高度不超过q的树,那么还有多少个连续的块. 思路分析: 记录左连续和   右连续和  用来维护区间的连续块的数量. 即 seg[num] = seg[num<<1]+seg[num<<1|1] ; 如果中间部分连起来  那么减一即可. #include <cstdio> #include <iostream> #include <cstring> #include <algor

HDU 2841-Visible Trees(容斥原理)

题目链接:传送门 题意:有一个n*m的矩阵上布满了树(矩阵从(1,1)开始),现在有一个农夫站在(0,0)点,问农夫可以看到多少棵树,其中如果这些树在一条线上那么只能看到最前面的那棵树,这个一开始看到确实蒙了..看了题解其实是挺简单的.首先考虑只能看到一条线上最前面的那棵树这个条件,对于坐标 比如 (2,3)(4,6)(6,9)..等 这些坐标是在一条直线上的 可以看出其除了(2,3) 其他的都是由(2,3)的x坐标*k y坐标*k 得到的,  拓展出来就是对于 任意坐标 (x,y) 令a=x/

[ACM] hdu 1260 Tickets (动态规划)

Tickets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Jesus, what a great movie! Thou

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys

hdu 1494 跑跑卡丁车(动态规划)

Problem Description 跑跑卡丁车是时下一款流行的网络休闲游戏,你可以在这虚拟的世界里体验驾驶的乐趣.这款游戏的特别之处是你可以通过漂移来获得一种加速卡,用这种加速卡可以在有限的时间里提高你的速度.为了使问题简单化,我们假设一个赛道分为L段,并且给你通过每段赛道的普通耗时Ai和用加速卡的耗时Bi.加速卡的获得机制是:普通行驶的情况下,每通过1段赛道,可以获得20%的能量(N2O).能量集满后获得一个加速卡(同时能量清0).加速卡最多可以储存2个,也就是说当你有2个加速卡而能量再次

HDU 5826 physics(物理)

HDU 5826 physics(物理) Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Description 题目描述 There are n balls on a smooth horizontal straight track. The track can be considered to be a number line. The balls can be con

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.