UVA - 11991 Easy Problem from Rujia Liu?

题目大意:给出一个包含n个整数的数组,你需要回答若干询问,每次询问两个整数k和v,输出从左到右第k个v的下标

解题思路:用map映射一个vector,对应即为map<int>即为一个可变长的数组,读取数组的时候将对应值放入即可。

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

int main() {
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF) {
		map<int, vector<int> > a;
		int x, y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &x);
			if (!a.count(x))
				a[x] = vector<int> ();
			a[x].push_back(i + 1);
		}

		while (m--) {
			scanf("%d%d", &x, &y);
			if (!a.count(y) || a[y].size() < x)
				printf("0\n");
			else
				printf("%d\n", a[y][x-1]);
		}
	}
	return 0;
}


你可能感兴趣的:(UVA - 11991 Easy Problem from Rujia Liu?)