Easy Problem from Rujia Liu?

Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ contests like Rujia Liu's Presents 1 and 2), he occasionally sets easy problem (for example, 'the Coco-Cola Store' in UVa OJ), to 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. 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). 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 4
1 3 2 2 4 3 2 1
1 3
2 4
3 2
4 2

Output for the Sample Input

2
0
7
0

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!


  STL练习,map+vector。

  题意是“给出一个包含n个整数的数组,你需要回答若干次询问。每次询问两个整数k和v,输出从左到右第k个v的下标(数组下标从左到右编号为1~n)”。

  很显然,这是一个二维的结构,我们用下标代表要查询的数v,a[v][k]就代表第k次出现v的下标值。首先我们会想到开一个二维数组,但是这样空间消耗太大。不如利用C++STL库提供的数据结构来存储,在这里我们可以用 map+vector,申请的变量为为map <int,vector<int> > a。它的好处是存储空间是动态改变的,不用一开始就创建很大的数组来存储。

  代码

 #pragma warning (disable:4786)
#include <stdio.h>
#include <map>
#include <vector>
using namespace std;
int main()
{
int i,n,m,x,k,v;
while(scanf("%d%d",&n,&m)!=EOF){
map <int,vector<int> > a;
for(i=;i<n;i++){ //预处理
scanf("%d",&x);
if(!a.count(x)) //x一次没有出现过
a[x] = vector<int>();
a[x].push_back(i+); //从0开始push
}
//查询
for(i=;i<m;i++){
scanf("%d%d",&k,&v);
if(a.size()<v && a[v].size()<k)
printf("0\n");
else
printf("%d\n",a[v][k-]); //因为是从0开始push,所以k-1
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)的更多相关文章

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

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  2. [UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]

    11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu ...

  3. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  4. UVA 11991 Easy Problem from Rujia Liu?(vector map)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  5. UVA 11991 Easy Problem from Rujia Liu?【STL】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...

  6. STL UVA 11991 Easy Problem from Rujia Liu?

    题目传送门 题意:训练指南P187 分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以) map #include <bits/stdc++.h> u ...

  7. uva 11991 Easy Problem from Rujia Liu? vector+map

    水题 学习一下数据的存储方法. #include<iostream> #include<cstdio> #include<cstdlib> #include< ...

  8. 11991 - Easy Problem from Rujia Liu?(的基础数据结构)

    UVA 11991 - Easy Problem from Rujia Liu? 题目链接 题意:给一个长度n的序列,有m询问,每一个询问会问第k个出现的数字的下标是多少 思路:用map和vector ...

  9. uva--11991 - Easy Problem from Rujia Liu?(sort+二分 map+vector vector)

    11991 - Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for e ...

随机推荐

  1. params参数的调用

    namespace params参数的用法 { class Program { public static void Test(string name,params int[] score) { ; ...

  2. mongo操作

    详细使用网址:http://blog.csdn.net/xinghebuluo/article/details/7050811 MongoDB基本使用 成功启动MongoDB后,再打开一个命令行窗口输 ...

  3. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  4. Eclipse启动时卡死解决方法

    Eclipse未正常关闭时,再次启动通常会卡死,解决方法为:到<workspace>\.metadata\.plugins\org.eclipse.core.resources目录,删除文 ...

  5. Linux下不同机器之间的文件拷贝

    通过 scp 命令实现不同机器之间的文件拷贝. (1)本机考到目标机器:scp 本机文件 目的地: 如:scp /home/odp-web.war   root@192.168.6.137:/usr/ ...

  6. SQL存储过程概念剖析

    一.SQL存储过程的概念,优点及语法 定义:将常用的或很复杂的工作,预先用SQL语句写好并用一个指定的名称存储起来, 那么以后要叫数据库提供与已定义好的存储过程的功能相同的服务时,只需调用execut ...

  7. Jquery 表单操作

    文本框,文本区域: 获取值: 1.$("#txt").attr("value"); 2. $("txt").val(); 单选按钮: 获取值 ...

  8. Strust的基础情况

    Struts的优点: 1.实现MVC模式,结构清晰 2.丰富的标签(tag) 3.通过配置文件页面导航,便于后期维护 4.与Servlet API松耦合,便于测试 Structs2=Structs1的 ...

  9. Linq 中 表连接查询

    public void Test(){ var query = from a in A join b in B on A.Id equals B.Id into c from d in c.Defau ...

  10. 【原创】express3.4.8源码解析之中间件

    前言 注意:旧文章转成markdown格式. 中间件(middleware)的概念来自于TJ的connect库,express就是建立在connect之上. 就如同connect的意思是 连接 一样, ...