【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

UVa11991 Easy Problem from Rujia Liu?

 思路:  构造数组data,使满足data[v][k]为第k个v的下标。因为不是每一个整数都会出现因此用到map,又因为每个数出现次数不等可能相差很大,因此用到vector。

注意:对于数据的清空与判空不要忘记,而map在调用之前必须有map.count的检查。

代码:

 1 #include<cstdio>
 2 #include<map>
 3 #include<vector>
 4 using namespace std;
 5
 6 int main(){
 7 int n,m;
 8 map<int,vector<int> > table;
 9
10   while(scanf("%d%d",&n,&m)==2) {
11      table.clear();
12      int x;
13      for(int i=1;i<=n;i++) {
14          scanf("%d",&x);      //
15          if(!table.count(x)) table[x] = vector<int>(); //map.count()的判断很重要 //vector的初始化
16          table[x].push_back(i);
17      }
18      int k,v;
19      for(int i=0;i<m;i++) {
20          scanf("%d%d",&k,&v);
21          if(!table.count(v) || table[v].size()<k) printf("0\n"); //在调用map之前定要count判断
22         else printf("%d\n",table[v][k-1]);  //k-1
23      }
24   }
25   return 0;
26 }
时间: 2024-11-13 08:04:04

【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?的相关文章

UVA11991 Easy Problem from Rujia Liu?(第K个V的位置)

Easy Problem from Rujia Liu? Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Bei

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

UVA 11991 - Easy Problem from Rujia Liu? 题目链接 题意:给一个长度n的序列,有m询问,每个询问会问第k个出现的数字的下标是多少 思路:用map和vector搞,map[i][j]直接保存的就是数字i第j个出现,每次直接输出即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <map> using namespace std; i

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

UVA 11991 - Easy Problem from Rujia Liu? 题目链接 题意:给一个长度n的序列,有m询问,每一个询问会问第k个出现的数字的下标是多少 思路:用map和vector搞,map[i][j]直接保存的就是数字i第j个出现,每次直接输出就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <map> using namespace std;

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

[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? 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 con

uva Easy Problem from Rujia Liu?

Problem E 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

uva 11991 Easy Problem from Rujia Liu? Data Structure

// uva 11991 Easy Problem from Rujia Liu? // 给一个包含n个数的数组,需要回答若干个询问,每次询问两个整数 // k和v,输出从左到右第k个v的下标 // // 本题因为n比较大,所以直接开二维数组是不现实的 // 如果直接用vector的话,也是会有大量的浪费 // 所以可以先离散化一下,找到一共有多少个不同的数 // 再用vector动态数组就可以搞定了 // // 看着书上的map写的挺精彩的,我就习用了下来 // // 非常精彩,继续练吧...

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

题目链接:uva 11991 - Easy Problem from Rujia Liu? 题目大意:给出一个包含n个整数的数组,你需要回答若干询问,每次询问两个整数k和v,输出从左到右第k个v的下标 解题思路:用map映射一个vector,对应即为map<int>即为一个可变长的数组,读取数组的时候将对应值放入即可. #include <cstdio> #include <cstring> #include <map> #include <vecto

UVA 11991 Easy Problem from Rujia Liu?【STL】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142 题意: 给一个长度n的序列,有m询问,每个询问会问第k个出现的数字的下标是多少 用map记录 代码: #include <stdio.h> #include <iostream> #include <algorithm> #includ