HDU - 5200 - Trees (upper_bound)
来源:互联网 发布:linux没有iptables文件 编辑:程序博客网 时间:2024/04/25 14:35
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 numberedz , 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?
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numbered
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 integersN and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the followingN lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the followingQ 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)
For each case, first line contains two integers
In the following
In the following
Please process to the end of file.
[Technical Specification]
1 \leq N, Q \leq 50000
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 252362
Sample Output
02HintIn 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;}
0 0
- HDU - 5200 - Trees (upper_bound)
- hdu 4288(lower_bound&&upper_bound的运用)
- Hdu 5200 Trees (离线线段树)
- hdu 5200 Trees(离线处理,排序)
- HDU 5200 Trees
- hdu 5200 Trees
- HDU 5200 - Trees (离线 + 思维)
- hdu 5200 trees(离线处理)
- HDU 5200/BC 36 C Trees
- HDU 5200 Trees 离线+链表维护.
- hdu 1392 Surround the Trees(凸包果题)
- HDU 2841 Visible Trees(数论)
- hdu 3015 Disharmony Trees(树状数组)
- hdu 1130 How Many Trees? (java)
- hdu 3015 Disharmony Trees(树状数组)
- HDU 3015 Disharmony Trees(树状数组)
- 二分(upper_bound)
- lower_bound/upper_bound(STL)
- 安装完 MySQL 后必须调整的 10 项配置
- mariadb简介
- 网页内容居中
- Hadoop科普文——常见的45个问题解答
- CWnd与HWND的区别以、转换以及如何获取本窗体对象
- HDU - 5200 - Trees (upper_bound)
- Duilib技巧:背景图片平铺
- java解析html 内有萌妹子~Jsoup+Httpcient
- GDB与coredump错误类文件的解析
- 我的算法学习之路
- JAVA的StringBuffer类
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- 通达信目录文件结构及说明
- 数组,二维数组