hdu5233 Gunner II

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;
}

时间: 2024-10-15 21:07:01

hdu5233 Gunner II的相关文章

HDU5233 Gunner II 离散化的各种方法

题目链接: HDU5233 题意: n棵树依次排好,每棵树都有一个高度,树的顶端有一只鸟. 猎人会打M枪,每一枪都能从高度为X的树上打下一只鸟,问每一枪打下的鸟是从  编号多少的树 上掉下来的 题解思路: 因为树的高度能达到(10^9)  而树的数量最多10^5  所以离散化   将所有高度为X的树离散化为 高度为第X高的树 有多种方法. 1  stl去重+set版: #include<iostream> #include<cstdio> #include<cstring&g

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 *****************************

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 ve

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的地方向右发射一棵子弹时,站在高度为

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

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

hdu 5233 Gunner II 【set+map】

题意不说了,之所以贴代码是因为想说容器是个很好的东西 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<set> #include<map> using namespace std; map<int,set<int> >mp; set<int>::iterator it; int

【map】【HDOJ】5233 Gunner II

http://acm.hdu.edu.cn/showproblem.php?pid=5233 外面有很多树,每棵树顶上有一只鸟,一个数组按从近到远的顺序列出这些树的高度(也就是鸟的高度) 猎人开始从不同高度打枪,子弹不能穿过鸟,也就是在同一高度上只有最近的鸟会被打下来. 给出一个数组表示猎人每次打枪的高度,要求出每次打落的鸟所在的树的编号(从1开始编号),如果这个高度上没有鸟输出-1 放一个MAP表示每个高度上从近到远的鸟的位置编号, 猎人每次射击就将该高度的定位数向后移,直到定位数大于鸟的数量

hdu 5233 Gunner II

开始试了很多方法,不过由于删除的效率导致tle,总之无论什么方法,能ac就是好方法 #include<iostream> #include<vector> #include<map> using namespace std; vector<int>mapp[100000+5]; map<int,int>root; int he[100000+5]; int main() { int n,m; cin.sync_with_stdio(false);