Gunner II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1724    Accepted Submission(s): 631

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.
 
Source
 感觉二分写的应该对的啊,但是一直wa
错误代码:

#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include<iostream>
using namespace std;
struct Node
{
int num,hight;
bool flg;
}a[110000];
bool cmp(Node x,Node y)
{
if(x.hight == y.hight )return x.num<y.num;
return x.hight<y.hight;
}
int b_search(int val,int len)
{
int l=0,r=len-1,mid;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid].hight<val)l=mid+1;
else if(a[mid].hight == val && a[mid].flg == false)l=mid+1;
else r=mid-1;
}
return l;
}
int main()
{
int m,n,i,j,cy,ans,ji;
while(~scanf("%d %d",&n,&m))
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i].hight);
a[i].num=i+1;
a[i].flg=true;
}
sort(a,a+n,cmp);
for(i=0;i<m;i++)
{
scanf("%d",&ji);
ans=b_search(ji,n);
a[ans].flg=false;
if(a[ans].hight==j)
printf("-1\n");
else
{
a[ans].flg=false;
cout<<a[ans].num<<endl;
}
}
}
return 0;
}

map+queue
#include<cstdio>
#include<map>
#include<list>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
map<int,queue<int> >mp;
int tmd;
for(int i=1;i<=n;i++)
{
scanf("%d",&tmd);
mp[tmd].push(i);
}
while(m--)
{
scanf("%d",&tmd);
queue<int> &p=mp[tmd];
if(p.empty()) printf("-1\n");
else
{
printf("%d\n",p.front());
p.pop();
}
}
}
return 0;
}

hdoj--5233--Gunner II(map+queue&&二分)的更多相关文章

  1. HDU 5233 Gunner II 离散化

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5233 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. hdu 5233 Gunner II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 简单题,stl水之... #include<algorithm> #include& ...

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

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

  4. Gunner II(二分,map,数字转化)

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

  5. Java中的容器类(List,Set,Map,Queue)

    Java中的容器类(List,Set,Map,Queue) 一.基本概念 Java容器类类库的用途是“保存对象”,并将其划分为两个不同的概念: 1)Collection.一个独立元素的序列,这些元素都 ...

  6. Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...

  7. hdoj 5199 Gunner map

    Gunner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5199 D ...

  8. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...

  9. hdu5233 Gunner II

    Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver ...

随机推荐

  1. PHP基础知识测试题及解析

      本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A ) A:Windows系统 B:Apache服务器 C:MyS ...

  2. html5——动画案例(太阳系)

    太阳系主要利用定位,伪元素 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  3. java web项目和java项目的区别(看清IDE本质)

    想必大家在使用MyEclipse时对这两个概念不去深究.只知道是Java EE类的基本都是Web项目,而Java应用程序就是Java项目.而且很多人都愿意使用MyEclipse作为开发工具,且不说大家 ...

  4. mysql命令行导出数据

    1. 包含表头 mysql -h${1} -P${2} -u${3} -p${4} -Dpom_${5} --default-character-set=utf8 -B -e > result. ...

  5. 扩增子图表解读1箱线图:Alpha多样性

    箱线图 箱形图(Box-plot)又称为盒须图.盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图.因形状如箱子而得名.在宏基因组领域,常用于展示样品组中各样品Alpha多样性的分布 第一种情 ...

  6. Linux常用命令(简单的常用)

      1. 文件和目录 cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd ...

  7. SqlServer 【基 本 操 作】

    1.Row_Number() select * from (select Row_Number() over (order by FSalary) as 'RowNum' ,* from dbo.T_ ...

  8. linux下root用户和tarena用户vimrc配置

    设置vim自动缩进,行号,tab键宽度 如果是root超级用户,他的主目录是: /root ,打开文件vim的配置文件.vimrc 如果是tarena用户,他的主目录是:/home/tarena 打开 ...

  9. axios的基本概念和安装以及配置方法

    ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 1.从浏览器制作XMLHttpReques ...

  10. HTML解析库BeautifulSoup4

    BeautifulSoup 是一个可以从HTML或XML文件中提取数据的Python库,它的使用方式相对于正则来说更加的简单方便,常常能够节省我们大量的时间. BeautifulSoup也是有官方中文 ...