传送门

Trees

 Accepts: 156
 Submissions: 533
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
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≤N,Q≤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.

题解:

窝题目看错了,,悲催啊。。题解以代码均来自小微哥。

对树的高度和查询的高度都分别排序。

然后,两个指针操作,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指针 ]的更多相关文章

  1. HDU 5200 Trees 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. HDU 5200 脑洞题 离线

    线段树,TLE,各种.唉....我真是笨死了.... 我用的线段树是记录左右区间最长连续棵数的...反正TLE #include <iostream> #include <cstdi ...

  3. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  4. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  5. 《挑战30天C++入门极限》在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子。

        在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子. 以下两个例子要非常注意,函数传递的不是数组中数组元素的真实值而是数组在内存中的实际地址. #include <std ...

  6. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  7. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  9. 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 ...

随机推荐

  1. 013、BOM对象的应用

    BOM结构图如下: DOM结构图如下: BOM和DOM BOM,Bowser Object Model浏览器对象模型.提供了访问和操作浏览器各组件的途径或方法. 比如:Navigator对象:浏览器的 ...

  2. VS2015调用低版本lib库出现“无法解析的外部符号 __snprintf ”问题的解决

    VS2015在调用低版本lib库出现有时会出现“无法解析的外部符号 __snprintf ”的问题,解决方法是加入lib库“legacy_stdio_definitions.lib”到工程.

  3. calc() 计算CSS属性值

    calc()是css3的一个新增的功能,用来指定元素的长度.比如说,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值.calc() ...

  4. ES之基本数据类型之间的显示转换和隐式转换

    typeof(数据)/ typeof 数据 判断数据的数据类型,typeof返回的都是字符串 输出结果类型有:number.string.boolean. undefined.object.funct ...

  5. iOS 动画(基于Lottie封装)

    一般app中都会带有动画,而如果是一些复杂的动画,不但实现成本比较高,而且实现效果可能还不能达到UI想要的效果,于是我们可以借助lottie来完成我们想要的动画.   lottie动画1.gif   ...

  6. Xilinx器件原语

    原语,其英文名为primitive,是FPGA厂商针对其器件特征开发的一系列常用模块的名称.原语是FPGA芯片中基本元件,代表FPGA中实际拥有的硬件逻辑单元,如LUT,D触发器,RAM等.相当于软件 ...

  7. SAP云平台架构概述

    在我们开始SAP云平台的架构之旅之前,让我们先看看SAP已经发布的一些其他云产品.这些云产品方案可以分为公有云和私有云两种. SAP公有云解决方案见下图最右侧,比较著名的有SAP SuccessFac ...

  8. javaee 第七周作业

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  9. pip和pip3安装、升级、版本查看及遇到的问题

    pip的安装 问题一 sudo apt-get install python-pip #安装pip sudo pip install --upgrade pip -i http://mirrors.a ...

  10. xorequation(DFS完全枚举)

    题目 有一个含有N个未知数的方程如下: x1^x2^...^xn= V,给定N,V,再给定正整数a1,a2,...an满足1≤ai≤9且∏Ni=1(ai+1)  ≤ 32768,请输出所有满足0≤xi ...