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
题意: n个僧侣,每行两个数字分别表示僧侣的 id 和战力值,要求在老僧侣中找到与其战力值最接近的僧侣进行挑战,如果存在多个,则输出编号最小的。挑战完之后该僧侣就可以列为老僧侣,最初只有长老一人,编号为1,战力值1,000,000,000。
题解: 从长老的战力值就可看出这有可能会是一个很长的跨度,肯定不可以用战力值作为数组的下标然后去遍历数组了。应该借用STL里的工具 map
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<int,int>mp;
int main(){
int n;
while(~scanf("%d", &n) && n){
mp.clear();
mp[1000000000] = 1;
while(n--){
int id, g;
scanf("%d%d", &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;
}
然后这题,是可以用terap树解决的,但是呢,我抄着模板,提交之后喜获Runtime Error
还不知道什么原因,代码放着”留念“一下,不是,记录一下,后续学会了再回来debug。
// Runtime Error
// (ACCESS_VIOLATION)
#include<iostream>
#include<algorithm>
#include<time.h>
using namespace std;
int id[5000000 +5];
struct node{
int size, rank, key;
//size以这个节点为根的子树的节点总数量,用于名次树
//rank优先级 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右旋
//d^1 与 1-d 等价,但是更快
node *k = o->son[d^1];
o->son[d^1] = k->son[d];
k->son[d] = o;
o->update();
k->update();
o = k;
}
void insert(node* &o, int 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){
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){
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(){
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=1; 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;
}