UVA - 11991 Easy Problem from Rujia Liu?

题意:给你一个数组,求第k个v的下标是

思路:用map和vector结合,很巧妙的应用,节省了很大的空间

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

map<int,vector<int> > a;

int main(){
    int n,m,x,y;
    while (scanf("%d%d",&n,&m) != EOF){
        a.clear();
        for (int i = 0; i < n; i++){
            scanf("%d",&x);
            if (!a.count(x))
                a[x] = vector<int>();
            a[x].push_back(i+1);
        }
        while (m--){
            scanf("%d%d",&x,&y);
            if (!a.count(y) || a[y].size() < x)
                printf("0\n");
            else printf("%d\n",a[y][x-1]);
        }
    }
    return 0;
}



你可能感兴趣的:(UVA - 11991 Easy Problem from Rujia Liu?)