hdu5199(map 离散化)

Gunner

Long long ago, there is a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are nn birds and nn trees. The ithi−th bird stands on the top of the ithi−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 bird which stands in the tree with height HH will falls.
Jack will shot many times, he wants to know how many birds fall during each shot.

a bullet can hit many birds, as long as they stand on the top of the tree with height of HH .InputThere are multiple test cases (about 5), every case gives n,mn,m in the first line, nn indicates there are nn trees and nn birds, mm means Jack will shot mm times.

In the second line, there are nn numbers h[1],h[2],h[3],,h[n]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]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]

1n,m1000000(106)1≤n,m≤1000000(106)

1h[i],q[i]1000000000(109)1≤h[i],q[i]≤1000000000(109)

All inputs are integers.
OutputFor each q[i]q[i] , output an integer in a single line indicates the number of birds Jack shot down.Sample Input

4 3
1 2 3 4
1 1 4

 

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include<string.h>
#include<map>
using namespace std;
int n,m,t;
string a,b;
int main()
{
   map<int,int> mp;
   int n,m,tot,ans;
   while(~scanf("%d%d",&n,&m)){
   for(int i=0;i<n;i++)
   {
     scanf("%d",&tot);
     mp[tot]++;//鸟的数量增加(树高的数量就是鸟的数量)
   }
   for(int i=0;i<m;i++)
   {
       scanf("%d",&ans);
       if(mp.find(ans)!=mp.end())//查找键
       {
           printf("%d\n",mp[ans]);
           mp[ans]=0;
       }
       else
        printf("0\n");
   }
   }
    return 0;
}

注意点:

1.一开始超时,换成printf就好了。以后都用printf。

2.if(mp.find(ans)!=mp.end())//查找键

posted @ 2019-05-17 22:42  zuiaimiusi  阅读(251)  评论(0编辑  收藏  举报