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. NavigationDrawer和NavigationView-Android M新控件

    Translucent System Bars-4.4新特性 Toolbar-5.0新特性 NavigationDrawer 简介 NavigationDrawer 是 Google 在 Materi ...

  2. iOS UIScrollView 滚动到当前展示的视图居中展示

    需求展示: 测试效果1 first uiscrollView  宽度 为屏幕宽度   滚动步长 为 scroll 宽度的1/3   分析: 这个是最普通版 无法使每一次滚动的结果子视图居中展示, WA ...

  3. asp.net IRequiresSessionState

    在一般处理程序中,使用context.Session对象,必须先继承IRequiresSessionState接口. System.Web.SessionState.IRequiresSessionS ...

  4. Apache commons-io实现单文件读取和写入

    Apache commons-io提供了很多类,这里只介绍FileUtils类. FileUtils类提供了一些操作文件对象的实用方法,包括文件的读取.写入.复制和比较等. 比如逐句读取和写入一个文件 ...

  5. 如何计算android设备的屏幕物理尺寸

    https://segmentfault.com/q/1010000002794179 通过android提供的接口,我们可以获取到如下的信息:int densityDpi = mContext.ge ...

  6. qt5.4.1的imx6编译

    2.到https://download.qt.io/archive/qt/5.4/5.4.1/single/下载源码包qt-everywhere-opensource-src-5.4.1.tar.gz ...

  7. CentOS 7卸载mariadb安装mysql

    CentOS 7已经将默认集成mariadb而不是mysql,这对于多数还是依赖于mysql的应用来说,需要手动的进行更新. 可能会遇到这样错误,换成MySQL就好了. error 2002 (hy0 ...

  8. .NET 中如何判断文件与目录

    FileInfo fileInfo = new FileInfo(pth); if ((fileInfo.Attributes & FileAttributes.Directory) != 0 ...

  9. thinkphp 多表事务处理

    try{ $this->user = D('User'); $this->user->startTrans(); //开始事务 $res = $this->user->S ...

  10. 安装Tomcat服务器以及错误汇总(tomcat8.0、jdk8)

    Tomcat安装和启动 一.下载Tomcat 你可以直接百度Tomcat官网, 或者,直接在地址栏输入他的官网地址:http://tomcat.apache.org/,然后进入他的主页,在主页左侧可以 ...