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代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<map>
  7. #include<string>
  8. using namespace std;
  9. const int INF=0x3f3f3f3f;
  10. #define SI(x) scanf("%d",&x)
  11. #define PI(x) printf("%d",x)
  12. #define P_ printf(" ")
  13. #define mem(x,y) memset(x,y,sizeof(x))
  14. const int MAXN=1e5+;
  15. struct Node{
  16. int v,num;
  17. bool operator < (const Node &b) const{
  18. if(v!=b.v){
  19. return v<b.v;
  20. }
  21. else return num<b.num;
  22. }
  23. };
  24. Node dt[MAXN];
  25.  
  26. int main(){
  27. int n,m;
  28. while(~scanf("%d%d",&n,&m)){
  29. map<string,int>mp;
  30. for(int i=;i<=n;i++)SI(dt[i].v),dt[i].num=i;
  31. sort(dt+,dt+n+);
  32. int x;
  33. sort(dt+,dt+n+);
  34. for(int i=;i<m;i++){
  35. SI(x);
  36. char s[];
  37. itoa(x,s,);
  38. int l=,r=n+,mid;
  39. while(l<=r){
  40. mid=(l+r)>>;
  41. if(x<=dt[mid].v)r=mid-;
  42. else l=mid+;
  43. }
  44. if(dt[r++mp[s]].v==x){
  45. printf("%d\n",dt[r++mp[s]].num);
  46. mp[s]++;
  47. }
  48. else puts("-1");
  49. }
  50. }
  51. return ;
  52. }

我的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=;
  13. int tp;
  14. int hsh[MAXN],head[MAXN],pos[MAXN],nxt[MAXN];
  15. int ans[];
  16. int q[];
  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,-);tp=;
  29. mem(nxt,);mem(hsh,);
  30. for(int i=;i<=n;i++){
  31. scanf("%d",&x);add(i,x);
  32. }
  33. for(int i=;i<=m;i++)scanf("%d",&q[i]);
  34. for(int i=m;i>=;i--){
  35. x=q[i];
  36. int flot=;
  37. for(int j=head[x%MAXN];j!=-;j=nxt[j]){
  38. if(x==hsh[j]){
  39. ans[i]=pos[j];
  40. flot=;
  41. hsh[j]=;break;
  42. }
  43. }
  44. if(!flot)ans[i]=-;
  45. }
  46. for(int i=;i<=m;i++)printf("%d\n",ans[i]);
  47. }
  48. return ;
  49. }

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. delphi 把多个线程的请求阻塞到另一个线程 TElegantThread

    本例是把多个线程访问数据库的请求,全部阻塞到一个线程. 这是实际编程中常见的一种问题. 示例源码下载,所需支持单元均在源码中,且附详细说明. TElegantThread 的父类是 TSimpleTh ...

  2. phonegap开发app中踩过的那些坑

    把遇到的问题列出来,假设有解决方式的,偶也会写下来.假设大家有更好解决方法的.欢迎留言噢 phonegap 2.9无法触发deviceready事件 亲们能够看下控制台有木有报错.假设有提示cordo ...

  3. 依赖注入及AOP简述(三)——依赖注入的原理

    3.     “依赖注入”登场 于是诸多优秀的IT工程师开始想出了更加轻量便利.更加具有可测试性和可维护性的设计模式——IoC模式.IoC,即Inversion of Control的缩写,中文里被称 ...

  4. java 命令行制定logback参数

    -Dlogback.configurationFile=file:///export/Workspace/apache-cassandra/apache-cassandra-main/target/b ...

  5. Chrome浏览器切换到之前打开的标签页会重新加载

    这是谷歌的一种策略.当系统内存不足时,系统会自动从内存中舍弃标签页 在地址栏输入chrome://flags/#automatic-tab-discarding,设置为停用即可.

  6. Jquery二级简单折叠菜单

    写在前面: 1.前端新手 2.欢迎交流 3. 源代码百度云页面示例链接: http://pan.baidu.com/s/1nt0yjd3 链接失效请留言 效果图: 代码部分:jquery部分: < ...

  7. AjaxHelper学习,ajax,microsoft,mvc,asp.net

    index.cshtml @using (Ajax.BeginForm("ContentAjax", new AjaxOptions { UpdateTargetId = &quo ...

  8. SET STATISTICS IO和SET STATISTICS TIME 在SQL Server查询性能优化中的作用

    近段时间以来,一直在探究SQL Server查询性能的问题,当然也漫无目的的查找了很多资料,也从网上的大神们的文章中学到了很多,在这里,向各位大神致敬.正是受大神们无私奉献精神的影响,所以小弟也作为回 ...

  9. Asp.net MVC + EF6.0 经常出现的问题

    1.运行视图时出现问题:未能加载文件或程序集"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c ...

  10. mysql server advanced 5.6基于oracle linux 6.6的安装

    mysql 安装有两种,rpm安装和源码包安装,两种包都可以从www.mysql.com官网下载,这次我测试下rpm安装方式. 1.安装环境以及mysql版本: 1.1vcenter 虚拟机环境 1. ...