Gunner II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1740    Accepted Submission(s): 635

Problem Description
Long long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i-th bird stands on the top of the i-th tree. The trees stand in straight line from left to the right. Every tree has its height. Jack stands on the left side of the left most tree. When Jack shots a bullet in height H to the right, the nearest bird which stands in the tree with height H will falls.
Jack will shot many times, he wants to know which bird will fall during each shot.
 
Input
There are multiple test cases (about 5), every case gives n, m in the first line, n indicates there are n trees and n birds, m means Jack will shot m times. 
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)
 
Output
For each q[i], output an integer in a single line indicates the id of bird Jack shots down. If Jack can’t shot any bird, just output -1.
The id starts from 1.
 
Sample Input
5 5
1 2 3 4 1
1 3 1 4 2
 
Sample Output
1
3
5
4
2

Hint

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,数字转化)的更多相关文章

  1. hdoj--5233--Gunner II(map+queue&&二分)

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

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

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

  3. LeetCode第十二题-将数字转化为罗马数字

    Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...

  4. Python代码阅读(第2篇):数字转化成列表

    本篇阅读的代码实现了将输入的数字转化成一个列表,输入数字中的每一位按照从左到右的顺序成为列表中的一项. 本篇阅读的代码片段来自于30-seconds-of-python. digitize def d ...

  5. POJ 1840 Eqs 二分+map/hash

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  6. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. 数字转化为汉字,如5->五

    //数字转化为汉字 如5-->五-(NSString*)translation:(NSString *)arebic{   NSString *str = arebic;    NSArray ...

  8. 计算机二级-C语言-字符数字转化为整型数字。形参与实参类型相一致。double类型的使用。

    //函数fun功能:将a和b所指的两个字符串分别转化成面值相同的整数,并进行相加作为函数值返回,规定只含有9个以下数字字符. //重难点:字符数字转化为整型数字. #include <stdio ...

  9. 剑指 Offer 56 - II. 数组中数字出现的次数 II + 位运算

    剑指 Offer 56 - II. 数组中数字出现的次数 II Offer_56_2 题目详情 解题思路 java代码 package com.walegarrett.offer; /** * @Au ...

随机推荐

  1. 【D3.V3.js系列教程】--(十四)有路径的文字

    [D3.V3.js系列教程]--(十四)有路径的文字 1. 在 svg 中插入一個 text // 在 body 中插入一個 svg var svg = d3.select('body').appen ...

  2. 【JSP实例】指定用户计数器

    不同的用户访问次数是不一样的,因此对于每一个用户的访问次数都要进行统计,以适应需要. 用户登陆的Login.html的源文件: <html> <head> <title& ...

  3. 一道google面试题(dp)

    输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...

  4. U盘量产的作用

    优盘量产:字面意思就是,批量生产优盘.是指批量对U盘主控芯片改写数据,如,写生产厂商信息.格式化等.而用来对U盘完成该操作的软件程序,顾名思义就是U盘量产工具.   U盘量产的作用:   电脑正确识别 ...

  5. mysql 初始化

    一.centos7下mysql 安装配置 yum -y install mariadb* systemctl start mariadb.service systemctl enable mariad ...

  6. SQL Server Reporting Services (SQLEXPRESS) 服务占用80端口

    win7, 好多时候,看到system进程占用了80端口,这个是系统进程,不能直接结束.我们不知道这个进程的哪个服务占用了80端口,这里记录其中一个服务"SQL Server Reporti ...

  7. ADO.NET DataSet、DataTable、DataRow、DataView的学习

    对于一个datatable中的数据进行嵌套查询判断某几列数据是否相同从而确定这条数据是否一样,并确定他重复的次数COUNT1字段. 例如: DataTable dt = new DataTable() ...

  8. 普通用户登录Oracle DB Control

    使用 sys 或者 system 用户登录 Oracle DB Control 是没有问题的. 但是,如果是普通的用户需要登录Oracle DB Control,建表或者视图之类的, 则需要授权 SE ...

  9. 实力为王 八年DBA经验谈

    首先说说我的经历吧,工作8年,一直是搞运维这一块.做数据库的工作大概6年多,ORACLE DBA大概做了有4年多,目前只能说是比入门级稍高点的水平.我是最近为了换个更好的工作才在今年通过的OCP 11 ...

  10. OC——NSString的常用方法

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...