uva 11991 - Easy Problem from Rujia Liu?(STL)

时间:2022-10-06 10:27:02

题目链接:uva 11991 - Easy Problem from Rujia Liu?

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

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

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;
map<int, vector<int> > g;

int main () {
    int N, M, x, y;
    while (scanf("%d%d", &N, &M) == 2) {
        g.clear();
        for (int i = 1; i <= N; i++) {
            scanf("%d", &x);
            if (!g.count(x))
                g[x] = vector<int>();
            g[x].push_back(i);
        }

        for (int i = 0; i < M; i++) {
            scanf("%d%d", &x, &y);
            if (!g.count(y) || g[y].size() < x)
                printf("0\n");
            else
                printf("%d\n", g[y][x-1]);
        }
    }
    return 0;
}