HDU4585 Shaolin (STL和treap)
Shaolin
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.
InputThere 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.
OutputA 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
treap:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int ans1,ans2,cnt;
struct in
{
int L,R,V,rnd,pos;
}tree[maxn];
void rturn(int &u)
{
int tmp=tree[u].L;
tree[u].L=tree[tmp].R;
tree[tmp].R=u;
u=tmp;
}
void lturn(int &u)
{
int tmp=tree[u].R;
tree[u].R=tree[tmp].L;
tree[tmp].L=u;
u=tmp;
}
void insert(int &u,int v,int ps)
{
if(u==){
u=++cnt;
tree[cnt].V=v;
tree[cnt].pos=ps;
tree[cnt].rnd=rand();
return ;
}
if(tree[u].V>v){
insert(tree[u].L,v,ps);
if(tree[tree[u].L].rnd>tree[u].rnd)
rturn(u);
}
else {
insert(tree[u].R,v,ps);
if(tree[tree[u].R].rnd>tree[u].rnd)
lturn(u);
}
}
void query(int u,int v)
{
if(u==) return ;
if(tree[u].V>v){
ans2=u;//大
query(tree[u].L,v);
}
else {
ans1=u;//小
query(tree[u].R,v);
}
}
int main()
{
int i,j,n,ps,v,root=;
while(scanf("%d",&n)){
if(n==) return ;
memset(tree,,sizeof(tree));
cnt=root=;
insert(root,,);
for(i=;i<=n;i++){
scanf("%d%d",&ps,&v);
printf("%d ",ps);
ans1=ans2=-;
query(root,v);
if(ans1!=-) j=ans1;
else if(ans2!=-) j=ans2;
if(ans1!=-&&ans2!=-){
if(v-tree[ans1].V<=tree[ans2].V-v) j=ans1;
else j=ans2;
}
printf("%d\n",tree[j].pos);
insert(root,v,ps);
}
}
return ;
}
因为前两天用set实现了这道treap题,今天还是想偷懒。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn=;
int a[maxn],b[maxn];
set<int>q;
map<int,int>Map;
int main()
{
int i,n,j,m,ps,u,v;
while(~scanf("%d",&n)){
if(n==) return ;
q.clear();
Map.clear();
q.insert();
Map[]=;
for(i=;i<=n;i++)
{
scanf("%d%d",&u,&v);
set<int> ::iterator it=q.lower_bound(v);
if(it==q.begin()) ps=Map[*it];
else {
j=*it;
it--;
if(j-v>=v-*it) j=*it;
ps=Map[j]; }
q.insert(v);
Map[v]=u;
printf("%d %d\n",u,ps);
}
}
return ;
}
然后发现map居然也是排好序了的,黑人脸。
#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) == && n)
{
mp.clear();
mp[] = ;
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 ;
}
HDU4585 Shaolin (STL和treap)的更多相关文章
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5956 Solved: 2317[Submit][Sta ...
- TOJ5705动态序列操作(STL or treap)
传送门:动态序列操作 在一个动态变化的序列中,完成以下基本操作: (1)插入一个整数 (2)删除一个整数 (3)查找序列中最大的数 (4)查找序列中最小的数 (5)求x的前驱(前驱定义为不大于x的序列 ...
- hdu 4585 Shaolin(STL map)
Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...
- [HDU4585]Shaolin
Problem 问你一个数的前驱和后继 Solution Treap模板题 Notice 注意输出那个人的编号 Code #include<cmath> #include<cstdi ...
- 【HDU4585 Shaolin】map的经典运用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题意大意:很多人想进少林寺,少林寺最开始只有一个和尚,每个人有有一个武力值,若这个人想进少林,必 ...
- HDU 4585 Shaolin (STL)
没想到map还有排序功能,默认按照键值从小到大排序 #include <cstdio> #include <iostream> #include <cstring> ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- HDU 4585 Shaolin(水题,STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HihoCoder1325 : 平衡树·Treap(附STL版本)
平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二 ...
随机推荐
- Python基础(8)_迭代器、生成器、列表解析
一.迭代器 1.什么是迭代 1 重复 2 下次重复一定是基于上一次的结果而来 l=[,,,] count= while count < len(l): print(l[count]) count ...
- 04 Spring框架 依赖注入(一)
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...
- InnoDB存储引擎内存缓冲池管理技术——LRU List、Free List、Flush List
InnoDB是事务安全的MySQL存储引擎,野山谷OLTP应用中核心表的首选存储引擎.他是基于表的存储引擎,而不是基于数据库的.其特点是行锁设计.支持MVCC.支持外键.提供一致性非锁定读,同时被设计 ...
- PHPExcel常用属性使用
PHPExcel常用属性使用 前景: 需先实例化几个变量: $this->objExcel = new PHPExcel(); //实例化一个PHPExcel变量 $this->objE ...
- nginx location proxy pass
nginx: 192.168.1.23作为nginx反向代理机器 目标机器192.168.1.5上部署一个8090端口的nginx [root@localhost conf.d]# cat test. ...
- UI基础_transform
#import "ViewController.h" typedef enum { ButtonTypeLeft = 1, ButtonTypeRight, ButtonTypeU ...
- INSPIRED启示录 读书笔记 - 第33章 新瓶装老酒
精明的公司至少要手握两件“法宝” 1.对目标市场了如指掌,对现有产品的缺陷洞若观火 2.跟踪最亲的技术趋势.新技术层出不穷,让之前无法实现的方案变得可能
- python模块cgihttpserver启动
cgi是web服务器运行web应用的一种机制,web服务器通过执行cgi脚本,然后将该程序的标准输出作为http响应的一部分 CGIHTTPServer是python标准模块的web服务器,它可以运行 ...
- 如何开启和禁止Linux系统的ping功能
在日常的网络维护和使用过程中,ping命令是最为常用的一个检测命令,它所使用的是ICMP协议,但是为了保护主机,很多时候我们需要禁止ICMP协议,在这种情况下,终端再使用ping命令检测,服务器是不会 ...
- 为什么可以Ping通IP地址,但Ping不通域名?
能否ping通IP地址,与能否解析域名是两回事不能ping通IP地址,说明对方禁止ICMP报文或对方没有开机等解析域名只是将域名翻译成IP地址,不论该IP地址是否能够正常访问 问题是ping域名的时候 ...