uva11991(二分查找或map的应用)
11991 - Easy Problem from Rujia Liu?
Time limit: 1.000 seconds
Easy Problem from Rujia Liu?
Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ contests like Rujia Liu's Presents 1 and 2), he occasionally sets easy problem (for example, 'the Coco-Cola Store' in UVa OJ), to encourage more people to solve his problems :D
Given an array, your task is to find the k-th occurrence (from left to right) of an integer v. To make the problem more difficult (and interesting!), you'll have to answer m such queries.
Input
There are several test cases. The first line of each test case contains two integers n, m(1<=n,m<=100,000), the number of elements in the array, and the number of queries. The next line contains n positive integers not larger than 1,000,000. Each of the following m lines contains two integer k and v (1<=k<=n, 1<=v<=1,000,000). The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.
Output
For each query, print the 1-based location of the occurrence. If there is no such element, output 0 instead.
Sample Input
8 4
1 3 2 2 4 3 2 1
1 3
2 4
3 2
4 2
Output for the Sample Input
2
0
7
0
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
简单题,我的方法是先带下标的排序,然后二分查找就行了。
这里用lower_bound()的时候需要用到它的第四个参数:
(这里有解释:http://msdn.microsoft.com/zh-cn/library/34hhk3zb.aspx)
comp
用户定义的谓词函数对象定义一个元素小于另一个。 二进制谓词采用两个参数,并且在满足时返回 true,在未满足时返回 false。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
struct node
{
int id,val;
} a[];
int n,m,k,v;
bool cmp(node x,node y)
{
if(x.val!=y.val)
return x.val<y.val;
else
return x.id<y.id;
}
bool cmp2(node x,int y)
{
return x.val<y;
}
int main()
{
//freopen("in6.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)==)
{
for(int i=; i<n; i++)
{
scanf("%d",&a[i].val);
a[i].id=i+;
}
sort(a,a+n,cmp);
for(int i=;i<=m;i++)
{
scanf("%d%d",&k,&v);
int t=lower_bound(a,a+n,v,cmp2)-a;
if(t+k->=n||a[t+k-].val!=v)
{
printf("0\n");
}
else
{
printf("%d\n",a[t+k-].id);
}
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
大白书上提供了使用map的一种解法,很精彩的map应用,值得学习。
关键是要想清楚为什么这种思路需要用map:如果用数组来存,就必须开data[1000000][100000],开不了这么大。开数组时必须要考虑到每一维最大可能值然后开的不能小于这个值,其实有很多空间浪费了,因为这道题两维不会同时取到最大的。所以就用map在结合vector,用到什么就开什么,空间节约很多,而且map的查找和插入是logn的复杂度,时间可以保证。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
map<int,vector<int> >a;
int n,m,x,k,v;
int main()
{
//freopen("in6.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)==)
{
a.clear();
for(int i=;i<n;i++)
{
scanf("%d",&x);
if(a.count(x)==) a[x]=vector<int>();
a[x].push_back(i+);//a[x]就表示x的映射
}
for(int i=;i<m;i++)
{
scanf("%d%d",&k,&v);
if(a.count(v)==||a[v].size()<k) printf("0\n");
else printf("%d\n",a[v][k-]);
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
注意:1.这里用的是a.count(x)来查找,这个函数就看二叉搜索树里有没有x,有返回1没有返回0.另一种查找方法是a.find(x),如果有x,返回x所在的迭代器位置;没有则返回a.end().
2.数组法和insert()法在map里插入元素时对重复元素的处理不同。前者保持原来的,后者用新的覆盖原来的。
3.a[x]本身就是表示x的映射。
uva11991(二分查找或map的应用)的更多相关文章
- 洛谷 P1571 眼红的Medusa【二分查找】 || 【map】
题目链接:https://www.luogu.org/problemnew/show/P1571 题目描述 虽然Miss Medusa到了北京,领了科技创新奖,但是他还是觉得不满意.原因是,他发现很多 ...
- lamda匿名函数(与sorted(),filter(),map() 一起用), 递归函数, 二分查找
一. 匿名函数 为了解决一些简单的需求而设计的一句话函数. lambda表示的是匿名函数. 不需要用def来声明, 一句话就可以声明出一个函数 语法: 函数名 = lambda 参数: 返回值 ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- K-th Number 线段树(归并树)+二分查找
K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
随机推荐
- “中兴捧月”比赛之——二叉查找树(BST)树的最短路径Java求解
问题描述: BST树,又称二叉查找树,求其到所有叶子节点路径的最小值 测试用例一: 10 5 20 返回15: 测试用例二: 100 20 70 110 120 10 null null 89 nu ...
- Linux安装jdk10
1.官网下载jdk10 下载方式两种,一种是wget下载,一种是windows系统下载,然后上传到linux系统上. 2.解压到/usr/local/java mkdir /usr/local/jav ...
- TIJ读书笔记01-操作符
TIJ读书笔记01-操作符 概述 关系操作符和逻辑操作符 位操作符 类型转换 概述 操作符 操作符接受一个或多个参数,并生成一个新值. 换句话说操作符作用于操作数,生成一个新值.有些操作符会改变操 ...
- IMX6Q RTC驱动分析
对于在工作中学习驱动的,讲究的是先使用,再理解.好吧,我们来看看板子里是如何注册的? 在板文件里,它的注册函数是这样的: imx6q_add_imx_snvs_rtc() 好吧,让我们追踪下去: 1 ...
- java利用反射将pojo转为json对象
最近做以太坊钱包项目需要与前台进行json交互,写一个工具类,经普通javaBean转为json对象 package util; import java.lang.reflect.Field; imp ...
- python之json模块的基本使用
json模块的作用:将字符串和字典相互转换 json和eval的区别: eval函数不能识别null转换成None json可以将null转换成python可以识别的None json序列化和反序列化 ...
- Kubernetes Heapster
Heapster是容器集群监控和性能分析工具,HPA.Dashborad.Kubectl top都依赖于heapster收集的数据. 但是Heapster从kubernetes 1.8以后已经被遗弃了 ...
- ZooKeeper服务-数据模型
ZooKeeper是一个具有高可用性的高性能协调服务. 数据模型 ZooKeeper维护着一个树形层次结构,树中的节点被称为znode.Znode可以用于存储数据,并且有一个与之相关联的ACL(Acc ...
- Python去除字符串的空格
Python能够找出字符串开头和末尾多余的空白. 要确保字符串末尾没有空白,可使用方法rstrip(). 还可以剔除字符串开头的空白,或同时剔除字符串两端的空白. 为此,可分别使用方法lstrip() ...
- 在openstack环境中安装rackspace private cloud --1 环境准备
在一个openstack环境中安装rackspace private cloud, 环境准备: 在good-net网络中创建3个虚拟机vm Network Detail: good-net Netwo ...