HDU 5233 Gunner II 离散化
题目链接:
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 离散化的更多相关文章
- hdu 5233 Gunner II
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 简单题,stl水之... #include<algorithm> #include& ...
- 二分查找 BestCoder Round #42 1002 Gunner II
题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...
- Gunner II(二分,map,数字转化)
Gunner II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- HDU 2236 无题II(二分图匹配+二分)
HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...
- hdu 5233 离散化
10^9的大数组显然开不了.所以也算比较裸的离散化了... 令pos[i].pp[j]表示从左到右第j个高度为i的树的位置 (pp是个vector,范围0..now-1) pos[i].num表示有几 ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
随机推荐
- 用file标签实现多图文件上传预览
效果图: js 代码: <script> //下面用于多图片上传预览功能 function setImagePreviews(avalue) { var docObj = document ...
- 解决ios下audio不能正常播放的问题
解决ios下audio不能正常播放的问题 ios系统下会自动屏蔽audio标签的自动播放,需要使用一个事件来驱动音频播放 this.$refs.startaudio.addEventListener( ...
- 07.centos7构建 IntelliJ IDEA(简称IDEA)开发环境
一.安装IDEA 进入官网下载linux版的社区便,IDEA分为社区版和旗舰版,社区版免费,并且基本满足spark开发需求. 解压安装 目录为/opt/idea 注意:centos命令行界面下是无法安 ...
- 树莓派 ubuntu16.04 安装SSH 配置SSH 开机自启SSH
入手个树莓派3B 装了 ubuntu 16.04 需要用到SSH 记录下 0.先获得树莓派IP 树莓派 使用网线连接路由器和树莓派 在路由器设置页面(一般是192.168.1.1具体看路由器的型号和设 ...
- ACM1020:Encoding
Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...
- Java NIO (1)
Java NIO (1) 看了下java核心技术这本书 关于nio的部分介绍比较少,而且如果自己写服务器的话nio用的还是比较多,整理一下nio的资料 java中nio主要是三个组件 Buffers ...
- ASP.NET Core 资源打包与压缩
ASP.NET Core 资源打包与压缩 在ASP.NET 中可以使用打包与压缩来提高Web应用程序页面加载的性能. 打包是将多个文件(CSS,JS等资源文件)合并或打包到单个文件.文件合并可减少We ...
- 20155313 2016-2017-2 《Java程序设计》第二周学习总结
20155313 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 1.1 基本类型 整数:可细分为short整数(占2字节).int整数(占4字节)与long ...
- P3940 分组
P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...
- iOS 的音频播放
一.Audio Toolbox 1.使用代码 #import <AudioToolbox/AudioToolbox.h> AudioServicesPlaySystemSound(1106 ...