Gunner II
题意描述
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代码
#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("-1n");
else{
printf("%dn",Node[low[p]].idx);
low[p]++;
}
}
}
}
int main(){
//IOS;
solve();
return 0;
}
- Farseer:一个用于Silverlight和XNA的开源物理引擎
- Visual Studio 2008 debug的时候发生郁闷的错误ContextSwitchDeadlock was detected
- LINQ TO XML
- 不被未来折叠掉:AI时代下的思考
- Google 分析的基准化测试
- WebFont 三宗罪之一:WebFont 与 FOUT
- 探究基于声明的身份标识
- 深化“互联网+先进制造业”发展工业互联网的系列解读二:打造平台体系
- WebFont 三宗罪之二:吹毛求疵的WebFont 渲染差异
- IDC发布IT转型报告,现代化、自动化、转型三要素必不可少
- 存储过程和触发器的应用
- 两部委印发车联网产业标准体系建设指南 提到了自动驾驶
- 状态开关按钮ToggleButton
- 微软开放 .NET 框架源代码
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法