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 ...
随机推荐
- AutoLayout 约束与frame、bounds的设置
介绍 关于AutoLayout的介绍可参考: 使用解读: https://segmentfault.com/a/1190000004386278 iOS 屏幕适配,autoResizing autoL ...
- java数据类型总结
java8大基本数据类型: 整型: byte 字节型 1字节 数据范围:-27 ~ 27- 1 即:-128 ~ 127 short 短整型 2字节 数据范围:-215 ~ ...
- Andriod Studio Clear Project或Rebuild Project出错
以前在Eclipse中出现过类似的错误:在编译工程时,提示无法删除bin目录下的某个jar. 想不到Android Studio中也会有. Clear Project或Rebuild Project, ...
- 【Lucene实验1】构建索引
一.实验名称:构建索引 二.实验日期:2013/9/21 三.实验目的: 1) 能理解Lucene中的Document-Field结构的数据建模过程: 2) 能编针对特定数 ...
- 翻译-微服务API Gateway
原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下你正在建立一个使用微服务模式的网上商 ...
- [USACO2003][poj2110]Mountain Walking(二分答案+bfs)
http://poj.org/problem?id=2110 题意:给你一个n*n矩形(n<=100),每个位置上都有一个数字代表此处山的高度,要从(1,1)走到 (n,n),要求一条路径使得这 ...
- 2016 版 Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】
本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本篇文章中,我将跟大家一起实现 Article ...
- java设计优化-享元模式
享元模式是设计模式中少数几个以调高系统性能为目的的设计模式.它的核心思想是:如果在一个系统中存在多个相同的对象,那么只需共享一份对象的拷贝,而不必为每一次使用都创建新的对象.在享元模式中,由于需要构建 ...
- zoj3882 博弈
我理解错题目意思,稀里糊涂A了.其实就是先手必胜. #include<stdio.h> int main() { int n; while(scanf("%d",&am ...
- 【bzoj4010】 HNOI2015—菜肴制作
http://www.lydsy.com/JudgeOnline/problem.php?id=4010 (题目链接) 题意 给出一张无向图要求出一个拓扑序列满足1的位置最靠前 ,在保证上面的条件下使 ...