前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Gunner II

Gunner II

作者头像
dejavu1zz
发布2020-10-23 15:16:30
2040
发布2020-10-23 15:16:30
举报

题意描述

Long long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i-th bird stands on the top of the i-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 nearest bird which stands in the tree with height H will falls.

Jack will shot many times, he wants to know which bird will fall during each shot.

给定n棵树和猎人能够进行的m次射击,每次射击能够射下相应高度最近树的鸟,询问落下鸟的顺序。

思路

我们可以使用结构体数组来存储树的序号与树的高度,使用两个数组来存储相应的序号和高度,同样可以使用lower_bound来寻找与射击高度相同的树,如果找到,则让对应的下标数组加1,没有找到则置-1。

下面来举个例子,对于样例

5 5 1 2 3 4 1 1 3 1 4 2

排序过后如图:

排序后
排序后

而对应的low数组和h数组如下:

low[1]=[1],low[2]=5,low[3]=2,low[4]=3,low[5]=4 h[1]=1,h[2]=1,h[3]=2,h[4]=3,h[5]=4

如果寻找到第一颗高为1的树,则low[1]++成为2,当到第二个高为1的样例时,h[low[p]]=5,匹配成功。

AC代码

代码语言:javascript
复制
#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
struct node{
    int idx,h;
}Node[N];
bool cmp(node a,node b){
    if(a.h!=b.h) return a.h<b.h;
    return a.idx<b.idx;
}
int h[N],low[N];
void solve(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&Node[i].h);
            Node[i].idx=i;
        }
        sort(Node+1,Node+1+n,cmp);
        for(int i=1;i<=n;i++) low[i]=i,h[i]=Node[i].h;
        for(int i=1;i<=m;i++){
            int x;scanf("%d",&x);
            int p=lower_bound(h+1,h+1+n,x)-h;
            if(h[low[p]]!=x) printf("-1\n");
            else{
                printf("%d\n",Node[low[p]].idx);
                low[p]++;
            }
        }
    }
}
int main(){
    //IOS;
    solve();
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档