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:就是二 ...
随机推荐
- commonAncestor
commonAncestor 光标或选区所在区域最外层的祖先节点
- Python学习进程(15)常用内置函数
本节介绍Python的一些常用的内置函数. (1)cmp(x, y): cmp()函数比较 x 和 y 两个对象,并根据比较结果返回一个整数,如果 x<y,则返回-1:如果x&g ...
- linux免密登录配置
第一步:安装openssh-clients yum install -y openssh-clients.x86_64第二步:生成密钥 ssh-keygen第三步:拷贝公钥到其他机器 ssh-copy ...
- 封装一个既能遍历数组又能遍历对象的的forEach函数
function newforEach(obj,fn) { var key; if(obj instanceof Array){ obj.forEach(function(item,index){ f ...
- 2017最全的php面试题目及答案总结
最近在网上看到很多的小伙伴们都在询问如何应对php面试,这个对于有工作经验和实战项目的小伙伴来说是没什么问题的,但是对于刚刚学习完php的小伙伴们.php面试却是一个很重要的一步,那么今天php中文网 ...
- ubuntu centos macos 配置上网代理
因为我国强大的GFW,导致很多国外的应用无法安装,因为需要在系统中配置http/https代理. Ubuntu代理配置 配置方式非常简单,在~/.bashrc文件中增加: echo "exp ...
- eclipse oxygen 版本(即为4.7版本)打开 could not create the java virtual machine问题
1.问题: could not create the java virtual machine 2.解决办法: 找到eclipse目录下的eclipse.ini文件.打开找到以下内容: -vmargs ...
- 为什么可以Ping通IP地址,但Ping不通域名?
能否ping通IP地址,与能否解析域名是两回事不能ping通IP地址,说明对方禁止ICMP报文或对方没有开机等解析域名只是将域名翻译成IP地址,不论该IP地址是否能够正常访问 问题是ping域名的时候 ...
- How to Delete using INNER JOIN with SQL Server?
https://stackoverflow.com/questions/16481379/how-to-delete-using-inner-join-with-sql-server You need ...
- python爬虫-韩寒新浪博客博文
博客地址:http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html 爬第一页博文 #-*-coding:utf--*- import re # ...