给一个数组,要求输出数字v第k次出现对应的下标
用map<int, vector<int>>存放下标
则查找时只需输出map[v][k-1]即可
代码如下:
#include <map>
#include <vector>
#include <cstdio>
#include <iostream>
using namespace std;
map<int, vector<int> >a;
int main(void) {
int n, m, k, v, x, i;
while(cin >> n >> m) {
a.clear();
for(i=0; i<n; ++i) {
cin >> x;
if(!a.count(x)) {
a[x] = vector<int>();
}
a[x].push_back(i+1);
}
while(m--) {
cin >> k >> v;
if(a.count(v) && a[v].size()>=k)
cout << a[v][k-1] << endl;
else cout << 0 << endl;
}
}
return 0;
}