CJOJ 2485 UVa 11991 生日禮物/UVa 11991 Easy Problem from Rujia Liu?

CJOJ 2485 UVa 11991 生日禮物 / UVa 11991 Easy Problem from Rujia Liu?

Description

(原題來自劉汝佳《訓練指南》Page187,UVa 11991.)

可愛的小F就要過生日啦,爲了刷存在感,我們的小F十分誠懇(xian de mei shi)地向女神小E要生日禮物。不過傲嬌(2333)的小E當然不會隨便答應的啦~爲了爲難小F,小E就說:

“如果你能做出這道簡單題,我就給你禮物;如果做不出來,嘿嘿嘿嘿…”
小F感到一絲殺氣…

爲了保小命&拿到禮物,信息學渣渣小F只得向你求助,小F能不能拿到夢寐以求的女神的禮物,全靠你了!

給出一個包含n個整數的數組,你需要回答m個詢問。
每次詢問兩個整數k和v,輸出從左到右第k個v的下標(數組下標從左到右編號爲1~n)。

英文版(原題):
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 easy problem (for example, `the Coco-Cola Store’ in UVa OJ), to encourage more people to solve his problems :D

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

每組數據第一行爲兩個整數n和m,第二行包含n個正整數,即待查詢的數組。
以下m行每行包含兩個整數k和v,意義與題目描述中的相同。
1<=n,m<=100000. 數組中的元素均不超過100000.
1<=k<=n,1<=v<=100000.

Output

對於每個查詢,輸出查詢結果。如果不存在,輸出0。

Sample Input

5 3
1 2 3 3 5
1 1
2 5
2 3

Sample Output

1
0
4

Http

CJOJ:http://oj.changjun.com.cn/problem/detail/pid/2485
UVA:https://vjudge.net/problem/UVA-11991

Source

STL

題目大意

有一個n個整數的數組,接下來有m個詢問,每個詢問有兩個整數k,v,輸出從左到右第k個v的下標,若不存在則輸出0

解決思路

這道題是Vector的運用,令Map[i]表示數字i在數組中出現的位置,從左往右,那麼查詢的時候就只要輸出Map[v][k]即可。
(這麼簡單的題不需要註釋吧)

代碼

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

int n,m;
map<int , vector<int> > Map;

int main()
{
    while (cin>>n>>m)
    {
        int x;
        int k,v;
        Map.clear();
        for (int i=1;i<=n;i++)
        {
            cin>>x;
            Map[x].push_back(i);
        }
        for (int i=1;i<=m;i++)
        {
            cin>>k>>v;
            //cout<<Map[v].size()<<' ';
            if (Map[v].size()<k)
            {
                cout<<0<<endl;
            }
            else
                cout<<Map[v][k-1]<<endl;
        }
    }
    return 0;
}
發佈了33 篇原創文章 · 獲贊 4 · 訪問量 8282
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章