F - Separated Lunch Editorial by en_translator
There are \(2^N\) ways to assign each of the \(N\) departments to either group \(A\) or \(B\) (or \(2^{N-1}\) by considering swapping them).
For each case, one can find in \(O(N)\) time to find the number of people in each group.
Thus, enumerating all the cases, finding the maximum number of people taking break simultaneously for each case, and finding the minimum among them, costs a total of \(O(N\cdot 2^N)\) time.
Under the constraints \(N\leq 20\), this is fast enough.
One can enumerate the ways to assign each of the \(N\) departments to group \(A\) or \(B\) using a binary-based exhaustive search.
Thus, the problem has been solved.
Sample code in C++:
#include <bits/stdc++.h>
using namespace std;
#define N 20
#define INF (int)2e+9
int main() {
int n,k,s,ss=0,ans=INF;
int a[N];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
ss+=a[i];
}
for(int i=0;i<(1<<n);i++){
k=i,s=0;
for(int j=0;j<n;j++){
if(k&(1<<j))s+=a[j];
}
ans=min(ans,max(s,ss-s));
}
cout<<ans<<endl;
return 0;
}
posted:
last update: