hdu 5200 Trees [ 排序 离线 2指针 ]
Trees
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?
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≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
For each q[i], output the number of tree block after CodeFamer cut the trees whose height are not larger than q[i].
3 2
5
2
3
6
2
0
2
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.
题解:
窝题目看错了,,悲催啊。。题解以代码均来自小微哥。
对树的高度和查询的高度都分别排序。
然后,两个指针操作,O(n+m)复杂度。
13341721 | 2015-04-04 21:14:13 | Accepted | 5200 | 702MS | 4588K | 1496 B | C++ | czy |
代码来自小微哥:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 500005 struct point{
int i, v;
}p[M];
struct node
{
int v;
int cur;
}s[M];
int has[M], res[M]; bool cmp (point a, point b)
{
return a.v > b.v;
}
int S()
{
int ret=,ok=;
char c;
while((c=getchar()))
{
if(c>=''&&c<='')
ret=(ret<<)+ret+ret+c-'',ok=;
else if(ok)
return ret;
}
return ret;
}
bool cmp2(node a,node b)
{
return a.v<b.v;
}
int main()
{
int n, d, i, j, ans;
while (~scanf("%d%d",&n,&d))
{
for (i = ; i < n; i++)
{
p[i].v=S();
p[i].i = i+;
}
sort(p, p+n, cmp);
for (j = ; j < d; j++)
{
s[j].v=S();
s[j].cur=j;
}
sort(s,s+d,cmp2);
memset(has, , sizeof(has));
ans = ;
for (i = , j = d - ; j >= ; j--)
{
for ( ; i < n; i++)
{
if (p[i].v <= s[j].v)
break;
int id = p[i].i;
has[id] = ;
if (!has[id-] && !has[id+]) ++ans;
else if (has[id-] && has[id+]) --ans;
}
res[s[j].cur] = ans;
}
for (i = ; i < d; i++)
{
printf ("%d\n", res[i]);
}
}
return ;
}
hdu 5200 Trees [ 排序 离线 2指针 ]的更多相关文章
- HDU 5200 Trees 二分
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 5200 脑洞题 离线
线段树,TLE,各种.唉....我真是笨死了.... 我用的线段树是记录左右区间最长连续棵数的...反正TLE #include <iostream> #include <cstdi ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- 《挑战30天C++入门极限》在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子。
在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子. 以下两个例子要非常注意,函数传递的不是数组中数组元素的真实值而是数组在内存中的实际地址. #include <std ...
- J - Super Mario HDU - 4417 线段树 离线处理 区间排序
J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4746 莫比乌斯反演+离线查询+树状数组
题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
随机推荐
- 证明碰撞集问题(Hitting Set)是NP-complete
证明碰撞集问题(Hitting Set)是NP-complete Problem In the HITTING SET problem, we are given a family of sets { ...
- mysql索引原理及创建与查询
索引介绍 一:为什么要有索引 索引是用来优化查询效率(速度)的 没有索引的话,对于大数据的表,就只能每次都遍历一遍,数据量越大,耗时越多有索引的话,可以提升好几个数量级的速度 一般的应用系统,读写比例 ...
- jQuery选择器之表单对象属性筛选选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- Eclipse被卡死了或者失去响应了后分析根源的一个小技巧
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- Python100天打卡-Day10-图形用户界面和游戏开发
基于tkinter模块的GUIPython默认的GUI开发模块是tkinter(在Python 3以前的版本中名为Tkinter)使用tkinter来开发GUI应用需要以下5个步骤: 导入tkinte ...
- v8引擎详解
引用网址: https://blog.csdn.net/swimming_in_it_/article/details/78869549 前言 JavaScript绝对是最火的编程语言之一,一直具有很 ...
- QT+信号和槽函数_自定义槽函数_一个信号对应多个槽函数
以下的代码里面有自定义槽函数的内容,同时也有信号实现的函数: #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #i ...
- centOS出现 -bash: vim: command not found
问题描述 用centos 的主机的時候, 用 vim 时出现 -bash: vim: command not found. 只能使用 vi. 那么如何安裝 vim 呢? 解决步骤 1.查看是否安装 输 ...
- koa源码解读
koa是有express原班人马打造的基于node.js的下一代web开发框架.koa 1.0使用generator实现异步,相比于回调简单和优雅和不少.koa团队并没有止步于koa 1.0, 随着n ...
- 02numpy
一. Numpy定义 一个在Python中做科学计算的基础库,重在数值计算,也是大部分PYTHON科学计算库的基础库,多用于在大型.多维数组上执行数值运算 二. Numpy使用 1.创建数组 2.nu ...