hdu5199Gunner(二分查找)
来源:互联网 发布:office文档修复软件 编辑:程序博客网 时间:2024/05/21 06:23
解题思路:
本题h的取值范围太大,所以无法直接开数组统计
1、 map统计
2、排序+二分
3、hash
4、优先队列
#include<cstdio>#include <cstring>#include <algorithm>#include <queue>#define MAXN 1000010using namespace std;struct node{ int id; int h;}q[MAXN];int ans[MAXN];priority_queue<int,vector<int>,greater<int> > pq;bool cmp(node a,node b){ if(a.h==b.h) return a.id<b.id; return a.h<b.h;}int n,m;int main(){ while(scanf("%d%d",&n,&m)==2){ while(!pq.empty()) pq.pop(); int tm; for(int i=0;i<n;++i) { scanf("%d",&tm); pq.push(tm); } for(int i=0;i<m;++i){ scanf("%d",&q[i].h); q[i].id=i; } sort(q,q+m,cmp); memset(ans,0,sizeof(ans)); for(int i=0;i<m;++i){ int tans=0; while(!pq.empty()&&pq.top()<q[i].h) pq.pop(); while(!pq.empty()&&i<m&&pq.top()==q[i].h){ tans++; pq.pop(); } ans[q[i].id]=tans; if(pq.empty()) break; } for(int i=0;i<m;++i) printf("%d\n",ans[i]); } return 0;}
收获:
map 数组 hash
共同点:一一对应的关系
不同点
1、map的key适用的数据类型比较广泛,而数组的key只能是int类型
2、map插入、查询相对数组来时间消耗大,map O(logN) 数组O(1)
3、数组相对于map来说消耗的空间大
hash相对于数组来说降低了空间消耗,但同时时间消耗又没有太大的影响
二分的思想。。。
0 0
- hdu5199Gunner(二分查找)
- 折半查找(二分查找)
- 二分查找(折半查找)
- 二分查找(折半查找)
- 折半查找(二分查找)
- 折半查找(二分查找)
- 二分查找(折半查找)
- 折半查找(二分查找)
- 查找(一)二分查找
- 二分查找(折半查找)
- 二分查找(折半查找)
- 折半查找(二分查找)
- 二分查找(折半查找)
- 折半查找(二分查找)
- 折半查找(二分查找)
- 折半查找(二分查找)
- 折半查找(二分查找)
- 折中查找(二分查找)
- Asp.net-知识总结(6)
- springMVC --拦截器详细,使用和自定义拦截器
- 伪类after
- 《MFC游戏开发》笔记三 游戏贴图与透明特效的实现
- UVA 11235 Frequent values (RMQ)
- hdu5199Gunner(二分查找)
- Android Funcitons Collection
- 《MFC游戏开发》笔记四 键盘响应和鼠标响应:让人物动起来
- wince web service 使用
- jQuery 的选择器
- UE4风化和老化的影响
- 洗牌算法
- 关于jpeg文件部分解码获取其dct系数遇到的一些问题
- 第二道水题