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 (⊙o⊙)哦~

二分的时候标记不用再开数组看数用过没,因为数很大啊。。这样会超内存!

直接标记上下界判断这个查找的高度是否访问过。

 1 //#include<cstdio>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 #include<map>
11 #include<set>
12
13 using namespace std;
14
15 typedef long long ll;
16
17 int h[1000010];
18 map<int,int> cnt;
19 bool flag[1000010];
20
21 int nextInt()
22 {
23     char c = getchar();
24     int t = 0;
25     while(c>=‘0‘ && c<=‘9‘)
26     {
27         t = t*10+c-‘0‘;
28         c = getchar();
29     }
30     return t;
31 }
32
33 int main()
34 {
35     int n,m;
36     while(~scanf("%d%d",&n,&m))
37     {
38         getchar();
39         cnt.clear();
40         for(int i=0;i<n;i++)
41         {
42             h[i] = nextInt();
43             cnt[h[i]]++;
44         }
45         sort(h,h+n);
46         memset(flag,0,sizeof(flag));
47         int en = unique(h,h+n)-h,ret;
48         for(int i=1;i<=m;i++)
49         {
50             int q = nextInt();
51             int p = lower_bound(h,h+en,q)-h;
52             if(p!=n && h[p]==q && !flag[p])   //flag[]用来标记上下界
53             {
54                 ret = cnt[q];
55                 flag[p] = true;
56             }
57             else ret = 0;
58             printf("%d\n",ret);
59         }
60     }
61     return 0;
62 }

还可以用map hash来做咩~

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<vector>
 8 #include<queue>
 9 #include<map>
10 #include<set>
11
12 using namespace std;
13
14 typedef long long ll;
15
16 map<ll,int> cnt;
17 ll h[1000010],q;
18
19 ll getInt()
20 {
21     ll t = 0;
22     char c = getchar();
23 //    while(c>‘9‘ || c<‘0‘)
24 //        c = getchar();
25     while(c>=‘0‘ && c<=‘9‘)
26     {
27         t = t*10+c-‘0‘;
28         c = getchar();
29     }
30     return t;
31 }
32
33 int main()
34 {
35     char c;
36     int n,m;
37     while(~scanf("%d%d",&n,&m))
38     {
39         getchar();
40         cnt.clear();
41         for(int i=1;i<=n;i++)
42         {
43             h[i] = getInt();
44             cnt[h[i]]++;
45         }
46         map<ll,int>::iterator iter;
47         for(int i=1;i<=m;i++)
48         {
49             q = getInt();
50             //iter = cnt.find(q);
51             if(cnt.count(q))
52             {
53                 printf("%d\n",cnt[q]);
54                 cnt[q] = 0;
55             }
56             else printf("0\n");
57         }
58     }
59     return 0;
60 }

时间: 2024-10-08 10:28:03

hdu 5199 Gunner (hash || 排序+二分)的相关文章

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 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 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

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 #incl

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

hdu 3264 圆的交+二分

Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1822    Accepted Submission(s): 651 Problem Description The city of M is a famous shopping city and its open-air shopping

hdu 2000 ASCII码排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2000 题目大意:按各字符的ASCII码从小到大的顺序进行排序 注意格式哦!输出时字符中间用一个空格分开 1 #include<stdio.h> 2 int main() 3 { 4 char a,b,c,t; 5 while(scanf("%c%c%c",&a,&b,&c)!=EOF) 6 { 7 getchar(); 8 if(a>b) 9 {

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

hdu 4941 STL HASH 模拟

http://acm.hdu.edu.cn/showproblem.php?pid=4941 比赛的时候现学的map的find...以前都是用下标做的,但是map用下标查询的话,如果查询的元素不存在,会插入一个新的元素. 贴一个map查找元素找到和找不到的模板 map<pair<int,int>,int>::iterator it=poshash.find(tmppos);//pair<int,int>poshash; int pp; if(it == poshash.