hdu 4585 map **

题意:

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.

map的这种用法还没见过呢,下次重拍一遍

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <math.h>
11 #include <stdlib.h>
12 using namespace std;
13
14 int main()
15 {
16     map<int,int>mp;
17     int n;
18     while(scanf("%d",&n) == 1 && n)
19     {
20         mp.clear();
21         mp[1000000000] = 1;
22         int u,v;
23         while(n--)
24         {
25             scanf("%d%d",&u,&v);
26             printf("%d ",u);
27             map<int,int>::iterator it = mp.lower_bound(v);
28             if(it == mp.end())
29             {
30                 it--;
31                 printf("%d\n",it->second);
32             }
33             else
34             {
35                 int t1 = it->first;
36                 int tmp = it->second;
37                 if(it != mp.begin())
38                 {
39                     it--;
40                     if(v - it->first <= t1 - v)
41                     {
42                         printf("%d\n",it->second);
43                     }
44                     else printf("%d\n",tmp);
45                 }
46                 else printf("%d\n",it->second);
47             }
48             mp[v] = u;
49         }
50     }
51     return 0;
52 }

2015/7/6

时间: 2024-10-22 12:29:15

hdu 4585 map **的相关文章

[HDU 4585] Shaolin (map应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map,先用upper_bound找到大的,然后迭代器减1,就能够找到相近的两个. 然后..用链表不知道为什么有问题.... 而且迭代器it,如果减1的话,不能写 it2 = --it1; 这样会wa 但是..it2 = it1; it2--;这样就AC了,在这里记录一下,今后注意. 1 //#pragm

HDU 4585 Shaolin(map应用+二分)

题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进入少林的先后顺序,求出每个和尚进去的时候应当和哪个和尚比武. #include<map> #include<iostream> using namespace std; int main() { int n,id,g; map<int,int>::iterator it,p

HDU 4585 Shaolin(STL map)

Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4585 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 mon

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 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 mast

HDU 4585

http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用map也可以,两个内部都是红黑树实现 水题一道 #include <iostream> #include <cstdio> #include <set> #include <cmath> using namespace std ; struct node { i

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

hdu 1754  I Hate It (线段树)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 35640    Accepted Submission(s): 14034 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的

hdu 1800 (map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10830    Accepted Submission(s): 3472 Problem Description In the year 8888, t