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

题目链接: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;
}
posted @ 2015-06-29 12:58  mengfanrong  阅读(135)  评论(0编辑  收藏  举报