hdu5199Gunner(二分查找)

来源:互联网 发布:office文档修复软件 编辑:程序博客网 时间:2024/05/21 06:23

解题思路:

本题h的取值范围太大,所以无法直接开数组统计

1、 map统计

2、排序+二分

3、hash

4、优先队列

#include<cstdio>#include <cstring>#include <algorithm>#include <queue>#define MAXN 1000010using namespace std;struct node{    int id;    int h;}q[MAXN];int ans[MAXN];priority_queue<int,vector<int>,greater<int> > pq;bool cmp(node a,node b){    if(a.h==b.h) return a.id<b.id;    return a.h<b.h;}int n,m;int main(){    while(scanf("%d%d",&n,&m)==2){        while(!pq.empty()) pq.pop();        int tm;        for(int i=0;i<n;++i)        {            scanf("%d",&tm);            pq.push(tm);        }        for(int i=0;i<m;++i){            scanf("%d",&q[i].h);            q[i].id=i;        }        sort(q,q+m,cmp);        memset(ans,0,sizeof(ans));        for(int i=0;i<m;++i){            int tans=0;            while(!pq.empty()&&pq.top()<q[i].h) pq.pop();            while(!pq.empty()&&i<m&&pq.top()==q[i].h){                tans++;                pq.pop();            }            ans[q[i].id]=tans;            if(pq.empty())                break;        }        for(int i=0;i<m;++i)            printf("%d\n",ans[i]);    }    return 0;}


收获:

map  数组 hash 

共同点:一一对应的关系

不同点

1、map的key适用的数据类型比较广泛,而数组的key只能是int类型

2、map插入、查询相对数组来时间消耗大,map O(logN)  数组O(1)

3、数组相对于map来说消耗的空间大

hash相对于数组来说降低了空间消耗,但同时时间消耗又没有太大的影响

二分的思想。。。


0 0