题目链接: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. js 内置对象常用方法

    1 内容概述 js包含一些内置对象,如Array,Function,String等,这些是基本的,常用的js类,所以了解它们十分重要:把他们的方法,用例子和文字简要的记录下来,方便今后参看. 2 Ar ...

  2. 一次完整的https过程

    参考: 1. 一次完整的HTTP事务是怎样一个过程? 2. The First Few Milliseconds of an HTTPS Connection 3. 也许,这样理解HTTPS更容易 4 ...

  3. /proc/modules分析

    参考:redhat linux deployment guide--5.2.21.  /proc/modules This file displays a list of all modules lo ...

  4. 微信APP支付 - C#

    最近挺忙的,没时间写东西.然后在弄微信APP支付,网上的搜索一趟,都比较凌乱,我也遇到一些坑,不过也算弄好了,记录分享一下. 1.准备各种调用接口需要的参数,配置app.config. <!-- ...

  5. postgresql data数据目录路径迁移

    默认的数据库路径是/var/lib/pgsql/9.x/data 将现有的数据库文件全部拷贝到新的数据库路径下,然后重启 新建一个路径作为新的数据库数据路径,假如是/home/data sudo mk ...

  6. java 多线程 1 “常用的实现多线程的2种方式”:Thread 和 Runnable

    转载系列自http://www.cnblogs.com/skywang12345/p/java_threads_category.html 当使用第一种方式(继承Thread的方式)来生成线程对象时, ...

  7. linux -- Ubuntu修改静态IP地址重启后无法上网的解决

    ubuntu设置静态IP地址后,上不了网 文章中也提到,如果是在/etc/resolv.conf添加DNS,由于Ubuntu 有一个 resolvconf 服务,如果重启它,那么 /etc/resol ...

  8. 要立刷金组flag了T_T

    刷了那么多银组,发现自己好多不会啊... 果然太弱 在这感谢hzwer神犇的blog.. 大部分题解都从黄学长这里来orz. orz.... 果然我太水

  9. 关于Android Animation的setFillBefore、setFillAfter和setFillEnable

    1. 如果是独立的Animation,只有setFillAfter有效,设置为true动画结束后保持最后的状态 2. 如果是AnimationSet中的Animation,因为Animation的作用 ...

  10. python XlsxWriter Example: Hello World

    http://xlsxwriter.readthedocs.io/example_hello_world.html The simplest possible spreadsheet. This is ...