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]

  • 相关阅读:
    C++ 使用老牌库xzip & unzip对文件进行压缩解压
    第一次玩蛇,有点紧张。
    fiddler 抓取手机http/https包
    disk或者Partition镜像的制作
    VS2013+phread.h环境配置
    C++ 浅谈 strlen 与 sizeof的区别
    Qt 显示网页的控件
    Qt error: C2236: 意外的标记“class”。是否忘记了“;”?
    初识MySQL——人生若如初相逢
    【学习笔记】HTML基础:列表、表格与媒体元素
  • 原文地址:https://www.cnblogs.com/peccavi/p/4980861.html
Copyright © 2011-2022 走看看