hdu 4585 Shaolin

原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<iostream>
  4 #include<algorithm>
  5 const int Max_N = 101000;
  6 const int INF = 0x7fffffff;
  7 struct Node{
  8     int v, id, s;
  9     Node *ch[2];
 10     inline void
 11     set(int _v = 0, int _id = 0, int _s = 0, Node *p = NULL){
 12         ch[0] = ch[1] = p;
 13         v = _v, id = _id, s = _s;
 14     }
 15     inline void push_up(){
 16         s = ch[0]->s + ch[1]->s + 1;
 17     }
 18     inline int cmp(int x) const{
 19         return x > v;
 20     }
 21 };
 22 struct SizeBalanceTree{
 23     Node *tail, *root, *null;
 24     Node stack[Max_N];
 25     void init(){
 26         tail = &stack[0];
 27         null = tail++;
 28         null->set();
 29         root = null;
 30     }
 31     inline Node *newNode(int v, int id){
 32         Node *p = tail++;
 33         p->set(v, id, 1, null);
 34         return p;
 35     }
 36     inline void rotate(Node* &x, int d){
 37         Node *k = x->ch[!d];
 38         x->ch[!d] = k->ch[d];
 39         k->ch[d] = x;
 40         k->s = x->s;
 41         x->push_up();
 42         x = k;
 43     }
 44     inline void Maintain(Node* &x, int d){
 45         if (x->ch[d] == null) return;
 46         if (x->ch[d]->ch[d]->s > x->ch[!d]->s){
 47             rotate(x, !d);
 48         } else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s){
 49             rotate(x->ch[d], d), rotate(x, !d);
 50         } else {
 51             return;
 52         }
 53         Maintain(x, 0), Maintain(x, 1);
 54     }
 55     inline void insert(Node* &x, int v, int id){
 56         if (x == null){
 57             x = newNode(v, id);
 58             return;
 59         } else {
 60             x->s++;
 61             int d = x->cmp(v);
 62             insert(x->ch[d], v, id);
 63             x->push_up();
 64             Maintain(x, d);
 65         }
 66     }
 67     inline void insert(int v, int id){
 68         insert(root, v, id);
 69     }
 70     inline Node *select(Node *x, int v, int dx){
 71         Node *pre = null;
 72         while (x != null && x->v != v){
 73             int d = x->cmp(v);
 74             if (d == !dx) pre = x;
 75             x = x->ch[d];
 76         }
 77         x = x->ch[dx];
 78         if (x == null){
 79             return pre;
 80         } else {
 81             while (x->ch[!dx] != null) x = x->ch[!dx];
 82             return x;
 83         }
 84     }
 85     inline int calc(Node *r, int k) {
 86         if (r == null) return INF;
 87         return std::abs(r->v - k);
 88     }
 89     inline void gogo(int id, int v){
 90         int ans = 0;
 91         Node *k1 = select(root, v, 0);
 92         Node *k2 = select(root, v, 1);
 93         int d1 = calc(k1, v), d2 = calc(k2, v);
 94         ans = d1 > d2 ? k2->id : k1->id;
 95         printf("%d %d\n", id, ans);
 96     }
 97 }sbt;
 98 int main(){
 99 #ifdef LOCAL
100     freopen("in.txt", "r", stdin);
101     freopen("out.txt", "w+", stdout);
102 #endif
103     int n, id, v;
104     while (~scanf("%d", &n) && n){
105         sbt.init();
106         sbt.insert((int)1e9, 1);
107         for (int i = 0; i < n; i++){
108             scanf("%d %d", &id, &v);
109             sbt.insert(v, id);
110             sbt.gogo(id, v);
111         }
112     }
113     return 0;
114 }

时间: 2024-10-11 04:25:27

hdu 4585 Shaolin的相关文章

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

HDU 4585 Shaolin(STL map)

Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4585 Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a mon

HDU 4585 Shaolin(Treap找前驱和后继)

Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 3191    Accepted Submission(s): 1350 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao

hdu 4585 Shaolin两种方法(暴力和STL map set)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

[HDU 4585] Shaolin (map应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map,先用upper_bound找到大的,然后迭代器减1,就能够找到相近的两个. 然后..用链表不知道为什么有问题.... 而且迭代器it,如果减1的话,不能写 it2 = --it1; 这样会wa 但是..it2 = it1; it2--;这样就AC了,在这里记录一下,今后注意. 1 //#pragm

hdu 4585 shaolin 平衡树

Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but

hdu 4585 Shaolin(STL map)

Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism script

HDU 4585 Shaolin(map应用+二分)

题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进入少林的先后顺序,求出每个和尚进去的时候应当和哪个和尚比武. #include<map> #include<iostream> using namespace std; int main() { int n,id,g; map<int,int>::iterator it,p

HDU 4585 Shaolin (set的应用)

set是STL中非常方便的工具,可以实现自动去重和排序,可我一直忽视它的重要性,导致吃了好几次亏. 在思考这道题的时候,我一直往二分上靠拢,可是二分需要直接插入排序,直接插入排序覆盖的时候复杂度最大是n,肯定不行,所以又想链表,结果链表又没办法二分,着实让我相当矛盾. 最后才发现自己忘了这么一个现成的好宝贝,set中有一个lower_bound(num)函数,可以返回第一个大于等于num的数,自动排序,自动二分,一切实现自动化……时间跑了700ms,作为stl的东西也不错,网上博主对stl中ma

HDU 4585 Shaolin (map)

题意: 思路: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<map> #include<algorithm> using namespace