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

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+100;
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=1;i<=n;i++)SI(dt[i].v),dt[i].num=i;
        sort(dt+1,dt+n+1);
        int x;
        sort(dt+1,dt+n+1);
        for(int i=0;i<m;i++){
            SI(x);
            char s[10];
            itoa(x,s,10);
            int l=1,r=n+1,mid;
            while(l<=r){
                mid=(l+r)>>1;
                if(x<=dt[mid].v)r=mid-1;
                else l=mid+1;
            }
            if(dt[r+1+mp[s]].v==x){
                printf("%d\n",dt[r+1+mp[s]].num);
                mp[s]++;
            }
            else puts("-1");
        }
    }
    return 0;
}

我的hash表,TLE了:(贡献了9次TLE,MLE)贴下吧

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 const int INF=0x3f3f3f3f;
 8 #define SI(x) scanf("%d",&x)
 9 #define PI(x) printf("%d",x)
10 #define P_ printf(" ")
11 #define mem(x,y) memset(x,y,sizeof(x))
12 const int MAXN=5000010;
13 int tp;
14 int hsh[MAXN],head[MAXN],pos[MAXN],nxt[MAXN];
15 int ans[100010];
16 int q[100010];
17 void add(int p,int x){
18     int i=x%MAXN;
19     hsh[tp]=x;
20     pos[tp]=p;
21     nxt[tp]=head[i];
22     head[i]=tp++;
23 }
24 int main(){
25     int n,m;
26     while(~scanf("%d%d",&n,&m)){
27         int x;
28         mem(head,-1);tp=0;
29         mem(nxt,0);mem(hsh,0);
30         for(int i=1;i<=n;i++){
31             scanf("%d",&x);add(i,x);
32         }
33         for(int i=1;i<=m;i++)scanf("%d",&q[i]);
34         for(int i=m;i>=1;i--){
35                 x=q[i];
36             int flot=0;
37             for(int j=head[x%MAXN];j!=-1;j=nxt[j]){
38                 if(x==hsh[j]){
39                     ans[i]=pos[j];
40                     flot=1;
41                     hsh[j]=0;break;
42                 }
43             }
44             if(!flot)ans[i]=-1;
45         }
46         for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
47     }
48     return 0;
49 }
时间: 2024-08-01 06:31:10

Gunner II(二分,map,数字转化)的相关文章

HDU 5233 Gunner II (二分)

问题描述 很久很久以前,有一个叫Jack的枪手.他非常喜欢打猎.一天,他去了一个小树林.那儿有n只鸟,还有n棵树.第i只鸟站在第i棵树的顶端.这些树从左到右排成一条直线.每一棵树都有它的高度.Jack站在最左边那棵树的左边.当Jack在高度为H的地方向右发射一棵子弹时,站在高度为H的树上且离Jack最近的鸟儿就会落下来. Jack会射击多次,他想知道每次射击哪只鸟儿会落下来. 输入描述 多组测试数据(大概5组),每一组的第一行给出n,m,n表示有n棵树和n只鸟,m表示Jack会射击m次. 在第二

hdu 5233 Gunner II 离散化

Gunner II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5233 Description 很久很久以前,有一个叫Jack的枪手.他非常喜欢打猎.一天,他去了一个小树林.那儿有n只鸟,还有n棵树.第i只鸟站在第i棵树的顶端.这些树从左到右排成一条直线.每一棵树都有它的高度.Jack站在最左边那棵树的左边.当Jack在高度为H的地方向右发射一棵子弹时,站在高度为

hdu 5233 Gunner II (bc #42 B)

Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1433    Accepted Submission(s): 540 Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver

HDU5233——离散化——Gunner II

http://acm.hdu.edu.cn/showproblem.php?pid=5233 /* 用map和set进行离散化,离散化就是对于大数据进行重新编号 */ /************************************************ * Author :Powatr * Created Time :2015-8-12 18:27:42 * File Name :HDU5233 Gunner II.cpp *****************************

将数字转化为字符串的快捷方式

1.将数字转化为字符串的快捷方式 2.请彻底抛弃float类型,使用CGFloat或double,float不够精确. BOOL b1 = YES; BOOL b2 = NO; NSInteger t1 = 100000; int t2 = 200000; float t3 = 300000.02; CGFloat t33 = 300000.02; double t4 = 400000.0000003; NSNumber *num1 = @(123); NSNumber *num2 = @(12

POJ 1840 Eqs 二分+map/hash

Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,

STL HDOJ 5233 Gunner II

题目传送门 1 /* 2 题意:查询x的id,每次前排的树倒下 3 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e5 + 10; 11 const int INF = 0x3f3f3f3

洛谷P1182 数列分段Section II 二分答案

洛谷P1182 数列分段Section II 二分答案 题意:将 n 个 数 分为 m段 求一种方案,使这m段中最大的和 最小 额..可能有点拗口,其实就是说每一种方案,都有对应的 每段和的最大值,要求一种方案,使最大值最小 题解 :二分答案 mid为分成的最大值, 然后O(n) 判断 答案 是否可行 贪心的做下去,如果再加上就要超过了,那就新开一段 最后判断开的段数是否小于 m 1.注意要判断 如果当前这个值大于 mid,一个值就已经大于 mid了,那就直接退出了,否则 ,这个值也只会单独算为

#001#人人笔试题 输入数字转化为汉字

Java实现 1 package test001; 2 import java.util.Scanner; 3 4 //输入530014 输出五十三万零一十四 5 //int 型数据最大值为 21 4748 3647 二十一亿 四千七百四十八万 三千六百四十七 6 //int 型数据最小值为-2147483647 7 public class file1 { 8 static String[] numArray={"零","一","二",&quo