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 ...
随机推荐
- MVC5 + EF6 + Bootstrap3 (7) Bootstrap的栅格系统
文章来源: Slark.NET-博客园http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-grid.html 上一节:ASP.NET MVC ...
- 第一个C语言编译器是怎样编写的?
首先向C语言之父Dennis MacAlistair Ritchie致敬! 当今几乎所有的实用的编译器/解释器(以下统称编译器)都是用C语言编写的,有一些语言比如Clojure,Jython等是基于J ...
- 第二章 Js函数
函数的定义二种定义 ①function myfunc () { console("hello"); }; ②var myfunc = function () { console ...
- Bootstrap系列 -- 25. 下拉菜单分割线
在Bootstrap框架中的下拉菜单还提供了下拉分隔线,假设下拉菜单有两个组,那么组与组之间可以通过添加一个空的<li>,并且给这个<li>添加类名“divider”来实现添加 ...
- tmux列表重命名
查看tmux会话列表时,会话名称是数值递增,不易识别 tmux ls 1: 1 windows (created Fri Oct 21 16:29:46 2016) [175x41]2: 1 wind ...
- [BZOJ 1816][Cqoi2010]扑克牌(二分答案)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1816 分析: 我先以为是道水题,但是要注意的是每套牌中Joker只能用1张的,所以就出现了可能 ...
- 第六章:javascript:字典
字典是一种以键-值对应形式存储的数据结构,就像电话薄里的名字和电话号码一样.只要找一个电话,查找名字,名字找到后,电话号码也就找到了.这里的键值是你用来查找的东西,值就是要查的到的结果. javasc ...
- 每天一个linux命令(39):iostat命令
Linux系统中的 iostat 是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会 汇报出CPU使用情况 ...
- 使用git管理代码的心得
一.简易使用流程 首先下载安装git,点击Git Bash进入编辑界面,之后如下图进入目录并通过命令 git init 把这个目录变成git可以管理的仓库 接下来使用git add .命令将所有文件添 ...
- HDU 5973 Game of Taking Stones 威佐夫博弈+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...