728x90
반응형
https://www.acmicpc.net/problem/2961
1. Logic
재료의 갯수 n이 10밖에 안되기 때문에 완전탐색으로 해결 가능하다.
조합 탐색을 하며 선택을 하는 경우 안하는 경우를 구해서 값을 선택 해 주면 된다.
2. Code
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> taste;
long long ans = LLONG_MAX;
long long n;
void solve(long long idx, long long sour, long long bitter) {
if(idx == n) return;
if(sour == 0) {
ans = min(ans, abs(taste[idx].first - taste[idx].second));
solve(idx+1, taste[idx].first, taste[idx].second);
}
else {
ans = min(ans, abs(sour*taste[idx].first - (bitter+taste[idx].second)));
solve(idx+1, sour, bitter);
solve(idx+1, sour*taste[idx].first, bitter+taste[idx].second);
}
return;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
long long sour, bitter;
cin >> sour >> bitter;
taste.push_back({sour, bitter});
}
for(int i = 0; i < n; i++) {
solve(i, 0, 1);
}
cout << ans;
}
알고리즘 Solve 후 제가 생각한 Logic을 기록하는 개인 공부 블로그입니다.
내용 중 최적화가 가능한 부분등은 언제든지 댓글로 틀린 부분 및 피드백 주시면 공부 및 반영하겠습니다🧐
728x90
반응형
'Algorithm > Beakjoon' 카테고리의 다른 글
[백준/Baekjoon] 20310 타노스 C++ :: Implementation & Greedy (2) | 2023.12.03 |
---|---|
[백준/Baekjoon] 22233 가희와 키워드 C++ :: Data structure (4) | 2023.12.02 |
[백준/Baekjoon] 1052 물병 C++ :: Bit masking | Greedy (1) | 2023.11.30 |
[백준/Baekjoon] 1072 게임 C++ :: Binary Search (0) | 2023.11.29 |
[백준/Baekjoon] 2075 N번째 큰 수 C++ :: Data structure & Priority queue (1) | 2023.11.29 |