BestCoder Round #36 (hdu5200)Strange Class(离线)

转载请注明出处: 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,一边树还有,那么块数不变。

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 #include <iostream>
 6 #include <sstream>
 7 #include <ios>
 8 #include <iomanip>
 9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 int Scan()
40 {
41     int res, ch=0;
42     while(!(ch>=‘0‘&&ch<=‘9‘)) ch=getchar();
43     res=ch-‘0‘;
44     while((ch=getchar())>=‘0‘&&ch<=‘9‘)
45         res=res*10+ch-‘0‘;
46     return res;
47 }
48 void Out(int a)
49 {
50     if(a>9)
51         Out(a/10);
52     putchar(a%10+‘0‘);
53 }
54 int h[100100];
55 int q[100100];
56 int p[100100];
57 int px[100100];
58 int ans[100100];
59 bool cmp(int x,int y){
60     if(q[x]==q[y])return x<y;
61     return q[x]<q[y];
62 }
63 bool cmp1(int x,int y){
64     return h[x]<h[y];
65 }
66 int main()
67 {
68     //ios::sync_with_stdio(false);
69     int n,m;
70     while(scanf("%d%d",&n,&m)!=EOF){
71         h[n+1]=-1;h[0]=-1;
72         for(int i=1;i<=n;i++)h[i]=Scan();
73         for(int i=1;i<=n;i++)px[i]=i;
74         sort(px+1,px+n+1,cmp1);
75         for(int i=1;i<=m;i++)q[i]=Scan();
76         for(int i=1;i<=m;i++)p[i]=i;
77         sort(p+1,p+m+1,cmp);
78         int j=1;
79         ans[0]=1;
80         p[0]=0;
81         for(int i=1;i<=m;i++){
82             ans[p[i]]=ans[p[i-1]];
83             while(j<=n&&h[px[j]]<=q[p[i]]){
84                 h[px[j]]=-1;
85                 if(h[px[j]-1]==-1&&h[px[j]+1]==-1)ans[p[i]]--;
86                 else if(h[px[j]-1]>0&&h[px[j]+1]>0)ans[p[i]]++;
87                 j++;
88             }
89         }
90         for(int i=1;i<=m;i++){
91             printf("%d\n",ans[i]);
92         }
93     }
94     return 0;
95 }
时间: 2024-12-28 08:46:36

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

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Strange Class Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description In Vivid’s school, there is a strange class(SC). In SC, the students’ nam

BestCoder Round #36(Strange Class-模拟)

Strange Class Accepts: 519 Submissions: 1749 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪.他们的名字形式是这样的a n b n c n   (a,b,c两两不相同.).例如,叫"abc","ddppqq"的学生是在S

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Gunner Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Long long ago, there is a gunner whose name is Jack. He likes to go hunting very

BestCoder Round #36 [B] Gunner

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5199 先对树的高度排序,然后对每次射击高度二分查找即可,打过之后数目变为0. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #incl

BestCoder Round #36(Trees-离线处理询问)

Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 今天CodeFamer去坎树.有N 棵树排成一排.他们被从1 到N 标号.第i 号树的高度为h i  .两棵未被坎掉的树编号分别为x,y  当且仅当他们满足如下条件中一条时,他们是属于同一个块的: 1) x+1=y 或 y+1=x; 2) 存在一个棵未被坎

BestCoder Round #36

A:签到题,注意情况都考虑全了判断即可 B:hash树高,统计即可,要加读入挂(略坑) C:离线操作,把询问和树高都从大到小排序,然后砍树变成加树,每次把超过当前询问的树都加进去,每次加树就和左右边判断下,记录下块变换的情况,然后把答案存到相应询问中 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str[105]; bool judge()

BestCoder Round #36(Gunner-hash)

Gunner Accepts: 391 Submissions: 1397 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 很久很久以前,有一个叫Jack的枪手.他非常喜欢打猎.一天,他去了一个小树林.那儿有只鸟,还有n 棵树.第i 只鸟站在第i 棵树的顶端.这些树从左到右排成一条直线.每一棵树都有它的高度.Jack站在最左边那棵树的左边.当Jack在高度为H 的地方向右发

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

BestCoder Round #4 前两题 hdu 4931 4932

第一题太水了.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[6]; 7 int main(){ 8 int cas; 9 scanf( "%d", &cas ); 10 while( cas-- ){ 11 for( int i = 0; i <