转载请注明出处: 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 }