Shaolin HDU - 4585 STL map||Treap树
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 fighting skill is also taken into account.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
InputThere are several test cases.
In each test case:
The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
OutputA fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.Sample Input
3 2 1 3 3 4 2 0
Sample Output
2 1 3 2 4 2
先对老和尚level排序,加入一个新和尚时,找到level最接近的老和尚,输出他的id,n比较大,因此复杂度为O(nlog2n)
1)STL map,set都是用二叉搜索树(binary search tree BST)实现的,此题用map
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = 4e18+10; const int mod = 1000000007; const int mx = 10005; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, 0, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pair void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } int m, n,k,sum=0,ans=0,t; map<int, int>mp;//it->first:level,it->second:id int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin>>n&&n) { mp.clear(); mp[1e9] = 1;//方丈id是1,level是1e9 while (n--) { int id, g; cin >> id >> g;//新和尚 mp[g] = id;//进队 map<int, int>::iterator it = mp.find(g);//找到排好序的位置 if (it == mp.begin())ans = (++it)->second; else { map<int, int>::iterator it2 = it; it2--, it++;//level接近的前后两个和尚 if (g - it2->first <= it->first - g) ans = it2->second; else ans = it->second; } cout << id << ' ' << ans << endl; } } return 0; }
2)Treap树
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = 4e18+10; const int mod = 1000000007; const int mx = 5e6+5; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, 0, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pair void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } int m, n,k,sum=0,ans=0,t; int id[mx]; struct node { int size;//以这个节点为根的子树的节点总数,用于名次树 int rank;//优先级 int key;//键值 node *son[2];//son[0]左儿子,son[1]右儿子 bool operator<(const node& a)const { return rank < a.rank;} int cmp(int x)const { if (x == key)return -1; return x < key ? 0 : 1; } void update() { size = 1; if (son[0] != NULL)size += son[0]->size; if (son[1] != NULL)size += son[1]->size; } }; void rotate(node* &o, int d) {//d=0,左旋,d=1,右旋 node *k = o->son[d ^ 1];//d^1等价于1-d,但是更快 o->son[d ^ 1] = k->son[d]; k->son[d] = o; o->update(); k->update(); o = k; } void insert(node* &o, int x) {//把x插入树中 if (o == NULL) { o = new node(); o->son[0] = o->son[1] = NULL; o->rank = rand(); o->key = x; o->size = 1; } else { int d = o->cmp(x); insert(o->son[d],x); o->update(); if (o < o->son[d]); rotate(o, d ^ 1); } } int kth(node* o, int k) {//返回第k大的数 if (o == NULL || k <= 0 || k > o->size) return -1; int s = o->son[1] == NULL ? 0 : o->son[1]->size; if (k == s + 1)return o->key; else if (k <= s)return kth(o->son[1], k); else return kth(o->son[0], k - s - 1); } int find(node* o, int k) {//返回元素k的名次 if (o == NULL) return -1; int d = o->cmp(k); if (d == -1) return o->son[1] == NULL ? 1 : o->son[1]->size + 1; else if (d == 1)return find(o->son[d],k); else { int tmp = find(o->son[d], k); if (tmp == -1) return -1; else { return o->son[1] == NULL ? tmp + 1 : tmp + 1 + o ->son[1]->size; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin>>n&&n) { srand(time(NULL)); int k, g; cin >> k >> g; node* root = new node(); root->son[0] = root-> son[1] = NULL; root->rank = rand(); root->key = g; root->size = 1; id[g] = k; cout << k << ' ' << 1<<endl; re(i, 2, n + 1) { cin >> k >> g; id[g] = k; insert(root, g); t = find(root, g);//返回新和尚的名次 int ans1, ans2; ans1 = kth(root, t - 1);//前一名的老和尚 ans2 = kth(root, t + 1);//后一名的老和尚 if (ans1 != -1 && ans2 != -1) { ans = ans1 - g >= g - ans2 ? ans2 : ans1; } else if (ans1 == -1) ans = ans2; else ans = ans1; cout << k << ' ' << id[ans] << endl; } } return 0; }