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
这道题因为删除方法不对,一直T,终于改用vis后,900多ms水过去了。。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
struct node{
int id,num;
}a[100006];
int n,vis[100006]; bool cmp(node a,node b){
if(a.num==b.num)return a.id>b.id;
return a.num<b.num;
}
int q[100006]; int find(int x,int l,int r)
{
int mid,j;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid].num==x)break;
if(a[mid].num>x)r=mid-1;
else l=mid+1;
}
j=mid;
if(vis[j]==1){
j--;
while(1){
if(vis[j]==0)break;
j--;
}
return j;
}
else{
j++;
while(1){
if(j>n || vis[j]==1 || a[j].num>x){
j--;break;
}
j++;
}
return j;
}
} int main()
{
int m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
map<int,int>hash;
hash.clear();
for(i=1;i<=n;i++){
vis[i]=0;
scanf("%d",&a[i].num);
a[i].id=i;
hash[a[i].num]++;
}
sort(a+1,a+1+n,cmp);
for(i=1;i<=m;i++){
scanf("%d",&q[i]);
if(hash[q[i]]==0){
printf("-1\n");continue;
}
else{
hash[q[i]]--;
j=find(q[i],1,n);
vis[j]=1;
printf("%d\n",a[j].id);
}
}
}
return 0;
}

hdu5233 Gunner II的更多相关文章

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

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

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

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

  3. hdu 5233 Gunner II

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

  4. HDU 5233 Gunner II 离散化

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

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

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

  6. HDU5233

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

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  9. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

随机推荐

  1. mysql中的基本注入函数

    1. 常见数据库注入函数: MYSQL: and length((user))>10 ACCESS: and (select count() from MSysAccessObject)> ...

  2. 【Linux】查看系统僵尸进程

    ps -ef|grep -v grep|grep defunct 如果这个有显示内容的话,可以手动将进程kill掉即可 ---------------------------------------- ...

  3. 【ORA】ORA-00257 archiver error. 错误的处理方法

    今天连接数据库,结果报错,ora-00257查看 [oracle@exam oracle]$ oerr ora 00257 00257, 00000, "archiver error. Co ...

  4. 记录Js动态加载页面.append、html、appendChild、repend添加元素节点不生效以及解决办法

    今天再优化blog页面的时候添加了个关注按钮和图片,但是页面上这个按钮和图片时有时无,本来是搞后端的,被这个前端的小问题搞得抓耳挠腮的! 网上各种查询解决方案,把我解决问题的艰辛历程分享出来,希望大家 ...

  5. 使用Gulp里面的浏览器同步插件browser-sync的注意事项

    使用Gulp里面的浏览器同步插件browser-sync的注意事项 第一步:打开你的开发者工具, 编写前端代码!图如下! 第二步:打开你当前工作目录的命令行窗口 第三步:输入浏览器同步执行的代码! b ...

  6. jQuery 页面滚动 吸顶 和 吸底

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Java SPI机制详解

    Java SPI机制详解 1.什么是SPI? SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制.SPI是一种动态替换发现的机制, 比如有个 ...

  8. 一个关于时区的bug

    起因: 在 Apollo 中配置了某活动的开始时间是 2020-05-15, 代码中的逻辑判断如下: const nowTime = new Date().getTime(); const start ...

  9. WireShark 之 text2pcap

    前言 本来想用 010Editer 的,看到破解教程头都大了,那么就用 WireShark 的 Text2pcap 吧! 正文 打开CMD控制台窗口,转到WireShark安装目录 ,此处可以shif ...

  10. C++ Primer Plus读书笔记(八)函数探幽

    1.内联函数 inline int square(x) {return x*x} 2.引用变量 int& 中的& 不是地址运算符,就想定义指针时的char* 一样,int&指的 ...