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];

int main()
{
     //freopen("in.txt","r",stdin);
     int n,m;
     while(scanf("%d%d",&n,&m)!=EOF)
     {
           int Max=0;
           for(int i=1;i<=n;i++)
           {
               scanf("%d",&a[i]);
               Max=max(a[i],Max);
          }
          for(int i=1;i<=Max;i++)
              v[i].clear();
         for(int i=1;i<=n;i++)
               v[a[i]].push_back(i);
         while(m--)
         {
               int k,x;
               scanf("%d%d",&k,&x);
               if(v[x].size()<k)
                     printf("0\n");
               else
                     printf("%d\n",v[x][k-1]);
         }
     }
  return 0;
}
时间: 2024-12-26 07:30:29

UVA11991--Easy Problem from Rujia Liu?的相关文章

【暑假】[实用数据结构]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

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

(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

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;

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

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

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