zoukankan      html  css  js  c++  java
  • [UVa11991] Easy Problem from Rujia Liu?

    留下题目 以启发思维:

    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). 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

    Sample Output

    2

    0

    7

    0

    难点:

    Q:如何维护一个map?

    A:The relationship between a map and a vector --> the relationship between a 2-D array and a 1-D array.

    i.e. map由很多个动态数组构成

    具体实现方法:

    e.g. 

    #include <map>
    #include <vector>
    map<int, vector<int> >a;
    int main(){
        a.clear(); //清空a里所有数据
        if(a.count(x)==0) //判断a中以(int)x打头的vector是否为空
        a[x]=vector<int>(); //在a中以(int)x打头create一个动态数组
        a[x].push_back(n); //把a[x]看作一个动态数组,在其后添加元素
        a[x].size() //把a[x]看作一个动态数组,计算其长度
        printf("%d",a[x][n]); //把a看作一个二维数组
    }

    盲点:

    1. 多组数据需clear

    2. vector是0-based 然而题目要求的是1-based 所以输出时需特殊处理i.e.a[y][x-1]

  • 相关阅读:
    快速傅立叶变换
    回文树
    gcc 编译c文件的几个过程
    linux quota---mount
    linux device driver3 读书笔记(一)
    linux驱动开发(十一)linux内核信号量、互斥锁、自旋锁
    linux驱动开发(十)——misc杂散设备
    linux驱动(九)platform驱动模型详解,以及基于platform驱动模型的led驱动
    (转)__ATTRIBUTE__ 你知多少?
    linux驱动(八)驱动设备模型
  • 原文地址:https://www.cnblogs.com/peccavi/p/4980861.html
Copyright © 2011-2022 走看看