题目链接:

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5233

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=585&pid=1002

题解:

离散化之后,存在一张表里面(相同的值的id号用链表串起来,倒着存),每次查询完就把表首的删了,继续查。

之前离散的时候没有把查询的一起加进去,就一直t,估计是查询的时候很多是没有结果的,就会比较耗时间。改成一起哈希之后就过了。也没有做读写优化。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL; const int maxn=2e5+; struct Edge{
int v,ne;
Edge(int v,int ne):v(v),ne(ne){};
Edge(){};
}egs[maxn]; int n,m; int head[maxn],tot;
int ha[maxn];
int arr[maxn],q[maxn];
//将元素加入邻接表
void addEdge(int x,int id){
egs[tot]=Edge(id,head[x]);
head[x]=tot++;
}
//查询并删除
int queEdge(int x){
int p=head[x];
if(p==-) return -;
int ret=egs[p].v;
head[x]=egs[p].ne;
return ret;
}
/*
離散化去重
void get_ID(){
sort(ha,ha+n+m); ha[n+m]=ha[n+m-1]-1;
int cur=0;
for(int i=0;i<n+m;i++){
if(ha[i]!=ha[i+1]){
ha[cur++]=ha[i];
}
}
ha[n+m]=cur;
*/ //二分
int find(int x){
int lef=,rig=ha[n+m];
int ret=-;
while(lef<rig){
int mid=lef+(rig-lef)/;
if(ha[mid]<x){
lef=mid+;
}else if(ha[mid]>x){
rig=mid;
}else{
ret=mid;
break;
}
}
return ret;
} void init(){
memset(head,-,sizeof(head));
tot=;
} int main(){
while(scanf("%d%d",&n,&m)==&&n){
init();
//将输入的数和查询的数一起做离散化
for(int i=;i<n;i++){
scanf("%d",arr+i);
ha[i]=arr[i];
}
for(int i=;i<m;i++){
scanf("%d",q+i);
ha[n+i]=q[i];
}
//离散化
sort(ha,ha+n+m);
ha[n+m]=unique(ha,ha+n+m)-ha; //倒着插入,这样只要每次删除表首的数就可以了
for(int i=n-;i>=;i--){
int x=find(arr[i]);
addEdge(x,i);
}
for(int i=;i<m;i++){
int x=find(q[i]);
printf("%d\n",queEdge(x)+);
}
}
return ;
} /*
5 5
1 2 3 4 1
1 3 1 4 9
*/

HDU 5233 Gunner II 离散化的更多相关文章

  1. hdu 5233 Gunner II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 简单题,stl水之... #include<algorithm> #include& ...

  2. 二分查找 BestCoder Round #42 1002 Gunner II

    题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...

  3. Gunner II(二分,map,数字转化)

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. HDU 3567 Eight II(八数码 II)

    HDU 3567 Eight II(八数码 II) /65536 K (Java/Others)   Problem Description - 题目描述 Eight-puzzle, which is ...

  7. HDU 2236 无题II(二分图匹配+二分)

    HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...

  8. hdu 5233 离散化

    10^9的大数组显然开不了.所以也算比较裸的离散化了... 令pos[i].pp[j]表示从左到右第j个高度为i的树的位置 (pp是个vector,范围0..now-1) pos[i].num表示有几 ...

  9. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

随机推荐

  1. php文件相关操作

    //遍历目录及文件 function myBianli($dirname){ //1.打开 $dir = opendir($dirname); //2.读取 while($filename = rea ...

  2. Delphi XE7的蓝牙 Bluetooth

    Delphi XE7已经内建了蓝牙功能,提供了System.Bluetooth.pas单元 顾名思义,System表示XE7的蓝牙功能可以在Windows,Android,IOS系统内使用 Syste ...

  3. linux-2.6.22.6内核启动分析之head.S引导段代码

    学习目标: 了解arch/arm/kernel/head.S作为内核启动的第一个文件所实现的功能! 前面通过对内核Makefile的分析,可以知道arch/arm/kernel/head.S是内核启动 ...

  4. 先进先出算法(FIFO)——页面置换

    原创 最近操作系统实习,写了先进先出算法(FIFO)的代码来实现页面置换. 题目阐述如下: 设计四:页面置换 设计目的: 加深对请求页式存储管理实现原理的理解,掌握页面置换算法. 设计内容: 设计一个 ...

  5. SQL学习笔记:基础教程

    SQL语法 在表中选择列 select 列名 from 表名 选择所有列 select * from 表名 返回唯一值 select distinct 列名 from 表名 where select ...

  6. 记账本app(1)

    今天开始做做这个app了,加油,目前在看视频,明天正式入手

  7. 20155224聂小益 2016-2017-2 《Java程序设计》第1周学习总结

    20155224聂小益 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 第一章内容不是很多,主要介绍了Java发展历程与Java的使用平台. JVM: ...

  8. day1 ORM

    ORM对象关系映射 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 <------->类实例对象 class Customer( ...

  9. 04-JVM内存模型:直接内存

    1.1.什么是直接内存(Derect Memory) 在内存模型最开始的章节中,我们画出了JVM的内存模型,里面并不包含直接内存,也就是说这块内存区域并不是JVM运行时数据区的一部分,但它却会被频繁的 ...

  10. lua 中的 loadfile、dofile和require的调用

    文件 hello.lua print("hello") function say() print("hello world") end 1. 介绍: dofil ...