HDU 5200 Trees 二分

题目链接:

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=574&pid=1003

题解:

把输入按照高度排序,离线处理出所有高度的答案,每次查询的时候二分查找(upper_bound)。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 50101;
 8 const int mod = 1e9 + 7;
 9 typedef long long LL;
10 
11 struct Node {
12     int id, h;
13     bool operator < (const Node& rhm) const {
14         return h<rhm.h;
15     }
16 }nds[maxn];
17 
18 int n, q;
19 int ans[maxn];
20 bool cuted[maxn];
21 // int fa[maxn];
22 
23 void init() {
24     // for(int i=0;i<maxn;i++) fa[i]=i;
25     memset(cuted, 0, sizeof(cuted));
26     cuted[0] = cuted[n + 1] = 1;
27 }
28 
29 int solve(int x) {
30     //超出去的部分要先处理掉
31     if (x<nds[0].h) return 1;
32     if (x >= nds[n - 1].h) return 0;
33     int low = 0, hig = n;
34     while (low + 1<hig) {
35         int mid = low + (hig - low) / 2;
36         if (nds[mid].h <= x) low = mid;
37         else hig = mid;
38     }
39     return ans[low];
40 }
41 
42 int main() {
43     while (scanf("%d%d", &n, &q) == 2 && n) {
44         init();
45         for (int i = 0; i<n; i++) {
46             scanf("%d", &nds[i].h);
47             nds[i].id = i + 1;
48         }
49         sort(nds, nds + n);
50         int num = 1;
51         for (int i = 0; i<n; i++) {
52             int id = nds[i].id;
53             if (!cuted[id - 1] && !cuted[id + 1]) {
54                 num++;
55             }
56             else if (cuted[id - 1] && cuted[id + 1]) {
57                 num--;
58             }
59             cuted[id] = 1;
60             ans[i] = num;
61         }
62         while (q--) {
63             int x;
64             scanf("%d", &x);
65             printf("%d\n", solve(x));
66         }
67     }
68     return 0;
69 }
70 /*
71 1 100
72 1
73 0
74 2
75 */

直接用upper_bound():

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 50101;
 8 const int mod = 1e9 + 7;
 9 typedef long long LL;
10 
11 struct Node {
12     int id, h;
13     Node(int id, int h) :id(id), h(h) {}
14     Node() {}
15     bool operator < (const Node& rhm) const {
16         return h<rhm.h;
17     }
18 }nds[maxn];
19 
20 int n, q;
21 int ans[maxn];
22 bool cuted[maxn];
23 // int fa[maxn];
24 
25 void init() {
26     // for(int i=0;i<maxn;i++) fa[i]=i;
27     memset(cuted, 0, sizeof(cuted));
28     cuted[0] = cuted[n + 1] = 1;
29 }
30 
31 int solve(int x) {
32     //超出去的部分要先处理掉
33     if (x<nds[0].h) return 1;
34     if (x >= nds[n - 1].h) return 0;
35     //upper_bound找到的是x后面的一个数,所以要减一
36     return ans[upper_bound(nds, nds + n, Node(0,x))-nds-1];
37 }
38 
39 int main() {
40     while (scanf("%d%d", &n, &q) == 2 && n) {
41         init();
42         for (int i = 0; i<n; i++) {
43             scanf("%d", &nds[i].h);
44             nds[i].id = i + 1;
45         }
46         sort(nds, nds + n);
47         int num = 1;
48         for (int i = 0; i<n; i++) {
49             int id = nds[i].id;
50             if (!cuted[id - 1] && !cuted[id + 1]) {
51                 num++;
52             }
53             else if (cuted[id - 1] && cuted[id + 1]) {
54                 num--;
55             }
56             cuted[id] = 1;
57             ans[i] = num;
58         }
59         while (q--) {
60             int x;
61             scanf("%d", &x);
62             printf("%d\n", solve(x));
63         }
64     }
65     return 0;
66 }
67 /*
68 1 100
69 1
70 0
71 2
72 */

 

posted @ 2016-04-19 23:00  fenicnn  阅读(174)  评论(0编辑  收藏  举报