uva11991(二分查找或map的应用)
2014-08-26 00:27
155 查看
11991 - Easy Problem from Rujia Liu?
Time limit: 1.000 secondsEasy 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 :DGiven 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 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)==2) { a.clear(); for(int i=0;i<n;i++) { scanf("%d",&x); if(a.count(x)==0) a[x]=vector<int>(); a[x].push_back(i+1);//a[x]就表示x的映射 } for(int i=0;i<m;i++) { scanf("%d%d",&k,&v); if(a.count(v)==0||a[v].size()<k) printf("0\n"); else printf("%d\n",a[v][k-1]); } } //fclose(stdin); //fclose(stdout); return 0; }
View Code
注意:1.这里用的是a.count(x)来查找,这个函数就看二叉搜索树里有没有x,有返回1没有返回0.另一种查找方法是a.find(x),如果有x,返回x所在的迭代器位置;没有则返回a.end().
2.数组法和insert()法在map里插入元素时对重复元素的处理不同。前者保持原来的,后者用新的覆盖原来的。
3.a[x]本身就是表示x的映射。
相关文章推荐
- uva 11991 Easy Problem from Rujia Liu?(map应用)
- UVA 11991 map应用
- Easy Problem from Rujia Liu?(UVa 11991) map应用
- Uva 11991 - Easy Problem from Rujia Liu?(map 查找)
- [二分查找]Babelfish uva 10282
- UVALive 3635 Pie 二分查找
- UVA-846 Steps 二分查找
- Can you solve this equation?(二分查找的简单应用)
- 关于二分查找的应用拓展
- UVA 11991 Easy Problem from Rujia Liu?(vector map)
- 二分查找的灵活应用
- UVA 10341 Solve It 解方程 二分查找+精度
- 二分查找的简单应用
- (DP6.1.4.3)UVA 10534 Wavio Sequence(利用二分查找来富足DP)
- UVa 10763 Foreign Exchange (map的应用)
- 二分查找及其应用
- 二分查找的应用
- 【编程珠玑】第二章 二分查找的巧妙应用
- 二分查找在数组中应用的若干实例
- UVA 10487 Closest Sums (二分查找)