hdu 4585 Shaolin(STL map)
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.
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.
2 1
3 3
4 2
0
3 2
4 2
题意:讲的是一群无聊的和尚在比武,当来了一个新和尚时要挑选一个战斗力与他最接近的老和尚比武,
如果有两个老和尚和尚与他的战斗力的绝对值相等,则选择战斗力比他小的。
输出这个新和尚的编号和与他比武的老和尚的编号
直接用map标记就行了,
不过还是WA了好几次,说明自己还是很差啊!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 100006
int n;
map<int,int>mp;
map<int,int>::iterator it,it2;
int main()
{
while(scanf("%d",&n)==1 && n)
{
mp.clear();
mp[1000000000]=1;
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
it=mp.lower_bound(y);
if(it==mp.begin())//当新的只能排在第一位时,直接输出第一个
{
printf("%d %d\n",x,it->second);
}
else
{
it2=it;
it--;
if(abs(y-it->first)>abs(y-it2->first))
printf("%d %d\n",x,it2->second);
else
printf("%d %d\n",x,it->second);
}
mp[y]=x;
}
}
return 0;
}
hdu 4585 Shaolin(STL map)的更多相关文章
- HDU 4585 Shaolin (STL)
没想到map还有排序功能,默认按照键值从小到大排序 #include <cstdio> #include <iostream> #include <cstring> ...
- HDU 4585 Shaolin(STL map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
- HDU 4585 Shaolin(Treap找前驱和后继)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
- HDU 4585 Shaolin (STL map)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- [HDU 4585] Shaolin (map应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...
- HDU 4585 Shaolin(水题,STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4585 Shaolin (STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4585 Shaolin(map应用+二分)
题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进 ...
- HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...
随机推荐
- Java基础知识强化73:正则表达式之分割功能
1. 分割功能: 使用String的split方法,split方法:根据给定正则表达式的匹配拆分字符串.如下: public String[] split(String regex): 2. 案例: ...
- Java基础知识强化71:正则表达式之基本规则 和 常用正则表达式
1. 基本规则: A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ('\u000D') B:字符类 [abc] a.b 或 ...
- php的mq客户端获取队列方法改造
获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...
- Hyper-V的三种网卡
External ======= 虚拟机和物理网络.本地主机都能通信 Internal ======= 虚拟机之间互相通信,并且虚拟机能和本机通信 Private ======= 仅允许运行在这台物理 ...
- Android开发手记(14) 使用MediaPlayer播放mp3
1.获取MediaPlayer实例 (1)可以直接通过new或者create方式: 调用setDataSource和create的区别是,create时已经执行了MediaPlayer.prepare ...
- Swift - 36 - 结尾闭包(Trailing closure)和捕获数值(Capturing Values)的简单介绍
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- JavaScript 客户端JavaScript之事件(DOM API 提供模块之一)
具有交互性的JavaScript程序使用的是事件驱动的程序设计模型. 目前使用的有3种完全不同的不兼容的事件处理模型. 1.原始事件模型 (一种简单的事件处理模式) 一般把它看作0级DOM API ...
- angularjs中的绑定策略“@”,“=”,“&”实例
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- jq事件绑定
有些时候我们在页面中会动态的添加一下dom结构,当我们想要给这些结点添加事件时需要在此节点绑定一系列的操作. <a href="#" onclick="addBtn ...
- li的border无法和上边对齐
如果利用border设置四周的border,如果一边的边框比较长无法对齐 解决办法: .test{ position: relative; border: 1px solid #808080; hei ...