hdu 5233 Gunner II 离散化

题目链接:

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

题意:

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

题解:

离散化

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //
16 const int maxn = 2e5+10;
17 
18 struct node{
19     int x,id;
20 }h[maxn];
21 
22 vector<int> k,dp[maxn];
23 map<int,int> H;
24 int q[maxn],cnt[maxn];
25 
26 void init(){
27     MS(h); MS(q); MS(cnt);
28     H.clear(); k.clear();
29     for(int i=0; i<maxn; i++)
30         dp[i].clear();
31 }
32 
33 int main(){
34     int n,m;
35     while(scanf("%d%d",&n,&m)!=EOF){
36         init();
37         for(int i=0; i<n; i++){
38             h[i].x = read();
39             h[i].id = i+1;
40             k.push_back(h[i].x);
41         }
42         for(int i=0; i<m; i++){
43             q[i] = read();
44             k.push_back(q[i]);
45         }
46         sort(k.begin(),k.end());
47         k.erase(unique(k.begin(),k.end()),k.end());
48         for(int i=0; i<(int)k.size(); i++){
49             H[k[i]] = i;
50         }
51         for(int i=0; i<n; i++)
52             dp[H[h[i].x]].push_back(h[i].id);
53         for(int i=0; i<m; i++){
54             if(cnt[H[q[i]]] >= (int)dp[H[q[i]]].size())
55                 cout << -1 << endl;
56             else{
57                 cout << dp[H[q[i]]][cnt[H[q[i]]]] << endl;
58                 cnt[H[q[i]]]++;
59             }
60         }
61 
62     }
63 
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/yxg123123/p/6827630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值