hdu 5233 Gunner II 离散化

来源:互联网 发布:java native的用法 编辑:程序博客网 时间:2024/05/22 10:53

题目链接:

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

题意:

有n只鸟,还有n棵树。第i只鸟站在第i棵树的顶端。这些树从左到右排成一条直线。每一棵树都有它的高度。Jack站在最左边那棵树的左边。当Jack在高度为H的地方向右发射一棵子弹时,站在高度为H的树上且离Jack最近的鸟儿就会落下来。 Jack会射击多次,他想知道每次射击哪只鸟儿会落下来。

题解:

离散化

代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst int INF = 0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){    ll x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}//////////////////////////////////////////////////////////////////////////const int maxn = 2e5+10;struct node{    int x,id;}h[maxn];vector<int> k,dp[maxn];map<int,int> H;int q[maxn],cnt[maxn];void init(){    MS(h); MS(q); MS(cnt);    H.clear(); k.clear();    for(int i=0; i<maxn; i++)        dp[i].clear();}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        init();        for(int i=0; i<n; i++){            h[i].x = read();            h[i].id = i+1;            k.push_back(h[i].x);        }        for(int i=0; i<m; i++){            q[i] = read();            k.push_back(q[i]);        }        sort(k.begin(),k.end());        k.erase(unique(k.begin(),k.end()),k.end());        for(int i=0; i<(int)k.size(); i++){            H[k[i]] = i;        }        for(int i=0; i<n; i++)            dp[H[h[i].x]].push_back(h[i].id);        for(int i=0; i<m; i++){            if(cnt[H[q[i]]] >= (int)dp[H[q[i]]].size())                cout << -1 << endl;            else{                cout << dp[H[q[i]]][cnt[H[q[i]]]] << endl;                cnt[H[q[i]]]++;            }        }    }    return 0;}
0 0