您的位置:首页 > 其它

HDU 4585 Shaolin(水题,STL)

2013-08-10 18:10 387 查看

Shaolin

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 38


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3
2 1
3 3
4 2
0

[align=left]Sample Output[/align]

2 1
3 2
4 2

[align=left]Source[/align]
2013ACM-ICPC杭州赛区全国邀请赛

STL的set乱搞就可以了

/* **********************************************
Author      : kuangbin
Created Time: 2013/8/10 11:55:20
File Name   : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1011.cpp
*********************************************** */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
set<int>st;
map<int,int>mp;
int n;
while(scanf("%d",&n) == 1 && n)
{
st.clear();
mp.clear();
st.insert(1000000000);
mp[1000000000] = 1;
int u,v;
while(n--)
{
scanf("%d%d",&u,&v);
printf("%d ",u);
set<int>::iterator it = st.lower_bound(v);
if(it == st.end())
{
it--;
printf("%d\n",mp[*it]);
}
else
{
int t1 = (*it);
if(it != st.begin())
{
it--;
if(v - (*it) <= t1 - v)
{
printf("%d\n",mp[*it]);
}
else printf("%d\n",mp[t1]);
}
else printf("%d\n",mp[*it]);
}
mp[v] = u;
st.insert(v);
}
}
return 0;
}


今天才知道原来直接用map也可以实现。

原来map也是排序了的,Orz....

/* **********************************************
Author      : kuangbin
Created Time: 2013/8/10 11:55:20
File Name   : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1011.cpp
*********************************************** */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
map<int,int>mp;
int n;
while(scanf("%d",&n) == 1 && n)
{
mp.clear();
mp[1000000000] = 1;
int u,v;
while(n--)
{
scanf("%d%d",&u,&v);
printf("%d ",u);
map<int,int>::iterator it = mp.lower_bound(v);
if(it == mp.end())
{
it--;
printf("%d\n",it->second);
}
else
{
int t1 = it->first;
int tmp = it->second;
if(it != mp.begin())
{
it--;
if(v - it->first <= t1 - v)
{
printf("%d\n",it->second);
}
else printf("%d\n",tmp);
}
else printf("%d\n",it->second);
}
mp[v] = u;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: