Skip to content

Commit

Permalink
[Silver III] Title: 원상 복구 (small), Time: 824 ms, Memory: 79684 KB -Ba…
Browse files Browse the repository at this point in the history
…ekjoonHub
  • Loading branch information
shinjaewon99 committed Aug 28, 2023
1 parent 8e419a3 commit 0af6ccd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 백준/Silver/22858. 원상 복구 (small)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Silver III] 원상 복구 (small) - 22858

[문제 링크](https://www.acmicpc.net/problem/22858)

### 성능 요약

메모리: 79684 KB, 시간: 824 ms

### 분류

구현, 시뮬레이션

### 문제 설명

<p>$P_1, P_2, \cdots , P_N$의 수가 적혀 있는 $N$개의 카드가 있다.</p>

<p>1부터 N까지 수가 하나씩 존재하는 수열 $D_1, D_2, \cdots , D_i , \cdots , D_N$이 있다. 이때 각 $i$에 대해 $D_i$번째 카드를 $i$번째로 가져오는 작업을 셔플이라고 부른다.</p>

<p>예를 들어, $P_1, P_2, \cdots , P_N$이 1, 4, 5, 3, 2이고, $D_1, D_2, \cdots , D_N$가 4, 3, 1, 2, 5라고 가정해보자. 이 카드를 한번 섞으면 3, 5, 1, 4, 2가 된다. 아래 그림에서 $S$는 카드를 한 번 섞은 후를 의미한다.</p>

<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/c315a95d-a165-4c50-ae75-7da607484771/-/crop/1167x696/355,193/-/preview/" style="height: 358px; width: 600px;"></p>

<p>위 방식을 그대로 $K$번 셔플한 카드의 정보와 $D$의 정보를 알고 있다고 할 때, 원래 카드는 어떤 배치를 이루고 있었는지 구해보자.</p>

### 입력

<p>첫번째 줄에는 카드의 개수 $N$과 카드를 섞은 횟수인 $K$가 공백으로 구분되어 주어진다.</p>

<p>두번째 줄에는 $K$번 카드를 섞은 후 카드의 배치를 의미하는 $S_i$가 공백으로 구분되어 총 $N$개 주어진다.</p>

<p>세번째 줄에는 총 $N$개의 $D_i$이 공백으로 구분되어 주어진다.</p>

### 출력

<p>원래 카드의 배치인 $P_1$부터 $P_N$까지의 값들을 공백으로 구분해서 출력한다.</p>

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

// 1. 카드의 갯수 N 입력
int cardSize = in.nextInt();

// 2. 카드를 섞은 횟수 K 입력
int supple = in.nextInt();

// 3. 카드의 갯수만큼 정수 입력
int[] cards = new int[cardSize + 1];
int[] compare = new int[cardSize + 1];

for (int i = 1; i <= cardSize; i++) {
cards[i] = in.nextInt();
}
for (int i = 1; i <= cardSize; i++) {
compare[i] = in.nextInt();
}

// 4. supple번 만큼 섞습니다.
// 3 5 1 4 2
// 1 4 5 3 2
for (int i = 0; i < supple; i++) {
int[] temp = new int[cardSize + 1];
for (int j = 1; j <= cardSize; j++) {
temp[compare[j]] = cards[j];
}
cards = temp; // 섞은 카드 재 할당
}

for (int i = 1; i <= cardSize; i++) {
System.out.print(cards[i] + " ");
}
}
}

0 comments on commit 0af6ccd

Please sign in to comment.