Gunner II--hdu5233(map&vector/二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233
题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 q[i], 求每次
打枪能打下鸟的编号,否则输出-1 ;
STL中的map和vector:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 100010 int dir[][] = { {,}, {-,}, {,}, {,-}, {,}, {,-}, {-,}, {-,-} }; int n, m, h[N], q[N]; map<int, vector<int> > mp; int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
mp.clear(); for(int i=; i<=n; i++) scanf("%d", &h[i]); for(int i=n; i>=; i--) mp[h[i]].push_back(i); for(int i=; i<=m; i++)
{
scanf("%d", &q[i]); if(mp[q[i]].size()>=)
{
printf("%d\n", mp[q[i]].back()); mp[q[i]].pop_back();
}
else printf("-1\n");
}
}
return ;
}
二分:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 100010 int dir[][] = { {,}, {-,}, {,}, {,-}, {,}, {,-}, {-,}, {-,-} }; struct node
{
int num, Id;
}h[N]; int n, m, q[N], vis[N]; int cmp(node a, node b)
{
if(a.num!=b.num)
return a.num < b.num;
return a.Id < b.Id;
} int BeSearch(int L, int R, int num)
{
int ans = -; while(L <= R)
{
int Mid = (L+R)/; if(num == h[Mid].num)
{
if(!vis[Mid])
{
ans = Mid; R = Mid - ;///先输出最左边的;
}
else
L = Mid + ;
}
else if(h[Mid].num > num) R = Mid - ;
else L = Mid + ;
}
if(ans != -)
{
vis[ans] = ;
return h[ans].Id;
}
return -;
} int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
met(vis, );
met(h, );
met(q, ); for(int i=; i<n; i++)
{
scanf("%d", &h[i].num); h[i].Id = i+;
} sort(h, h+n, cmp); for(int i=; i<=m; i++)
{
scanf("%d", &q[i]); int pos = BeSearch(, n, q[i]); printf("%d\n", pos);
}
}
return ;
}
Gunner II--hdu5233(map&vector/二分)的更多相关文章
- hdoj--5233--Gunner II(map+queue&&二分)
Gunner II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Gunner II(二分,map,数字转化)
Gunner II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- uva--11991 - Easy Problem from Rujia Liu?(sort+二分 map+vector vector)
11991 - Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for e ...
- 二分查找 BestCoder Round #42 1002 Gunner II
题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)
传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- map,vector 等容器内容的循环删除问题(C++)
map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...
- hdu5233 Gunner II
Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver ...
随机推荐
- django model 多对多保存
- Netty 源码分析之 番外篇 Java NIO 的前生今世
简介 Java NIO 是由 Java 1.4 引进的异步 IO. Java NIO 由以下几个核心部分组成: Channel Buffer Selector NIO 和 IO 的对比 IO 和 NI ...
- Extjs,实现树形结构的总结
工作总结,用extjs.mybatis.springMVC实现树形显示班级 前台extjs实现树形代码如下: xtype : 'combotree', fieldLabel : '部门名称', nam ...
- [I2C]pca9555应用层测试代码
注意点: 如果在设置I2C_SLAVE的时候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驱动里面二者对应同一个case语句 应用层可以调用接口:i2c_smbus_wri ...
- 关于webRTC中video的使用实践
此次demo使用chrome49调试测试 前端在操作视频输入,音频输入,输出上一直是比较弱的,或者说很难进行相关的操作,经过我最近的一些研究发现,在PC上实际上是可以实现这一系列的功能的,其实现原理主 ...
- MySQL线程池总结
线程池是Mysql5.6的一个核心功能,对于服务器应用而言,无论是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发访问时,一定伴随着资源的不断创建和释放,导致资源利用率低 ...
- Unity3d优化总结2
优化: 1. 更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种, 每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 2. 对于透明贴图,我们只能选择RG ...
- linux -- 查看ip,路由,dns
查看ip地址:ifconfig 查看gateway:route 查看dns:nm-tool
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
- joomla对象注入漏洞分析
0x00 漏洞简单介绍 jooomla 1.5 到 3.4.5 的全部版本号中存在反序列化对象造成对象注入的漏洞,漏洞利用无须登录,直接在前台就可以运行随意PHP代码. Joomla 安全团队紧急公布 ...