UVA 11991 - Easy Problem from Rujia Liu?
题意:给一个长度n的序列,有m询问,每个询问会问第k个出现的数字的下标是多少
思路:用map和vector搞,map[i][j]直接保存的就是数字i第j个出现,每次直接输出即可
代码:
#include <cstdio> #include <cstring> #include <vector> #include <map> using namespace std; int n, m; map<int, vector<int> > ans; int main() { while (~scanf("%d%d", &n, &m)) { int a, b; ans.clear(); for (int i = 1; i <= n; i++) { scanf("%d", &a); if (!ans.count(a)) ans[a] = vector<int> (); ans[a].push_back(i); } while (m--) { scanf("%d%d", &a, &b); if (ans[b].size() < a) printf("0\n"); else printf("%d\n", ans[b][a - 1]); } } return 0; }
11991 - Easy Problem from Rujia Liu?(基础数据结构)
时间: 2024-10-12 19:52:54