题目链接: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. mysql数据库批量操作

    批量KILL会话: 1.首先,根据条件将查询到要kill的进程写入文件:如:desc information_schema.processlist; SELECT concat('KILL ',id, ...

  2. Excel TargetRange.Validation为空的

    做Excel的时候遇到过TargetRange.Validation为空,赋值类似空指针一样的情况. 这样的情况,不懂Excel调试了好久,最后还知道,这个对象需要自己去定义才能够进行赋值, 这样定义 ...

  3. [Busybox]Busybox制作文件系统

    问题: 1.目前busybox和bootstrap两种方案制作文件系统,哪种开发周期更短,更加简单? 2.如果需要在文件系统中添加某个package,要怎么做,如vim/udhcpd等? 转自:htt ...

  4. am335x -- led 控制

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h&g ...

  5. HashMap与ConcurrentHashMap的区别(转)

    从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...

  6. ps aux|awk -F'[ ]+' '$3>90{print $2}'|xargs -n1 kill -9

    ps aux|awk -F'[ ]+' '$3>90{print $2}'|xargs -n1 kill -9

  7. 2015 Multi-University Training Contest 5 1009 MZL's Border

    MZL's Border Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5351 Mean: 给出一个类似斐波那契数列的字符串序列 ...

  8. ASP.NET MVC4 异常拦截

    ASP.NET MVC4 程序发生异常时,通过拦截Action的异常,重写ActionFilterAttribute 的方法OnActionExecuted实现. 具体实现代码如下: /// < ...

  9. 使用JAVASCRIPT进行数据完整性验证

    页面输入完整性是编写BS经常遇到的问题,如果那里需要就到那里写,那可是要花不少的时候,并且造成不必要的浪费,下面是一个通过校验脚本,使用非常方便,通过传入FORM名就可以进行校验,通过在页面控件中增加 ...

  10. Spring Framework 官方文档学习(二)之IoC容器与bean lifecycle

    到目前为止,已经看了一百页.再次感慨下,如果想使用Spring,那可以看视频或者找例子,但如果想深入理解Spring,最好还是看官方文档. 原计划是把一些基本接口的功能.层次以及彼此的关系罗列一下.同 ...