这道题目也是比较简单的吧,就是用vector就可以解决,也是一个比较简单的用vector,当做邻接表来用吧。比较方便,实用。
这个没有什么好说的,就是注意每次用完之后都要清楚一遍容器就行了。
贴出代码:
#include <stdio.h> #include <string.h> #include <vector> #include <iostream> #include <string> using namespace std; const int MAXN = 1000000 + 11; int N, M; vector <int> V[MAXN]; void init() { for (int i = 0; i < N; i++) { V[i].clear(); } } int main() { int a; while (scanf("%d%d", &N, &M) != EOF) { init(); for (int i = 1; i <= N; i++) { scanf("%d", &a); V[a].push_back(i); } int k, v; for (int i = 0; i < M; i++) { scanf("%d%d", &v, &k); int nc = V[k].size(); if (nc < v) { printf("0\n"); } else { printf("%d\n", V[k][v - 1]); } } } // system("pause"); return 0; }