转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Trees

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 \leq N, Q \leq 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.

 
其实这题离线搞就变得挺水了,从低的开始算,每次砍掉这颗变成小于等于0时就有三种情况:
1.左边和右边已经是-1了,那么块数-1;
2.左边和右边的树都还有,那么块数+1;
3.其中一边是-1,一边树还有,那么块数不变。
 
 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int Scan()
{
int res, ch=;
while(!(ch>=''&&ch<='')) ch=getchar();
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return res;
}
void Out(int a)
{
if(a>)
Out(a/);
putchar(a%+'');
}
int h[];
int q[];
int p[];
int px[];
int ans[];
bool cmp(int x,int y){
if(q[x]==q[y])return x<y;
return q[x]<q[y];
}
bool cmp1(int x,int y){
return h[x]<h[y];
}
int main()
{
//ios::sync_with_stdio(false);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
h[n+]=-;h[]=-;
for(int i=;i<=n;i++)h[i]=Scan();
for(int i=;i<=n;i++)px[i]=i;
sort(px+,px+n+,cmp1);
for(int i=;i<=m;i++)q[i]=Scan();
for(int i=;i<=m;i++)p[i]=i;
sort(p+,p+m+,cmp);
int j=;
ans[]=;
p[]=;
for(int i=;i<=m;i++){
ans[p[i]]=ans[p[i-]];
while(j<=n&&h[px[j]]<=q[p[i]]){
h[px[j]]=-;
if(h[px[j]-]==-&&h[px[j]+]==-)ans[p[i]]--;
else if(h[px[j]-]>&&h[px[j]+]>)ans[p[i]]++;
j++;
}
}
for(int i=;i<=m;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
 
 

BestCoder Round #36 (hdu5200)Strange Class(离线)的更多相关文章

  1. BestCoder Round #36 (hdu5198)Strange Class(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Strange Class Time Limit: 2000/1000 MS (J ...

  2. BestCoder Round #36

    HDU5198 Strange Class 问题描述 在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪.他们的名字形式是这样的anbncn(a,b,c两两不相同.).例 ...

  3. BestCoder Round #36 [B] Gunner

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5199 先对树的高度排序,然后对每次射击高度二分查找即可,打过之后数目变为0. #include< ...

  4. BestCoder Round #36 (hdu5199)Gunner(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Gunner Time Limit: 8000/4000 MS (Java/Oth ...

  5. 二分查找 BestCoder Round #36 ($) Gunner

    题目传送门 /* 题意:问值为x的个数有几个,第二次查询就是0 lower/upper_bound ()函数的使用,map也可过,hash方法不会 */ #include <cstdio> ...

  6. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  7. BestCoder Round #75 1001 - King's Cake

    Problem Description It is the king's birthday before the military parade . The ministers prepared a ...

  8. BestCoder Round #14

    Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

随机推荐

  1. 菜鸟做HTML5小游戏 - 翻翻乐

    记录下开放过程.做小游戏开发,又要跨平台,flash又不支持iPhone,html5是最好的选择. 先看看最后效果: 好了,开始demo. 1.准备工作: 图片素材(省略...最后代码一起打包) 了解 ...

  2. [linux]磁盘挂载

    最近磁盘空间不足了, 所以需要将更多的磁盘空间加进来. 因为目前占空间最多的就是home, 无论是下载还是本地用户的东西都是放在这里的. 将分区格式化为ext4, 然后使用blkid /dev/sda ...

  3. Android 源码编译环境搭建(64位Ubuntu)各种依赖包安装

    1.准备: 普通PC(要求能上网), PC的操作系统Ubuntu 10.04 LTS(64位的),已经下载好的Android 1.6_r1的源代码. 2.Linux的依赖package安装: 为了更快 ...

  4. Gradle Android客户端程序打包(基于gradle 2.10版本验证通过)

    一.前言 目前正在准备从eclipse开发环境向AndroidStudio迁移,提前过去探探路,不出所料,原来gradle脚本果然报错,无法运行,想想索性把本地的gradle一起升级到最新版本,毕竟1 ...

  5. ServletContext对象(每个工程只有一个此对象)

    一]重点方法:        1>存取对象                        void setAttribute(String name, Object object);//将obj ...

  6. DDUI For Delphi Seattle Directui界面组件

    http://www.delphigear.cn/0/11258/go.aspx http://bbs.csdn.net/topics/390285613

  7. 由于 UNION ALL Chinese_PRC_CI_AS”之间的排序规则冲突,值的排序规则未经解析

    由于不同的表之间的排序规则不一样,在归并集合的 时候会出现排序问题. 只要在查询的列后面 声明结果列的排序规则保持一致即可:  SELECT b0.[CardCode] collate SQL_Lat ...

  8. jquery+ajax分页

    先看效果图:

  9. mysql出现错误“ Every derived table must have its own alias”

    Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时 ...

  10. 走进C++程序世界-------类的定义和使用(数据成员和方法成员,析构函数,构造函数,内联实现)

    类的成员简介 在C++中,可以通过声明一个类来穿件一种新的类型.类将一组变量(他们的类型通常不同)和一组相关的函数组合在一起.类可以有各种类型的变量组成,还可以包含其他类对象.成员变量称为数据成员它们 ...