传送门

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 ;
}

最新文章

  1. Hibernate的save()和persist()的区别
  2. ASP.NET程序中常用的三十三种代码
  3. [Spring Batch] 图解Spring Batch原理
  4. SpringMVC系列之基本配置
  5. Java中Properties类的操作
  6. Firefox常用插件
  7. Python安装、配置
  8. python3.4 尝试 py2exe
  9. 修改TabPageIndicator下划线的颜色
  10. Less的模式匹配
  11. WebApi接收复杂类型参数
  12. 英雄联盟LOL用什么语言写的?
  13. LVS集群TUN模式实例(5)
  14. jquery checkbox勾选/取消勾选checked属性不生效问题
  15. ansible基础-优化
  16. L2-005 集合相似度 (25 分) (STL——set)
  17. linux 添加ssh和开启ssh服务apt管理的ubuntu
  18. js 原型链与继承
  19. mongodb之 非正常关闭启动报错处理
  20. c++11 闭包的实现

热门文章

  1. logging模块进阶
  2. P3372 【模板】线段树 1 区间查询与区间修改
  3. (5)《Head First HTML与CSS》学习笔记---布局与定位
  4. JS学习-事件响应小结-简单的计算器
  5. HYSBZ 1086 王室联邦 (树的分块)
  6. HTTP 200 OK和HTTP 304 Not modified的由来
  7. 网新恩普(T 面试)
  8. ios operationqueue
  9. python 03 8/25-8/27 range 、randint
  10. 条款32:确定你的public继承塑模出is-a 关系(Make sure public inheritacne models &quot;is-a&quot;)