문제) 백준 - 그리디 (Greedy) - 삼각형 만들기
-> https://www.acmicpc.net/problem/1448
1448번: 삼각형 만들기
첫째 줄에 빨대의 개수 N이 주어진다. N은 3보다 크거나 같고, 1,000,000보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 빨대의 길이가 한 줄에 하나씩 주어진다. 빨대의 길이는 1,000,000보다
www.acmicpc.net
세 변으로 만들 수 있는 삼각형 중 세 변의 합의 최대를 구하는 문제. N이 100만이므로 O(nC3)으로 완전 탐색이 불가능한다. 최대만 구하면 되므로 모든 변을 내림차순으로 정렬한 뒤, 삼각형을 이룰 수 있는 변 3개가 존재하면 답을 출력한다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl "\n" | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 1000001 | |
#define MOD 1000000000 | |
#define int ll | |
using namespace std; | |
int arr[MAX]; | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
int n; cin >> n; | |
for (int i = 0; i < n; ++i) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
for (int i = n - 3; i >= 0; --i) { | |
if (arr[i + 2] < arr[i + 1] + arr[i]) { | |
cout << arr[i + 2] + arr[i] + arr[i + 1] << endl; | |
break; | |
} | |
else if (i == 0) { | |
cout << -1 << endl; | |
} | |
} | |
return 0; | |
} | |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 5557번 - 1학년 (C++) 문제 및 풀이 (0) | 2021.07.27 |
---|---|
[백준] 2186번 - 문자판 (C++) 문제 및 풀이 (0) | 2021.07.27 |
[백준] 1188번 - 음식 평론가 (C++) 문제 및 풀이 (0) | 2021.07.27 |
[백준] 9934번 - 완전 이진 트리 (C++) 문제 및 풀이 (0) | 2021.07.14 |
[백준] 1563번 - 개근상 (C++) 문제 및 풀이 (0) | 2021.07.06 |
댓글