【map】【HDOJ】5233 Gunner II

http://acm.hdu.edu.cn/showproblem.php?pid=5233

 

外面有很多树,每棵树顶上有一只鸟,一个数组按从近到远的顺序列出这些树的高度(也就是鸟的高度)

猎人开始从不同高度打枪,子弹不能穿过鸟,也就是在同一高度上只有最近的鸟会被打下来。

给出一个数组表示猎人每次打枪的高度,要求出每次打落的鸟所在的树的编号(从1开始编号),如果这个高度上没有鸟输出-1

 

放一个MAP表示每个高度上从近到远的鸟的位置编号,

猎人每次射击就将该高度的定位数向后移,直到定位数大于鸟的数量,就输出-1

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef vector<int> vec;
 5 map<int, vec> b;
 6 map<int, int> pos;
 7 int n, m, shot;
 8 
 9 int main(){
10     while(~scanf("%d%d", &n, &m)){
11         b.clear();
12         pos.clear();
13         for(int i = 1; i <= n; i++){
14             int h;
15             scanf("%d", &h);
16             b[h].push_back(i);
17             pos[h] = 0;
18         }
19         map<int, vec>::iterator it;
20         while(m--){
21             scanf("%d", &shot);
22             it = b.find(shot);
23             if(it != b.end()){
24                 int p = it->first;
25                 int l = it->second.size();
26                 if(pos[p] < l){
27                     printf("%d\n", it->second[pos[p]]);
28                     pos[p]++;
29                 }
30                 else printf("-1\n");
31             }
32             else{
33                 printf("-1\n");
34             }
35         }
36     }
37 
38     return 0;
39 }

 

posted @ 2016-04-02 10:35  快扶哀家去刷题  阅读(146)  评论(0编辑  收藏  举报