Trees
Accepts: 156
Submissions: 533
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≤N,Q≤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.
题解:
窝题目看错了,,悲催啊。。题解以代码均来自小微哥。
对树的高度和查询的高度都分别排序。
然后,两个指针操作,O(n+m)复杂度。
13341721 | 2015-04-04 21:14:13 | Accepted | 5200 | 702MS | 4588K | 1496 B | C++ | czy |
代码来自小微哥:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 #define M 500005 7 8 struct point{ 9 int i, v; 10 }p[M]; 11 struct node 12 { 13 int v; 14 int cur; 15 }s[M]; 16 int has[M], res[M]; 17 18 bool cmp (point a, point b) 19 { 20 return a.v > b.v; 21 } 22 int S() 23 { 24 int ret=0,ok=0; 25 char c; 26 while((c=getchar())) 27 { 28 if(c>=‘0‘&&c<=‘9‘) 29 ret=(ret<<3)+ret+ret+c-‘0‘,ok=1; 30 else if(ok) 31 return ret; 32 } 33 return ret; 34 } 35 bool cmp2(node a,node b) 36 { 37 return a.v<b.v; 38 } 39 int main() 40 { 41 int n, d, i, j, ans; 42 while (~scanf("%d%d",&n,&d)) 43 { 44 for (i = 0; i < n; i++) 45 { 46 p[i].v=S(); 47 p[i].i = i+1; 48 } 49 sort(p, p+n, cmp); 50 for (j = 0; j < d; j++) 51 { 52 s[j].v=S(); 53 s[j].cur=j; 54 } 55 sort(s,s+d,cmp2); 56 memset(has, 0, sizeof(has)); 57 ans = 0; 58 for (i = 0, j = d - 1; j >= 0; j--) 59 { 60 for ( ; i < n; i++) 61 { 62 if (p[i].v <= s[j].v) 63 break; 64 int id = p[i].i; 65 has[id] = 1; 66 if (!has[id-1] && !has[id+1]) ++ans; 67 else if (has[id-1] && has[id+1]) --ans; 68 } 69 res[s[j].cur] = ans; 70 } 71 for (i = 0; i < d; i++) 72 { 73 printf ("%d\n", res[i]); 74 } 75 } 76 return 0; 77 }