※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 27708번 문제인 Antisort이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/27708 

 

27708번: Antisort

In the first test case, First, make 3 grand mix pancakes. You are left with 9 bananas, 32 g of strawberry jam, 85 g of chocolate spread, and 10 walnuts. That is enough for another 9+1+3+1=14 easy pancakes. In the second test case, we only have enough flour

www.acmicpc.net

주어지는 모든 수열의 원소가 모두 다르고 그 길이가 3 이상이라는 조건을 이용하자.

 

주어진 수열을 재배열한 수열 중 답이 될 수 없는 수열은 오름차순 정렬 및 내림차순 정렬의 결과물 단 두 가지 뿐이다. 따라서 주어진 수열을 먼저 (오름차순이나 내림차순으로) 정렬한 뒤 아무 두 수의 위치나 바꿔 출력하면 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int T, N;
int arr[1000];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	cout << T << '\n' << '\n';
	while (T--) {
		cin >> N;
		for (int i = 0; i < N; i++) cin >> arr[i];
		sort(arr, arr + N);
		swap(arr[0], arr[1]);
		cout << N << '\n';
		for (int i = 0; i < N; i++) cout << arr[i] << ' ';
		cout << '\n' << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 27621 // C++] Sum of Three Cubes  (0) 2023.03.04
[BOJ 27627 // C++] Splitology  (0) 2023.03.04
[BOJ 20877 // C++] Minigolf  (0) 2023.03.04
[BOJ 16485 // C++] 작도하자! - ②  (0) 2023.03.03
[BOJ 2616 // C++] 소형기관차  (0) 2023.03.02

+ Recent posts