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 MySQL 连接数据库,进行增、删、改、查、操作

    <table width="100%" border="1" cellpadding="0" cellspacing="0& ...

  2. UVM基础之--------uvm_root

    uvm_root 是uvm的顶层实例扮演了一个top-level and phase controller 的作用,对于component来说.该类不需要用户实例化,他是一个自动实例化了的类,用户直接 ...

  3. (转)分布式文件存储FastDFS(二)FastDFS安装

    http://blog.csdn.net/xingjiarong/article/details/50559761 在前面的一篇中,我们分析了FastDFS的架构,知道了FastDFS是由客户端,跟踪 ...

  4. 数据库操作(二)SOQL

    1.SOQL SOQL是对象查询语言.它可以在单个sObject中在给定标准上搜索记录. 2.SELECT语句 [格式]SELECT 列名称 FROM 表名称 [示例] 3.SELECT...WHER ...

  5. How To:使用dmidecode获取机器序列号(Serial number)

    使用dmidecode可以获取关于机器的诸多信息,比如机器的序列号   [root@dc1db01 ~]# dmidecode -s system-serial-number   processor的 ...

  6. sdp概览

    sdp会话描述符有多行用如下格式组成的文本: <type>=<value>等号旁边不允许留白. sdp会话描述符有一个session-level的段,后面会接零个或者多个med ...

  7. 分布式服务框架Dubbo入门案例和项目源码

    本项目源代码:http://download.csdn.net/detail/fansunion/9498406 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案, 是 ...

  8. Django——4 模板标签 模板的继承与引用

    Django 模板标签 常用标签 模板的继承与引用 模板标签 标签在渲染的过程中提供任意的逻辑 标签语法: 由%}和 {% 来定义的,例如:{%tag%} {%endtag%} 这个定义是刻意模糊的. ...

  9. 洛谷 P1469 找筷子

    题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘手的问题,筷子!CX ...

  10. hdu_1019_Least Common Multiple_201310290920

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...