(DS "algorithm Primer classic") UVA 11991 Easy problem from Rujia Liu? (Index of K-V occurrences)

Source: Internet
Author: User

Main topic:

Index of K-V occurrences

Problem Solving Ideas:

If a data structure can be constructed, so that data[v][k] is the index value of K-V can be solved. DATA[V] represents an array of indices that appear as a number V,

DATA[V][K] represents the index of the first K v occurrence.


Problem eeasy problem from Rujia Liu?

Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi ' an 2006, Beijing Wuhan, or UVa OJ contests like Rujia Liu's presents 1 and 2), he occasionally sets easy problem (for example, ' the C Oco-cola Store ' in UVa OJ-encourage more people to solve his problems:D

Given an array, your task is to find the k-th occurrence (from left to right) of an integer v. The problem more difficult (and interesting!), you'll have to answer m such queries.

Input

There is several test cases. The first line of all test case contains-integers n, m (1<=n,m<=100,000), the number of elements in the array, a nd the number of queries. The next line contains n positive integers not larger than 1,000,000. Each of the following m lines contains the integer k and V (1<=k<=n, 1<=v<=1,000,000). The input is terminated by End-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each query, print the 1-based location of the occurrence. If There is no such element, output 0 instead.

Sample Input
8 41 3 2 2 4 3 2 11 32 43 24 2
Output for the Sample Input
2070
Rujia Liu ' s Present 3: A Data Structure Contest celebrating the 100th anniversary of Tsinghua University
Special thanks:yiming Li
Note:please Make sure to test your program with the gift I/O files before submitting!



The code is as follows:

/* * uva_11991.cpp * *  Created on:2014 December 28 *      author:administrator * * #include <iostream> #include <cs tdio> #include <map> #include <vector>using namespace Std;map<int,vector<int> > Mp;int n,m; int main () {while (scanf ("%d%d", &n,&m)!=eof) {mp.clear (); int i;for (i = 0; i < n; ++i) {int tmp;scanf ("%d", &t MP);/** * Returns the number of occurrences of the key k in the map (for map, because a key corresponds to a value, * Therefore returns only 0 and 1, so you can use this function to determine whether K is in the map) */if (mp.count (tmp) = = 0) {// If this key value does not appear over mp[tmp] = vector<int> ();//Then create a new vector. Notice here that the instantiation of the vector ()}mp[tmp].push_back (i+1);} while (m--) {int k,v;scanf ("%d%d", &k,&v), if (Mp.count (v) = = 0 | | mp[v].size () < K) {//If this value does not exist, or the location that needs to be accessed exceeds the number of occurrences of this only occurrence of printf ("0\n");//Print 1}else{printf ("%d\n", Mp[v][k-1]);}} return 0;}






(DS "algorithm Primer classic") UVA 11991 Easy problem from Rujia Liu? (Index of K-V occurrences)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.