Skip to content

Commit

Permalink
[Silver IV] Title: 수열 정렬, Time: 216 ms, Memory: 17892 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
shinjaewon99 committed Oct 19, 2023
1 parent e1814c3 commit 45cef8c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 백준/Silver/1015. 수열 정렬/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [Silver IV] 수열 정렬 - 1015

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

### 성능 요약

메모리: 17892 KB, 시간: 216 ms

### 분류

정렬

### 제출 일자

2023년 10월 19일 11:52:52

### 문제 설명

<p>P[0], P[1], ...., P[N-1]은 0부터 N-1까지(포함)의 수를 한 번씩 포함하고 있는 수열이다. 수열 P를 길이가 N인 배열 A에 적용하면 길이가 N인 배열 B가 된다. 적용하는 방법은 B[P[i]] = A[i]이다.</p>

<p>배열 A가 주어졌을 때, 수열 P를 적용한 결과가 비내림차순이 되는 수열을 찾는 프로그램을 작성하시오. 비내림차순이란, 각각의 원소가 바로 앞에 있는 원소보다 크거나 같을 경우를 말한다. 만약 그러한 수열이 여러개라면 사전순으로 앞서는 것을 출력한다.</p>

### 입력

<p>첫째 줄에 배열 A의 크기 N이 주어진다. 둘째 줄에는 배열 A의 원소가 0번부터 차례대로 주어진다. N은 50보다 작거나 같은 자연수이고, 배열의 원소는 1,000보다 작거나 같은 자연수이다.</p>

### 출력

<p>첫째 줄에 비내림차순으로 만드는 수열 P를 출력한다.</p>

36 changes: 36 additions & 0 deletions 백준/Silver/1015. 수열 정렬/수열 정렬.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.Arrays;
import java.util.Scanner;

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

// 1. 크기입력, 배열 크기지정
StringBuilder output = new StringBuilder();
int size = in.nextInt();
int[] arr1 = new int[size];
int[] arr2 = new int[size];

// 2. 배열의 값 입력
for (int i = 0; i < size; i++) {
arr1[i] = in.nextInt();
arr2[i] = arr1[i];
}

// 3. 오름차순 정렬
Arrays.sort(arr2);

for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr1[i] == arr2[j]) {
output.append(j).append(" ");
// 4. 배열에서 값을 찾았으면 -1으로 할당, 추가적인 탐색을 막기위해
arr2[j] = -1;
break;
}
}
}

System.out.println(output.toString());
}
}

0 comments on commit 45cef8c

Please sign in to comment.