USACO/Bronze
백준 15594 Out of Place (USACO January 2018 Bronze 3번)
ssam..
2020. 3. 6. 22:53
문제 링크: https://www.acmicpc.net/problem/15594
15594번: Out of Place
The first line of input contains $N$ ($2 \leq N \leq 100$). The next $N$ lines describe the heights of the cows as they are lined up after Bessie makes her move. Each cow height is an integer in the range $1 \ldots 1,000,000$. Cows may have the same height
www.acmicpc.net
먼저 숫자들을 bef, aft 두 배열에 입력받은후, aft 배열만 sort함으로써, bef 배열을 aft 배열과 최소의 swap으로 똑같이 만드는게 목표임. 앞에서부터 훑으면서, aft 배열과 bef 배열에 있는 숫자가 같으면 continue로 넘어가고, 다르면 swap할 적당한 숫자를 찾아야함. swap 해야할 숫자의 index에서 aft와 bef 배열에 있는 숫자가 같으면 swap할 필요가 전혀 없다는것을 주의해야함.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include <bits/stdc++.h>
using namespace std;
int N, bef[101], aft[101];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for(int i = 0; i < N; i++) {
cin >> bef[i];
aft[i] = bef[i];
}
sort(aft, aft + N);
int ans = 0;
for(int i = 0; i < N; i++) {
if(aft[i] == bef[i]) continue;
for(int j = i; j < N; j++) {
if(aft[i] == bef[j] && aft[j] != bef[j]) {
swap(bef[i], bef[j]);
ans++;
}
}
}
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|