STL HDOJ 5199 Gunner

题目传送门

 1 /*
 2     题意:问值为x的个数有几个,第二次查询就是0
 3     lower/upper_bound ()函数的使用,map也可过,hash方法不会
 4 */
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <cstring>
 8 #include <algorithm>
 9 #include <iostream>
10 #include <vector>
11 #include <set>
12 #include <map>
13 #include <queue>
14 using namespace std;
15
16 const int MAXN = 1e6 + 10;
17 const int INF = 0x3f3f3f3f;
18 int a[MAXN], b[MAXN];
19 int used[MAXN];
20 int n, m;
21
22 inline int read(void)
23 {
24     int x = 0, f = 1;    char ch = getchar ();
25     while (ch < ‘0‘ || ch > ‘9‘)    {if (ch == ‘-‘) f = -1;    ch = getchar ();}
26     while (ch >= ‘0‘ && ch <= ‘9‘)    {x = x * 10 + ch - ‘0‘;    ch = getchar ();}
27     return f * x;
28 }
29
30 int main(void)        //HDOJ 5199 Gunner
31 {
32     //freopen ("B.in", "r", stdin);
33
34     while (scanf ("%d%d", &n, &m) == 2)
35     {
36         memset (used, 0, sizeof (used));
37         for (int i=1; i<=n; ++i)    a[i] = read ();
38         for (int i=1; i<=m; ++i)    b[i] = read ();
39
40         sort (a+1, a+1+n);
41         for (int i=1; i<=m; ++i)
42         {
43             int x = upper_bound (a+1, a+1+n, b[i]) - a;
44             int y = lower_bound (a+1, a+1+n, b[i]) - a;
45             if (used[x])
46             {
47                 puts ("0"); continue;
48             }
49             used[x] = 1;
50             printf ("%d\n", x - y);
51         }
52     }
53
54     return 0;
55 }

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <map>
 5 using namespace std;
 6
 7 const int MAXN = 1e6 + 10;
 8 const int INF = 0x3f3f3f3f;
 9
10 inline int read(void)
11 {
12     int x = 0, f = 1;    char ch = getchar ();
13     while (ch < ‘0‘ || ch > ‘9‘)    {if (ch == ‘-‘) f = -1;    ch = getchar ();}
14     while (ch >= ‘0‘ && ch <= ‘9‘)    {x = x * 10 + ch - ‘0‘;    ch = getchar ();}
15     return f * x;
16 }
17 int main(void)
18 {
19     //freopen ("B.in", "r", stdin);
20
21     int n, m;
22     while (scanf ("%d%d", &n, &m) == 2)
23     {
24         map<int, int> ma;    int x;
25         while (n--)
26         {
27             x = read ();   ma[x]++;
28         }
29         map<int, int>::iterator it;
30         while (m--)
31         {
32             x = read ();
33             it = ma.find (x);
34             if (it == ma.end ())
35                 puts ("0");
36             else
37             {
38                 printf ("%d\n", it->second);
39                 it->second = 0;
40             }
41         }
42     }
43
44     return 0;
45 }

map

时间: 2024-08-05 07:43:03

STL HDOJ 5199 Gunner的相关文章

STL HDOJ 5233 Gunner II

题目传送门 1 /* 2 题意:查询x的id,每次前排的树倒下 3 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e5 + 10; 11 const int INF = 0x3f3f3f3

hdoj 5199 Gunner map

Gunner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5199 Description Long long ago, there is a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n

hdu 5199 Gunner(STL之map,水)

Problem Description Long long ago, there is a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i−thbird stands on the top of the i−th tree. The trees stand in straight line fr

hdu 5199 Gunner

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199 简单题,stl水之... 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8 using std::m

HDU - 5198 - Strange Class &amp;&amp; 5199 - Gunner

Strange Class Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 74    Accepted Submission(s): 60 Problem Description In Vivid's school, there is a strange class(SC). In SC, the students' names ar

hdu 5199 Gunner (hash || 排序+二分)

题意:有n个不同高度的树排成一列,树上各有1只鸟. 然后m个询问,每次询问可以打掉h[i](1<=i<=m)高度的所有鸟,问m个询问中的各高度能打掉多少只鸟. 分析: 1.题目给了提示要 “快速读入”  咩~ 2.排序+二分 这里可以两次二分(lower_bound找下界,upper_bound找上界) 也可以合并+二分(合并相同的数字,记录次数) g++提交要用#include<stdio.h> 不要用#include<cstdio> 后者有效率问题,会 tle (⊙

hdoj 1022 Train Problem I 【简易STL】

题意:不解释(这题是学数据结构必做的) 以前自学数据结构的时候,只是会顺序表来模拟栈.最近简单学习了stack头文件 又来做了一遍(还是以前的味道) 代码: #include <stdio.h> #include <stack> #include <string.h> using std::stack; stack<char > s; char s1[100], s2[100]; int vis[10]; char stac[100]; int main()

HDOJ 1004 Let the Balloon Rise (字符串+stl)

题目: Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each co

Hdoj 5194 DZY Loves Balls 【打表】+【STL】

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 394    Accepted Submission(s): 221 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts