Shaolin

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

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+1个和尚,他们都有一个独有的编号和战斗力值,当一个年轻人通过所有考试并被宣布为少林的新僧人时,将会有一场战斗,作为欢迎的一部分。新和尚必须与一位战斗等级最接近他的战斗等级的老和尚战斗。如果有两个老僧人满足这个条件,新僧侣将采取战斗等级低于他的僧侣与他对打。现在保证输入是按照编号顺序升序输入的,要求按顺序输出每一组战斗的双方编号,先输出新和尚的后输出老和尚的。(第一个和尚编号是1,战斗力是1000000000)

题目分析:

这个题充分体现出了map的优势,因为map存储的时候是按照关键字升序存储的,所以如果按照战斗力值作为关键字建立map的话,找到此战力值的前驱和后继就是可能要与新和尚对打的老和尚了。

代码:

#include<bits/stdc++.h>

using namespace std;
map<int,int>a;
int n,id,level,i;
int main()
{
while(scanf("%d",&n)!=EOF,n!=)
{
a.clear();
a[]=;
for(i=;i<=n;i++)
{
cin>>id>>level;
a[level]=id;
map<int,int>::iterator p=a.find(level);
if(p==a.begin())
printf("%d %d\n",id,(++p)->second);
else if(p==a.end())
printf("%d %d\n",id,(--p)->second);
else if((abs((++p)->first)-(--p)->first)<abs((--p)->first-(++p)->first))
printf("%d %d\n",id,(++p)->second);
else
printf("%d %d\n",id,(--p)->second);
}
}
}

HDU 4585 Shaolin (STL map)的更多相关文章

  1. HDU 4585 Shaolin(STL map)

    Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit cid= ...

  2. HDU 4585 Shaolin (STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  3. HDU 1263 水果 (STL map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

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

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  5. HDU 4585 Shaolin(水题,STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. HDU 2094 产生冠军(STL map)

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. hdu 4858 项目管理(STL集装箱)

    项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. CodeForces 501B Misha and Changing Handles(STL map)

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  9. 【UVA】10391 Compound Words(STL map)

    题目 题目     分析 自认已经很简洁了,虽说牺牲了一些效率     代码 #include <bits/stdc++.h> using namespace std; set <s ...

随机推荐

  1. WTL项目各种error的解决方法

    error RC1015: cannot open include file 'atlres.h' 解决办法: 向“VC项目属性->资源->附加包含目录” 添加WTL的Include目录( ...

  2. 【Winform-GataGridView】根据DataGridView中的数据内容设置行的文字颜色、背景色 — 根据状态变色

    C#中可以根据每行内容的不同来对DataGridView数据表格控制每行的文字颜色.背景颜色进行不同的设置. 效果如下: 实现: 在DataGridView的RowPrePaint事件中进行行颜色控制 ...

  3. 手写一个类加载器demo

    1.什么是类加载器? 2.加载方式 ClassLoader类加载器,主要的作用是将class文件加载到jvm虚拟机中.jvm启动的时候,并不是一次性加载所有的类,而是根据需要动态去加载类,主要分为隐式 ...

  4. 《剑指offer》算法题第一天

    按照个人计划,从今天开始做<剑指offer>上面的算法题,练习平台为牛客网,上面对每道题都有充分的测试实例,感觉还是很不错的.今天下午做了四道题,分别为: 1. 二叉树的深度(书55题) ...

  5. Java多线程和并发(七),synchronized

    目录 1.线程安全的主要原因 2.互斥锁的特性 3.锁的类别 4.类锁和对象锁的总结 七.synchronized 1.线程安全的主要原因 2.互斥锁的特性 Java中synchronized锁的不是 ...

  6. 计蒜客 2018南京网络赛 I Skr ( 回文树 )

    题目链接 题意 : 给出一个由数字组成的字符串.然后要你找出其所有本质不同的回文子串.然后将这些回文子串转化为整数后相加.问你最后的结果是多少.答案模 1e9+7 分析 : 应该可以算是回文树挺裸的题 ...

  7. Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )

    题目链接 题意 : 中文题.点链接 分析 : 前置技能是 SG 函数.NIM博弈变形 每次可取石子是约数的情况下.那么就要打出 SG 函数 才可以去通过异或操作判断一个局面的胜负 打 SG 函数的时候 ...

  8. poj 2976 Dropping tests 二分搜索+精度处理

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8349   Accepted: 2919 De ...

  9. 实现一个成熟的底层毛玻璃效果(纯CSS)

    写在前面 毛玻璃背景是一个很常见的网页样式,想要实现,其实并不难,但经过我在网上的搜索发现,大量实现方法都较为不规范,且把问题复杂化了(例如各种z-index属性和position的定位)现提供一个代 ...

  10. 【转】diamond专题(二)– 核心原理介绍

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...