Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Uva 11991 - Easy Problem from Rujia Liu?(map 查找)

$
0
0

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=229&page=show_problem&problem=3142


题目分析:

每个询问都搜索一次,猜想超时过不了!于是将其弄成一一映射,用data[v][k]存第k个v的下标,询问的时候O(1)的输出,就不会超时喽!用STL中的map与vector来做

#include<cstdio>
#include<vector>
#include<map>
using namespace std;

map<int, vector<int> > m;
int main()
{
	int n, t, temp, i, k, l;
	while(scanf("%d%d", &n, &t) == 2)
	{
		m.clear();
		for(i = 0; i < n; i++)
		{
			scanf("%d", &temp);
			if (! m.count(temp))
				m [temp] = vector<int>();
			m [temp].push_back(i + 1);
		}
		while (t--)
		{
			scanf("%d%d", &k, &l);
			if(m[l].empty() || k > m[l].size())        
                printf("0\n");
            else
                printf("%d\n", m[l][k-1]);       
		}
	}
	return 0;
}


作者:a1006570862 发表于2013-11-27 0:19:02 原文链接
阅读:113 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>