题目链接: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/二分)的更多相关文章

  1. hdoj--5233--Gunner II(map+queue&&二分)

     Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  2. Gunner II(二分,map,数字转化)

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  3. 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 ...

  4. 二分查找 BestCoder Round #42 1002 Gunner II

    题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...

  5. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  6. 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)

    传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...

  7. 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, ...

  8. map,vector 等容器内容的循环删除问题(C++)

    map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...

  9. hdu5233 Gunner II

    Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver ...

随机推荐

  1. LinQ的简单使用

    1.LinQ to Sql类(NET Language Integrated Query (LINQ) ) LINQ定义了大约40个查询操作符,如select.from.in.where以及order ...

  2. 一款jquery和css3实现的卡通人物动画特效

    之前为大家分享了很多jquery和css3的动画实例.今天给大家带来一款非常炫的jquery和css3实现的卡通人物动画特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: < ...

  3. Pgsql排序让空值NULL排在数字后边

    遇到一种情况,对数字进行排序的时候,出现NULL在数字后面的情况,现在的需求是NULL排在前面然后才是升序的排数字 [Oracle 结论] order by colum asc 时,null默认被放在 ...

  4. Spring的AOP简单理解

    最近在研究spring的AOP,翻译出来的意思是面向切面. 总结如下: 所谓AOP就是将分散在各个方法处的公共代码提取到一处, 并通过类似拦截器的机制实现代码的动态整合.可以简单地想象成, 在某个方法 ...

  5. 关闭socket连接最好的方法

    最好关闭连接的方法 `C S` `shutdown-WR ` `发送FIN` ` read-0发送ACK` ` ......` ` close` ` 发送FIN` `read-0` `close` ` ...

  6. Netty系列之Netty百万级推送服务设计要点(转)

    1. 背景 1.1. 话题来源 最近很多从事移动互联网和物联网开发的同学给我发邮件或者微博私信我,咨询推送服务相关的问题.问题五花八门,在帮助大家答疑解惑的过程中,我也对问题进行了总结,大概可以归纳为 ...

  7. 用Zend OPCache提高PHP的性能

    Zend OPCache的前身是Zend Optimizer + (Zend O+),在PHP5.5的发行版本中自带了Zend O+,并重新命名为:Zend OPCache.但是默认是没有启用的,可以 ...

  8. JS调用asp.net后台方法:PageMethods

    先帮朋友宣传一下程序人生(http://www.manong123.com)的网站,里面都是开发感悟,开发人员创业,支持一下吧~ 原来是通过PageMethods来实现的. 举个列子: Default ...

  9. python中的字典 和 集合

    python中字典是一种key-value的数据类型 字典的特性: 1.无序的 2.key必须的唯一的,so,字典天生去重 语法: 增加 修改 删除 查找 多级字典嵌套及操作 字典的其他用法 #set ...

  10. android 编译模块

    android 编译模块 在写完.c文件之后,需要加载到android上进行测试.使用arm-linux-gcc编译,并添加到android开发板上运行失败. 由于android与linux不同,需要 ...