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. PCA和LDA降维的比较

    PCA 主成分分析方法,LDA 线性判别分析方法,可以认为是有监督的数据降维.下面的代码分别实现了两种降维方式: print(__doc__) import matplotlib.pyplot as ...

  2. JavaScript实例---表格隔行变色以及移入鼠标高亮

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  3. Jmeter使用指南

    序言 由于公司在来年需要进行压力测试,所以也就借节假日的机会来学习一下压力测试的步骤,由于本人的学习时间比较短,希望各位大神朋友们能够多多的谅解并指正在下的错误,在此仅表敬意 适应人群 1.初入门的压 ...

  4. Orchard 刨析:Caching

    关于Orchard中的Caching组件已经有一些文章做了介绍,为了系列的完整性会再次对Caching组件进行一次介绍. 缓存的使用 在Orchard看到如下一段代码: 可以看到使用缓存的方法Get而 ...

  5. 年前辞职-WCF入门学习(3)

    前言 [2了,刚写好的,手贱,点了删除,还真TMD的删除了,只好重写] 今天周六,去同事家聚了一个餐,感觉各种豪华. 废话不多说,前两集大致介绍了一下什么是WCF以及和WCF相关的WebService ...

  6. angular的DEMO(用来练习和顺便看看)

    inflector(辅助) 将用户输入的字符串转化成驼峰或者空格或者底线的小插件; 这个是一个小的过滤器, 平常也是用不到的, 合格是过滤器的代码: app.filter("inflecto ...

  7. java编程

    Java编程:五子棋游戏源代码 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing ...

  8. 【OpenCV入门教程之二】OPENCV3 开源之美 — 编译源代码、配置opencv_contrib

    为什么要配置opencv_contrib? opencv3.0版本 功能更加模块块,一些功能模块不够完善,等足够完善在merge到主分支中,而我们图像识别中要用到的SIFT等算法被封装在xfeactu ...

  9. 关于opacity透明度子元素继承现象的若干研究以及hack方法

    [感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...

  10. LINQ构建交叉表

    最近碰到客户的一个需求.使用交叉表来显示客户数据.也就是以同时以行头和列头交叉形式显示数据内容.同时要求即使有些列没有数据,也需要显示该列内容,并设置默认值. 说明: “交叉表”对象是一个网格,用来根 ...