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. linux常用技巧(资料)

    Linux中查看程序安装位置 如果是rpm的安装,用rpm -ql如果是一般安装 用 whereis 或者 find find /usr -name catalina.out======== 如何查看 ...

  2. Struts2笔记01——基础MVC架构(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Apache Struts 2是用来创建企业级Java ...

  3. 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境

    搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...

  4. 【Flask】Flask-Sqlalchemy使用笔记

    ### 安装:```shellpip install flask-sqlalchemy``` ### 数据库连接:1. 跟sqlalchemy一样,定义好数据库连接字符串DB_URI.2. 将这个定义 ...

  5. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  6. c++ boost库学习三:实用工具

    noncopyable 大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等. 这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c= ...

  7. 【bzoj1299】[LLH邀请赛]巧克力棒(博弈论思维题)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1299 首先我们把每根巧克力棒看成一堆石子,把巧克力棒的长度看作石子的个数,那么原问题就 ...

  8. LeetCode——palindrome-partitioning

    Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...

  9. CF703D Mishka and Interesting sum

    题意:给定一个1e6长度的值域1e9的数组.每次给定询问,询问区间内出现偶数次的数的异或和. 题解:首先很显然,每一次询问的答案,等于这个区间所有不同元素异或和异或上区间异或和.(因为出现偶数次的对区 ...

  10. hive学习7(条件函数case)

    case函数 语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END 说明:如果a为TRUE,则返回b:如果c为TRUE,则返回d:否则返回e 实例 ...