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. iOS欢迎界面Launch Screen动态加载广告

    有许多应用程序在打开的时候,欢迎界面会加载一张连网获取的广告图片或者显示一组动画,这样的效果是如何做到的呢?下面给大家介绍一种简单的实现加载广告的方式. 程序运行起来,欢迎界面之后,会进入AppDel ...

  2. 锋利的jQuery-4--给事件添加命名空间

    可以把为元素绑定的多个事件用命名空间规范起来. $(function(){ $("p").bind("mouseover.plugin", function() ...

  3. ios开发 <AppName>-Prefix.pch文件的用法详解

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  4. 向github提交代码

    Quick setup — if you've done this kind of thing before https://github.com/KoMiles/emlog.git Create a ...

  5. vsftpd 搭建与介绍

    CentOS Linux Vsftp服务器配置 CentOS Linux Vsftp服务器配置 1.开启防火墙ftp端口           vi /etc/sysconfig/iptables    ...

  6. 和我一起来了解SEO

    基础知识 搜索引擎 搜索引擎爬虫会检索各个网站,分析他们的关键字,从一个连接到另一个连接,如果爬虫觉得这个关键字是有用的 就会存入搜索引擎数据库,反之如果没用的.恶意的.或者已经在数据库的,就会舍弃. ...

  7. Spell checker(暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 De ...

  8. table表头标题th浮动提示-MyTable.js

    /* $(document).ready(function () { var maxH = ($(window).height() - $("#divParent").positi ...

  9. linux查看python安装路径,版本号

    一.想要查看ubuntu中安装的Python路径 方法一:whereis python 方法二:which python 二.想要查看ubuntu中安装的python版本号 python

  10. 基于DCMTK的DICOM相关程序编写攻略

    2008年09月10日 星期三 15:35 基于DCMTK的DICOM相关程序编写攻略 前言: 由于现在的医学影像设备的图像存储和传输正在逐渐向DICOM标准靠拢,在我们进行医学图像处理的过程中,经常 ...