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.

 #include<stdio.h>
#include<map>
#include<algorithm>
#include<set>
#include<string.h>
using namespace std;
int n , m ;
const int M = + ;
int cut[M] ;
bool vis[M] ;
pair <int , int> a[M] ; void solve ()
{
for (int i = ; i <= n ; i++) {
scanf ("%d" , &a[i].first) ;
a[i].second = i ;
}
sort (a + , a + n + ) ;
for (int i = n ; i >= ; i --) {
cut [i] = cut [i + ] ;
int now = a[i].second ;
if (vis[now - ] && vis[now + ] ) {
cut[i] -- ;
}
else if (!vis[now - ] && !vis[now + ]) {
cut[i] ++ ;
}
vis[now] = ;//1 代表没砍 , 0 代表砍了
}
while (m--) {
int h ;
scanf ("%d" , &h) ;
int x = upper_bound (a + , a + n + , make_pair (h , n + ) ) - a ;
if (x > n) puts ("") ; else printf ("%d\n" , cut[x]) ;
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d" , &n , &m)) {
memset (vis , , sizeof(vis)) ;
memset (cut , , sizeof(cut)) ;
solve () ;
}
return ;
}

假设0为砍掉状态 , 1 为活着状态。

假设一排树的高度为               2 4 3 1 6 5

他们一开始都为                      0 0 0 0 0 0

若只有最高的活着,状态变为  0 0 0 0 1 0   sta[6] = 1

若有第2高的活着,状态为       0 0 0 0 1 1   sta[5] = sta[6] = 1 , 显而易见第5,6棵树构成block , 不用+ 1

若有第3高的活者,状态为       0 1 0 0 1 1   sta[4] = sta[5] + 1 = 2 , 因为第{2} , {5 , 6}都为block

若有第4高的活着,状态为       0 1 1 0 1 1   sta[3] = sta[4]  = 2 ; {2 , 3} , {5 , 6}

如有第5高的活着,状态为       1 1 1 0 1 1 sta[2] = sta[3] = 2 ;  {1 , 2 , 3} , {5 , 6}

如果都活着, 状态为               1 1 1 1 1 1   sta[1] = sta[2] - 1 ; 只有{1 , 2 ,3 , 4 , 5 , 6} 一个block

虽然很orz,但过然是dp吧

BC.5200.Trees(dp)的更多相关文章

  1. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  2. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  3. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  4. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. D. How many trees? DP

    D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...

  6. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  7. Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

    D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...

  8. HDU 5200 Trees 二分

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

  9. hdu 5200 Trees [ 排序 离线 2指针 ]

    传送门 Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

随机推荐

  1. 20145234黄斐《信息安全系统设计基础》GDB调试汇编堆栈过程分析

    堆栈跟踪 首先编辑一个程序 用gcc编译,再使用gdb调试,发现gdb尚未下载 下载后重新运行gdb 设置断点:b+行号或者"main" 运行:r frame:打印出的信息:栈的层 ...

  2. js实现黑客帝国二进制雨

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<对于RegExp反向引用的一点理解> 作者主页:myvin 博主QQ:851399101(点击QQ和博 ...

  3. 『随笔』Socket 链接 必须 上下行 同时使用

    结论: > Socket 理论上 支持 只上行,或者 只下行. > 心跳包 必须是 上下行的 —— 心跳包请求(上行) - 心跳包响应(下行). > 如果 长时间 只有单向链接(只发 ...

  4. Bootstrap系列 -- 39. 导航条添加标题

    在Web页面制作中,常常在菜单前面都会有一个标题(文字字号比其它文字稍大一些),其实在Bootstrap框架也为大家做了这方面考虑,其通过“navbar-header”和“navbar-brand”来 ...

  5. 教你用netstat-实践案例

    netstat命令的功能是显示网络连接.路由表和网络接口信息,可以让用户得知目前都有哪些网络连接正在运作. 该命令的一般格式为: netstat [选项] 命令中各选项的含义如下: -a 显示所有so ...

  6. 关于hangfire的使用

    hangfire 是一个分布式后台执行服务.用它可以代替ThreadPool.QueunItemWork等原生方法.当然4.5后的 task也是相当好用且功能强大.不过如果想分布式处理并且可监控的话, ...

  7. redis学习笔记——(2)

    4.Redis中的string类型 String类型是最简单的类型,一个Key对应一个Value,String类型是二进制安全的.Redis的String可以包含任何数据,比如jpg图片或者序列化的对 ...

  8. session,cookie

    Session 和cookie的学习 cookie cookie的建立 setcookie(name,value); setcookie(name,value,expiration,path,host ...

  9. nginx配置反向代理示例

    环境: nginx1:192.168.68.41 tomcat1:192.168.68.43 tomcat2:192.168.68.45 nginx安装网上很多教程,我是用yum安装的. 配置ngin ...

  10. keep_on _coding——js_good_parts

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...