码迷,mamicode.com
首页 > 其他好文 > 详细

传统_POJ-2528

时间:2015-02-08 11:27:20      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://poj.org/problem?id=2528

搞一个简单的映射就可以了,代码如下

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define lowbit(a) ((a)&(-a))
 7 #define max(a, b) ((a)>(b)?(a):(b))
 8 #define min(a, b) ((a)<(b)?(a):(b))
 9 #define MAXN 10010
10 #define PI 3.1415926
11 #define E 2.718281828
12 #define INF 0x777777f
13 typedef long long LL;
14 
15 struct heap{
16     #define hMAXN 10010
17     int hCnt, a[hMAXN], idx[hMAXN][2];
18     inline void clear(){hCnt = 0;}
19     inline void swp(int p, int q){a[q] = a[p]; idx[q][0] = idx[p][0];}
20     inline void maintain(int p){idx[idx[p][0]][1] = p;}
21     void swim(int p){
22         int tmp[] = {a[p], idx[p][0]};
23         for (int q = p >> 1; q > 0; p = q, q >>= 1){
24             if (a[q] > tmp[0]){
25                 swp(q, p); maintain(p);
26             } else break;
27         } a[p] = tmp[0]; idx[p][0] = tmp[1]; maintain(p);
28     }
29     void sink(int p){
30         int tmp[] = {a[p], idx[p][0]};
31         for (int q = p << 1; q <= hCnt; p = q, q <<= 1){
32             if (q < hCnt && a[q] > a[q+1]) q++;
33             if (a[q] < tmp[0]){
34                 swp(q, p); maintain(p);
35             } else break;
36         } a[p] = tmp[0]; idx[p][0] = tmp[1]; maintain(p);
37     }
38     void push(int x){a[++hCnt] = x; swim(hCnt);}
39     void push(int x, int p){a[++hCnt] = x; idx[hCnt][0] = p; swim(hCnt);}
40     int pop(int p){int ret = a[p]; swp(hCnt, p); a[hCnt] = idx[hCnt][0] = 0; hCnt--; swim(p); sink(p); return ret;}
41 } h;
42 struct node{
43     int a, b, c;
44 } x[MAXN*2];
45 int n, vis[MAXN];
46 
47 bool cmp(node a, node b){return (a.a<b.a)||(a.a==b.a)&&(a.b<b.b);}
48 void init(){
49     scanf("%d", &n);
50     for (int i = 1; i <= n; i++){
51         vis[i] = 0;
52         scanf("%d%d", &x[i*2-1].a, &x[i*2].a); x[i*2].a++;
53         x[i*2-1].b = 0; x[i*2-1].c = i;
54         x[i*2].b = 1;    x[i*2].c = i;
55     }
56     sort(x+1, x+1+2*n, cmp);
57     h.clear();
58 }
59 void work(){
60     for (int i = 1; i < 2*n; i++){
61         for ( ; x[i].a==x[i+1].a; i++){
62             if (!x[i].b){h.push(-x[i].c, x[i].c);}
63             else {h.pop(h.idx[x[i].c][1]);}
64         }
65         if (!x[i].b){h.push(-x[i].c, x[i].c);}
66         else {h.pop(h.idx[x[i].c][1]);}
67         if (h.hCnt) vis[-h.a[1]] = 1;
68     }
69     int ans = 0;
70     for (int i = 1; i <= n; i++)
71         if (vis[i]) ans++;
72     printf("%d\n", ans);
73 }
74 int main(){
75     int t; scanf("%d", &t);
76     for (int i = 0; i < t; i++){
77         init();
78         work();
79     }
80     return 0;
81 }

传统_POJ-2528

标签:

原文地址:http://www.cnblogs.com/zhu8655/p/4279748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!