BestCoder36 1002.Gunner 解题报告

时间:2023-03-09 01:18:45
BestCoder36  1002.Gunner  解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199

题目意思:给出鸟在树上的高度,以及射击到的高度,问每次射击能射中鸟的数量

  用 vector 里面的 lower_bound() 函数求出大于等于某个 x  的下标,upper_bound() 求出大于某个 x 的下标,然后相减就是射中的数量了。vis[] 数组是防止再次击中相同高度的,明显是 0 嘛~~~。

  因为是 huge input ,用到 get_int() 来加快输入(不过貌似没啥用 O.O)。

 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; const int maxn = 1e6 + ;
int vis[maxn];
vector<int>::iterator p1, p2;
vector<int> h; inline int get_int()
{
char ch = getchar();
while (ch < '' || ch > '') {
ch = getchar();
}
int ret = ;
while (ch >= '' && ch <= '') {
ret = ret * + ch - '';
ch = getchar();
}
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, m, q, hi;
while (scanf("%d%d", &n, &m) != EOF) {
h.clear();
for (int i = ; i < n; i++) {
hi = get_int();
h.push_back(hi);
} sort(h.begin(), h.end()); memset(vis, , sizeof(vis));
for (int i = ; i < m; i++) {
q = get_int();
p1 = lower_bound(h.begin(), h.end(), q);
p2 = upper_bound(h.begin(), h.end(), q); int pos1 = p1 - h.begin();
int pos2 = p2 - h.begin();
if (pos1 <= n && pos2 <= n && pos1 != pos2) {
int t1 = h[pos1], t2 = h[pos2];
if (t1 == q) {
if (!vis[pos1]) {
printf("%d\n", pos2-pos1);
vis[pos1] = ;
}
else
printf("0\n");
}
}
else
printf("0\n");
}
}
return ;
}