ZeroJudge M582: Study

You are trapped in an ancient library room, and a note on the desk tells you that you need to find the blueprint of the door mechanism to escape. It is known that there are N bookshelves in the room (numbered 1, 2, ..., N), and the blueprint is divided into M pieces (numbered 1, 2, ..., M), hidden in the corners of some bookshelves. Please output the bookshelf numbers according to the blueprint numbers.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains two positive integers, N (1 ≤ N ≤ 1000) and M (1 ≤ M ≤ N), indicating that there are N bookshelves and M pieces of blueprint fragments.
The second line contains N integers, A_i (0 ≤ ai ≤ M, 1 ≤ i ≤ N), representing the blueprint fragment number on shelf i. If there is no blueprint fragment on shelf i, Ai is 0. Each shelf can have at most one blueprint fragment, and it is guaranteed that the blueprint fragment numbers are not repeated.
Integers in the same line are separated by a space.
Output M positive integers, indicating the shelf number where blueprint fragment numbers 1, 2, ..., and M are located, respectively.
Integers are separated by a space.
10 3
0 2 1 0 0 0 0 0 3 0
3 2 9
5 5
4 1 2 3 5
2 3 4 1 5

Thought Process

Use a Map to store the location of each blueprint fragment. If Ai is not 0, then set Map[Ai] to i.

When outputting, use a for loop with "for (auto it : Map)" and output "it.second". The loop will automatically start from 1, so you don't need to worry about sorting.

Sample Code-ZeroJudge M582: Study

#include <iostream>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    map<int, int>MAP;
    for (int i = 0; i<N; i++)
    {
        int tmp;
        cin >> tmp;
        if (tmp != 0) MAP[tmp] = i+1;
    }
    for (auto it:MAP)
    {
        cout << it.second << " ";
    }
    cout << "\n";
}

//ZeroJudge M582
//Dr. SeanXD

Comments