Shaolin

HDU - 4585

      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. 

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)的更多相关文章

  1. bzoj1208 [HNOI2004]宠物收养所(STL,Treap)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5956  Solved: 2317[Submit][Sta ...

  2. TOJ5705动态序列操作(STL or treap)

    传送门:动态序列操作 在一个动态变化的序列中,完成以下基本操作: (1)插入一个整数 (2)删除一个整数 (3)查找序列中最大的数 (4)查找序列中最小的数 (5)求x的前驱(前驱定义为不大于x的序列 ...

  3. hdu 4585 Shaolin(STL map)

    Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...

  4. [HDU4585]Shaolin

    Problem 问你一个数的前驱和后继 Solution Treap模板题 Notice 注意输出那个人的编号 Code #include<cmath> #include<cstdi ...

  5. 【HDU4585 Shaolin】map的经典运用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题意大意:很多人想进少林寺,少林寺最开始只有一个和尚,每个人有有一个武力值,若这个人想进少林,必 ...

  6. HDU 4585 Shaolin (STL)

    没想到map还有排序功能,默认按照键值从小到大排序 #include <cstdio> #include <iostream> #include <cstring> ...

  7. hdu 4585 Shaolin treap

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

  8. HDU 4585 Shaolin(水题,STL)

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

  9. HihoCoder1325 : 平衡树·Treap(附STL版本)

    平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二 ...

随机推荐

  1. Docker部署JavaWeb项目实战

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在Ubuntu14.04 64位系统下来创建一个运行Javaweb应用程 ...

  2. php大转盘抽奖

    抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html function getRand($proArr, $proCount) { $result = ''; $ ...

  3. MySQL数据库基本操作(四)

    在进行查询之前,我们要先建好关系表,并往数据表中插入些数据.为查询操作做好准备. 五张关系表的创建: #创建并进入数据库: mysql> CREATE DATABASE `info`; Quer ...

  4. RazorEngine性能研究(反射的延深)

    先说下结论 1)RazorEngine 确实很慢,编译过程特别慢,编译过后仍不适合大量重复调用的情况(一次调用可以接受). 2 )   RazorEngine 和 asp.net mvc 里的Razo ...

  5. yum 源配置

    在 /etc/yum.repos.d 下建立一个 .repo 文件 vim  yum.repo [cd] name=cd baseurl=file:///run/media/root/RHEL-7.0 ...

  6. 出现GC overhead limit exceeded 的解决方案

    当我在使用MyEclispe IDE创建Maven项目的时候出现  "An internal error occurred during: “Build Project”. GC overh ...

  7. CentOS/Linux 卸载MATLAB

    rm -rf /usr/local/MATLAB/R2012arm /usr/local/bin/matlab /usr/local/bin/mcc /usr/local/bin/mex /usr/l ...

  8. 虚拟机开启Linux时出现“我以复制虚拟机”、“我已移动虚拟机”

    当出现标题的情况时,并且网络出现状况时,可以尝试一下解决办法 首先用ifconfig -a命令调出现在的网卡驱动的名称和HWaddr地址,然后再编辑/etc/sysconfig/networking/ ...

  9. 玲珑杯 第4次 String cut(暴力字符串)

    题意:给你长度为n(<=100000)的字符串,问你任意删除一个字符后得到循环节最多的数量是多少 题解:最简单的想法就是枚举删除的字符,再kmp求循环节,但是时间复杂度为O(n*n)会超时 因此 ...

  10. Javascript中的prototype和__proto__的联系区别

    转载至http://www.cnblogs.com/sinstone/p/5136871.html   一.联系 prototype和__proto__都指向原型对象,任意一个函数(包括构造函数)都有 ...