๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1015๋ฒˆ - ์ˆ˜์—ด ์ •๋ ฌ

by ์•ˆ์ฃผํ˜• 2021. 10. 16.
 

1015๋ฒˆ: ์ˆ˜์—ด ์ •๋ ฌ

P[0], P[1], ...., P[N-1]์€ 0๋ถ€ํ„ฐ N-1๊นŒ์ง€(ํฌํ•จ)์˜ ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ˆ˜์—ด์ด๋‹ค. ์ˆ˜์—ด P๋ฅผ ๊ธธ์ด๊ฐ€ N์ธ ๋ฐฐ์—ด A์— ์ ์šฉํ•˜๋ฉด ๊ธธ์ด๊ฐ€ N์ธ ๋ฐฐ์—ด B๊ฐ€ ๋œ๋‹ค. ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ B[P[i]] = A[i]์ด๋‹ค. ๋ฐฐ์—ด A๊ฐ€ ์ฃผ

www.acmicpc.net

 

//  Copyright © 2021 ์•ˆ์ฃผํ˜•. All rights reserved.
//
//  https://www.acmicpc.net/problem/1015
//  BOJ1015 ์ˆ˜์—ด ์ •๋ ฌ

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);

    int A; //๋ฐฐ์—ด์˜ ํฌ๊ธฐ
    cin >> A;
    vector<int>arr_value;
    vector<int>arr_index;
    vector<int>ans;
    for (int i = 0; i < A; i++) {
        int number;
        cin >> number;
        arr_value .push_back(number);
        arr_index.push_back(number);

    }
    sort(arr_index.begin(), arr_index.end());

    for (int i = 0; i < A; i++) {
        for (int j = 0; j < A; j++) {
            if (arr_value[i] == arr_index[j]) {
                ans.push_back(j);
                arr_index[j] = -1;    //๋จผ์ € ๋‚˜์˜ค๋Š” ๋˜‘๊ฐ™์€ ๊ฐ’์„  ์ธ๋ฑ์Šค๋กœ ํŒ๋‹จํ• ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ฐ’ ๋ณ€๊ฒฝ.
                break;        
            }
        }
    }
    for (int i = 0; i < A; i++) {
        cout << ans[i]<<" ";
    }
}

๋Œ“๊ธ€