Codeforce問題の解決


今日、我々はCodeforce問題を解決しようとします:136 a -プレゼント.ああ!では始めましょう.

目次
  • The Question
  • My Analysis with Sample Examples
  • Code and Complexity
  • Links and References

  • 問題
    問題声明

    Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.

    If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.

    Now Petya wants to know for each friend i the number of a friend who has given him a gift.


    入力

    The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.



    出力
    n個のスペース区切りの整数を表示します.i番目の数は友人番号iに贈り物を与えた友人の数と等しくなければなりません.

    サンプル例の分析

    Now, before discussing the solution, I first need to address a mistake in the description in the input of the question as given on Codeforces. In the input description, it says that:

    the i-th number is pi — the number of a friend who gave a gift to friend number i.

    This is not how the input is given in the various test cases. On closer inspection this is very similar to the description of the expected output of the questions:

    i-th number should equal the number of the friend who gave a gift to friend number i.

    So, now the natural question you'll be asking is "What exactly is the input format?"

    If we re-examine the description of the Problem Statement above, we see the line:

    He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi.

    Eureka! This is the correct description of the input format! We can verify this as well with the given sample examples. Let's try that now.




    入力

    4
    2 3 4 1


    ここでは、最初のラインで我々はPetyaは、パーティーに4人の友人を招待して与えられている😅), そして、次の行には、次のように解釈される必要がある数字のリストが与えられています.
  • 友人1は、友人2にプレゼントを与えました
  • 友人2は、友人3にプレゼントを与えました
  • 友人3は、友人4にプレゼントを与えました
  • 友人4は、友人1にプレゼントを与えました
  • さて、今私たちがする必要があるのは、そのような数字のリストを出力することですi-th 友達にプレゼントをくれた友人の数をi .
    出力は以下のようになります:

    4 1 2 3


    この関数は、以下のように解釈されるべきです(注意:太字の数字は出力順に表示されます.
  • 友人4は、友人1にプレゼントを与えました
  • 友人1は、友人2にプレゼントを与えました
  • 友人2は、友人3にプレゼントを与えました
  • 友人3は、友人4にプレゼントを与えました

  • 出力

    4 1 2 3



    例2

    入力

    3
    1 3 2


    ここでは、最初の行では、Petyaはパーティーに3人の友人を招待し、次の行には次のように解釈する必要がある番号のリストが与えられている(通知:ボールドの数字は、入力では、その順序で表示されるものです).
  • フレンドになりたい人
  • 友人2は、友人3にプレゼントを与えました
  • 友人3は、友人2にプレゼントを与えました
  • さて、友人1が新年精神を持っていないようであるという事実以外に、我々がする必要があることは、そのような数のリストを出力することですi-th 友達にプレゼントをくれた友人の数をi .
    出力は以下のようになります:

    1 3 2


    この関数は、以下のように解釈されるべきです(注意:太字の数字は出力順に表示されます.
  • フレンドになりたい人
  • 友人3は、友人2にプレゼントを与えました
  • 友人2は、友人3にプレゼントを与えました

  • 出力

    1 3 2



    例3

    入力

    2
    1 2


    ここで、最初の行では、Petyaが2人の友人をパーティに招待し、次の行に次のように解釈する必要がある番号のリストを与えられます.
  • フレンドになりたい人
  • 友人2は、友人2にプレゼントを与えました
  • さて、友人1と友人2がなぜ彼らがパーティーにプレゼントで来たかについて彼ら自身に尋ね始めるべきであるという事実以外に、我々がする必要があることは、そのような数のリストを出力することですi-th 友達にプレゼントをくれた友人の数をi .
    出力は以下のようになります:

    1 2


    この関数は、以下のように解釈されるべきです(注意:太字の数字は出力順に表示されます.
  • フレンドになりたい人
  • 友人2は、友人2にプレゼントを与えました

  • 出力

    1 2



    コードと複雑さ

    Alright, so now we know how to interpret out input and process it to get our output. To be extra sure we have also verified our logic over the given examples as well. Let's now translate this logic into code. For this I'll be using modern C++ (C++11 and beyond), so that it's easier to get the big picture logic which is implemented in the code.

    Another interesting reason to use modern C++ is that some of its features make the code more readable and similar looking to Python code (many people consider this a plus).

    /**
     * @file 136A-Presents.cpp
     * @author Rishit Chaudhary (@rishitc)
     * @version 1.0
     * @date 2021-06-23
     * 
     * @copyright Copyright (c) 2021
     * 
     */
    
    #include <iostream>
    
    int main()
    {
        std::ios_base::sync_with_stdio(false);
        std::cin.tie(NULL);
        int n;
        int p;
    
        std::cin >> n;
    
        int ans[n];
    
        for (int i = 1; i < n + 1; ++i)
        {
            std::cin >> p;
    
            ans[p - 1] = i; // Zero index correction for p
        }
    
        for (int i = 0; i < n; ++i)
        {
            std::cout << ans[i] << " ";
        }
        std::cout << "\n";
    }
    

    複雑さ
    複雑さタイプ
    アンサー
    マイコメント
    時間複雑度
    o ( n )
    n ( 1 ) ≤ n ≤ 100 -友人ペッタのパーティーに招待の量.
    空間複雑度
    o ( n )
    n ( 1 ) ≤ n ≤ 100 -友人ペッタのパーティーに招待の量.
    難易度
    容易
    問題の言語の使用は、入力の説明の間違いと組み合わせることで質問を最初に把握するのは難しいようになります.

    リンクと参照 Link to the problem: https://codeforces.com/problemset/problem/136/A
    私の提出https://codeforces.com/contest/136/submission/120021060
    私のGITITBリポジトリのソリューションへのリンクhttps://github.com/rishitc/Codeforces-Codes/blob/main/136A-Presents.cpp