CF - The Party and Sweets
Authors: Jesse Choe, Brad Ma
Solution (Greedy)
Let's consider the following example:
2 2 0 1 1 0
The answer to this example is since boy ends up giving girl too many sweets, even if boy gives girl the minimum number of sweets he could possibly give, which is . However, girl received a maximum of no sweets, making it impossible.
If a single boy gives more sweets than a girl received, then any arrangement of sweets is impossible with the constraints. More formally, if
then the answer is .
If the input provided has an arrangement of sweets that follow the given constraints, then we can greedily find the answer.
Since our objective is to minimize the total number of sweets given out, let's first consider a lower bound on the answer. Since the fewest number of sweets boy gives each girl is , he gives out a total of at least sweets. Thus, the answer to this problem is lower bounded by .
We are not done yet, however. The previous sum does not necessarily satisfy the condition that is the maximum number of sweets a single girl received. Each girl has some boy who gave her sweets instead of sweets. This raises our lower bound by . We would like to choose such that is maximal. Since we know , we would always choose the boy who gives the most sweets.
However, this boy still must give sweets to some girl; thus if no is equal to , then he needs to have given some girl sweets and the boy who gives the second most sweets can give this girl her maximal sweets.
Time Complexity:
C++
C++ Implementation
#include <bits/stdc++.h>using namespace std;using ll = long long;int main() {int n, m;cin >> n >> m;vector<int> b(n), g(m);
Java
import java.io.*;import java.util.*;public class ThePartyAndSweets {public static void main(String[] args) {Kattio io = new Kattio();int n = io.nextInt();int m = io.nextInt();
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!