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

 

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

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

 

6904번: Picture Perfect

Roy has a stack of student yearbook photos. He wants to lay the pictures on a flat surface edge-to-edge to form a filled rectangle with minimum perimeter. All photos must be fully visible. Each picture is a square with dimensions 1 unit by 1 unit. For exam

www.acmicpc.net

주어지는 사진의 개수 C에 대하여 해당 수의 약수 d를 한 변으로, C/d를 다른 한 변으로 갖는 사각형들의 둘레 중 최솟값을 찾는 문제이다.

 

산술-기하평균 부등식의 등호조건을 생각하면 d와 C/d가 C의 제곱근에 가까울 수록 사각형의 둘레가 작아짐을 알 수 있다. 이를 이용해 아래와 같이 문제를 빠르게 해결하자.

 

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

#include <iostream>
using namespace std;
typedef long long ll;

ll C;

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

	cin >> C;
	while (C) {
		ll ans = 1000000007, r, c;
		
		for (ll i = 1; i * i <= C; i++) {
			if (C % i) continue;
			ans = i * 2 + (C / i) * 2, r = i, c = C / i;
		}

		cout << "Minimum perimeter is " << ans << " with dimensions " << r << " x " << c << '\n';
		cin >> C;
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 15826 // C++] Namje Adventure  (0) 2023.08.19
[BOJ 15831 // C++] 준표의 조약돌  (0) 2023.08.18
[BOJ 3234 // C++] LUKA  (0) 2023.08.17
[BOJ 6905 // C++] Snakes and Ladders  (0) 2023.08.17
[BOJ 17613 // C++] 점프  (0) 2023.08.17

+ Recent posts