[UVa11991] Easy Problem from Rujia Liu?

留下题目 以启发思维:

Given an array, your task is to find the k-th occurrence (from left to right) of an integer v. To make the problem more difficult (and interesting!), you’ll have to answer m such queries. Input There are several test cases. The first line of each test case contains two integers n, m (1 ≤ n, m ≤ 100, 000), the number of elements in the array, and the number of queries. The next line contains n positive integers not larger than 1,000,000. Each of the following m lines contains two integer k and v (1 ≤ k ≤ n, 1 ≤ v ≤ 1, 000, 000). The input is terminated by end-of-file (EOF). Output For each query, print the 1-based location of the occurrence. If there is no such element, output ‘0’ instead.

Sample Input

8 4

1 3 2 2 4 3 2 1

1 3

2 4

3 2

4 2

Sample Output

2

0

7

0

 

难点:

Q:如何维护一个map?

A:The relationship between a map and a vector --> the relationship between a 2-D array and a 1-D array.

i.e. map由很多个动态数组构成

具体实现方法:

e.g. 

#include <map>
#include <vector>
map<int, vector<int> >a;
int main(){
    a.clear(); //清空a里所有数据
    if(a.count(x)==0) //判断a中以(int)x打头的vector是否为空
    a[x]=vector<int>(); //在a中以(int)x打头create一个动态数组
    a[x].push_back(n); //把a[x]看作一个动态数组,在其后添加元素
    a[x].size() //把a[x]看作一个动态数组,计算其长度
    printf("%d",a[x][n]); //把a看作一个二维数组
}

盲点:

1. 多组数据需clear

2. vector是0-based 然而题目要求的是1-based 所以输出时需特殊处理i.e.a[y][x-1]

 

posted @ 2015-11-20 15:15  Peccavi  阅读(223)  评论(0编辑  收藏  举报