From 1bd17a77301438997ccc4e3eaf7ebe04474a5767 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 19 Feb 2025 11:48:40 -0700 Subject: 1623. apple division --- introductory_problems/apple_division.cpp | 23 +++++++++++++++++++++++ introductory_problems/readme.md | 7 +++++++ 2 files changed, 30 insertions(+) create mode 100644 introductory_problems/apple_division.cpp (limited to 'introductory_problems') diff --git a/introductory_problems/apple_division.cpp b/introductory_problems/apple_division.cpp new file mode 100644 index 0000000..11a0450 --- /dev/null +++ b/introductory_problems/apple_division.cpp @@ -0,0 +1,23 @@ +// 1623. Apple Division +#include +using namespace std; + +int main() { + const long long max = INT_MAX; + static int n, p[20]; + long long a, b, best = max; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &p[i]); + for (int i = 0; i < (1 << n); i++) { + a = b = 0; + for (int j = 0; j < n; j++) { + if (i & (1 << j)) + a += p[j]; + else + b += p[j]; + } + best = min(best, abs(a - b)); + } + printf("%lld\n", best); +} diff --git a/introductory_problems/readme.md b/introductory_problems/readme.md index 52a1f51..32519e6 100644 --- a/introductory_problems/readme.md +++ b/introductory_problems/readme.md @@ -112,3 +112,10 @@ The goal is to move all the disks to the right stack using the middle stack. On **Input**: The only input line has an integer `n`: the number of disks. (Example: 2) **Output**: First print an integer `k`: the minimum number of moves. After this, print `k` lines that describe the moves. Each line has two integers `a` and `b`: you move a distk from stack `a` to stack `b`. (Example: 3 12 13 23) + +### Apple Division +There are `n` apples with known weights. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal. + +**Input**: The first input line has an integer `n`: the number of apples. The next input line has `n` integers `p1, p2, ..., pn`: the weight of each apple. (Example: 5`\n`3 2 7 4 1) + +**Output**: Print one integer: the minimum difference between the weights of the groups. (Example: 1) -- cgit v1.2.3