UVA11991Easy Problem from Rujia Liu?(map + vector)

题目:UVA11991Easy Problem from Rujia Liu?(map + vector)

题目大意:给你N个数字,这些数字可能有重复,然后M个查询,问第k个V的下标是,如果不存在这样的数字输出0。

解题思路:用map来将每个数字和下标做一一映射,但是因为相同数字会出现多次,所以需要一个数组,并且长度不一定,所以用vector。

代码:

#include <cstdio>
#include <map>
#include <vector>

using namespace std;

map<int , vector<int> > vis;//不要连写

int main () {

	int n, m;
	int v, k;
	while (scanf ("%d%d", &n, &m) != EOF) {

		vis.clear();//清空
		for (int i = 1; i <= n; i++) {

			scanf ("%d", &v);
			if (!vis.count(v))
				vis[v] = vector<int> ();//创建数组
			vis[v].push_back(i);
		}

		for (int i = 0; i < m; i++) {
			scanf ("%d%d", &k, &v);
			if (!vis.count(v) || vis[v].size() < k)
				printf ("0\n");
			else
				printf ("%d\n", vis[v][k - 1]);
		}
	}
	return 0;
}
时间: 2024-10-07 04:17:20

UVA11991Easy Problem from Rujia Liu?(map + vector)的相关文章

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;

UVA11991--Easy Problem from Rujia Liu?

题目链接:点击进入 数据结构水题,用vector记录每个元素所有出现的位置.(其实也可以用map做,只要map的第二维声明为vector就行). 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; #define maxn 1100000 vector<int>v[maxn]; int a[maxn/10

(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