BC.5200.Trees(dp)
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.
#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)的更多相关文章
- 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 ...
- CodeForces #369 C. Coloring Trees DP
题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少. K:连续的颜色为一组 ...
- C. Coloring Trees DP
传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...
- codeforces 711C C. Coloring Trees(dp)
题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- D. How many trees? DP
D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- 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 ...
- HDU 5200 Trees 二分
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- hdu 5200 Trees [ 排序 离线 2指针 ]
传送门 Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
随机推荐
- 利用location.hash+iframe跨域获取数据详解
前言 如果看懂了前文利用window.name+iframe跨域获取数据,那么此文也就很好理解了.一样都是动态插入一个iframe,然后把iframe的src指向服务端地址,而服务端同样都是输出一段j ...
- 拥Bootstrap入怀——模态框(modal)篇
置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<CSS绘制Android Robot> 作者主页:myvin 博主QQ:851399101(点击QQ和 ...
- TO~亲爱的自己
你累的时候,谢绝别人的肩膀: 你扛不动的时候,拒绝别人的帮忙: 你和别人吃饭,不让别人买单你总是想我这样优秀善良, 总不给别人添麻烦,为什么总是找不到爱的人呢? 有时,爱只有在相处时才能找得到的, 是 ...
- onload是代码在也买你的追加元素的完成,而不是http请求的完成
- nginx 出现413 Request Entity Too Large问题的解决方法
nginx 出现413 Request Entity Too Large问题的解决方法 使用php上传图片(大小1.9M),出现 nginx: 413 Request Entity Too Large ...
- linux 添加永久ip、路由和开启路由功能
一.添加永久ip 编辑/etc/sysconfig/network-scripts/ifcfg-eth0文件: 网络接口配置文件 [root@localhost ~]# cat /etc/syscon ...
- SQL Server中,Numric,Decimal,Money三种字段类型的区别
都是精确数据类型, 前两个可以自己定义长度和小数位数, Money的定义相当于Numric(19,4) numeric(10,2) 表示最大可以放10位数,但这10位数里有2位是小数如: 123456 ...
- HDU 4901 The Romantic Hero
The Romantic Hero Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u D ...
- HD1847 Good Luck in CET-4 Everybody!(巴什博弈)
巴什博弈: 一堆物品n个,最多取m个,最少取1个,最后取走的人获胜 分析:只要保证取玩最后剩m+1个,则必定胜利,所以构造m+1,只要n是 m+1的倍数,则先手必败,每次先手取玩,后手可取使得剩下的仍 ...
- Nagios告警和监控主机安装介绍(三)
Nagios邮件告警 配置sendEmail 解压缩tar –zxvf sendEmail-v1.56.tar.gz cd sendEmail-v1.56 将可执行程序复制cp sendEmail / ...