Gunner II(二分,map,数字转化)
Gunner II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1740 Accepted Submission(s): 635
Jack will shot many times, he wants to know which bird will fall during each shot.
In the second line, there are n numbers h[1],h[2],h[3],…,h[n] which describes the height of the trees.
In the third line, there are m numbers q[1],q[2],q[3],…,q[m] which describes the height of the Jack’s shots.
Please process to the end of file.
[Technical Specification]
All input items are integers.
1<=n,m<=100000(10^5)
1<=h[i],q[i]<=1000000000(10^9)
The id starts from 1.
1 2 3 4 1
1 3 1 4 2
3
5
4
2
Huge input, fast IO is recommended.
题解:这题就做的曲折了,刚开始一看不就是个hash表么,倒着记录下就好了,然而在tel和mel中,我放弃了。。。
于是就想着二分了,对于已经用过的怎么办,再开个数组记录已经用过的,可是数组太大了啊,于是我用map来记录,可是是数字啊,木事木事,转化成string不就好了。。。想用stl里的二分类,但是是结构体啊,那就自己写。。。
写完了,感觉很有可能wa,因为二分感觉写的有点挫,交了下竟然AC了。。。我只想笑。。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+;
struct Node{
int v,num;
bool operator < (const Node &b) const{
if(v!=b.v){
return v<b.v;
}
else return num<b.num;
}
};
Node dt[MAXN]; int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
map<string,int>mp;
for(int i=;i<=n;i++)SI(dt[i].v),dt[i].num=i;
sort(dt+,dt+n+);
int x;
sort(dt+,dt+n+);
for(int i=;i<m;i++){
SI(x);
char s[];
itoa(x,s,);
int l=,r=n+,mid;
while(l<=r){
mid=(l+r)>>;
if(x<=dt[mid].v)r=mid-;
else l=mid+;
}
if(dt[r++mp[s]].v==x){
printf("%d\n",dt[r++mp[s]].num);
mp[s]++;
}
else puts("-1");
}
}
return ;
}
我的hash表,TLE了:(贡献了9次TLE,MLE)贴下吧
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
int tp;
int hsh[MAXN],head[MAXN],pos[MAXN],nxt[MAXN];
int ans[];
int q[];
void add(int p,int x){
int i=x%MAXN;
hsh[tp]=x;
pos[tp]=p;
nxt[tp]=head[i];
head[i]=tp++;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int x;
mem(head,-);tp=;
mem(nxt,);mem(hsh,);
for(int i=;i<=n;i++){
scanf("%d",&x);add(i,x);
}
for(int i=;i<=m;i++)scanf("%d",&q[i]);
for(int i=m;i>=;i--){
x=q[i];
int flot=;
for(int j=head[x%MAXN];j!=-;j=nxt[j]){
if(x==hsh[j]){
ans[i]=pos[j];
flot=;
hsh[j]=;break;
}
}
if(!flot)ans[i]=-;
}
for(int i=;i<=m;i++)printf("%d\n",ans[i]);
}
return ;
}
Gunner II(二分,map,数字转化)的更多相关文章
- hdoj--5233--Gunner II(map+queue&&二分)
Gunner II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 二分查找 BestCoder Round #42 1002 Gunner II
题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...
- LeetCode第十二题-将数字转化为罗马数字
Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...
- Python代码阅读(第2篇):数字转化成列表
本篇阅读的代码实现了将输入的数字转化成一个列表,输入数字中的每一位按照从左到右的顺序成为列表中的一项. 本篇阅读的代码片段来自于30-seconds-of-python. digitize def d ...
- POJ 1840 Eqs 二分+map/hash
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 数字转化为汉字,如5->五
//数字转化为汉字 如5-->五-(NSString*)translation:(NSString *)arebic{ NSString *str = arebic; NSArray ...
- 计算机二级-C语言-字符数字转化为整型数字。形参与实参类型相一致。double类型的使用。
//函数fun功能:将a和b所指的两个字符串分别转化成面值相同的整数,并进行相加作为函数值返回,规定只含有9个以下数字字符. //重难点:字符数字转化为整型数字. #include <stdio ...
- 剑指 Offer 56 - II. 数组中数字出现的次数 II + 位运算
剑指 Offer 56 - II. 数组中数字出现的次数 II Offer_56_2 题目详情 解题思路 java代码 package com.walegarrett.offer; /** * @Au ...
随机推荐
- U盘开发之安全U盘
普通型安全U盘,虚拟KEY和U盘两个设备,由主机软件分别对KEY和U盘进行操作,U盘与上位机采用usb mass storage接口,KEY采用HID接口,两者均无需驱动.也有虚拟成光盘和U盘两个设备 ...
- Linux下静态编译Qt程序
一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...
- linux下so动态库一些不为人知的秘密(上)
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识. 基本上每一个linux 程序都至少会有一个动态库,查看某个程序使 ...
- wireshark的 rdp dissector
开源 wireshark的 rdp dissector https://github.com/boytm/wireshark_rdp. 1)support TCP reassemble 2)Fastp ...
- 关键路径(CriticalPath)算法
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXVEX 30 //最大顶 ...
- 国内常用ntp服务器ip地址
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.edu.cn 北京邮电大学s1b.time.edu.cn 清华大学s1c.time ...
- 解决ZBarSDK 中文出现乱码
使用ZBarSDK进行二维码解释时出现乱码是因为ZBarSDK在解释时使用的字符集不对,需在ZBarSDK的源码中更正. 1.可以到http://sourceforge.net/p/zbar/_lis ...
- 网易云课堂_C++程序设计入门(上)_第2单元:丹青画松石– EGE图形库
第2节:一个简单的EGE程序 #ifndef _GRAPHICS_H_ #define _GRAPHICS_H_ #ifndef __cplusplus #error You must use C++ ...
- python基础教程_学习笔记14:标准库:一些最爱——re
标准库:一些最爱 re re模块包括对正則表達式的支持,由于以前系统学习过正則表達式,所以基础内容略过,直接看python对于正則表達式的支持. 正則表達式的学习,见<Mastering Reg ...
- 【27前端】base标签带有href属性会让chrome里的svg元素url失效
一个chrome的问题,但具体原因不明. 触发条件:chrome浏览器base标签里href属性有值的时候 触发问题:svg里面的元素如果有用url的滤镜和模糊,则会失效,在firefox里和IE10 ...