Shaolin
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
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 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.
Input
There 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.
Output
A 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
Source
2013ACM-ICPC杭州赛区全国邀请赛
Submit
STL map代码
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp; // it->first是等级,it->second是id
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
mp.clear();
mp[1000000000] = 1; //方丈1,等级1000000000
while (n--)
{
int id, g;
scanf("%d %d", &id, &g); //新和尚id,等级是g
mp[g] = id; //新和尚进队
int ans;
map<int, int>::iterator it = mp.find(g); //找到排好序的位置
if (it == mp.begin())
ans = (++it)->second;
else
{
map<int, int>::iterator it2 = it;
it2--;
it++; //等级接近的前后两个和尚
if (g - it2->first <= it->first - g)
ans = it2->second;
else
ans = it->second;
}
printf("%d %d\n", id, ans);
}
}
return 0;
}
Treap(名次树)代码
#include <bits/stdc++.h>
using namespace std;
int id[5000000 + 5];
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
{
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 temp = find(o->son[d], k);
if (temp == -1)
return -1;
else
return o->son[1] == NULL ? temp + 1 : temp + 1 + o->son[1]->size;
}
}
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
srand(time(NULL));
int k, g;
scanf("%d %d", &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;
printf("%d %d\n", k, 1);
for (int i = 2; i <= n; ++i)
{
scanf(" %d %d", &k, &g);
id[g] = k;
insert(root, g);
int t = find(root, g); //返回新和尚的名次
int ans1, ans2, ans;
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;
printf("%d %d\n", k, id[ans]);
}
}
return 0;
}