题解 CF2229F Load Unbalancing

· · 题解

题解 CF2229F Load Unbalancing

本场其他题

在本文中,你将看到评论区中的多种做法,包括几乎稳过的随机化做法。

简要题意

给定长度为 n 的数组 a。你有一个长度为 k、初始全为 0 的数组 b。你可以将 a 中的元素任意排序,然后按你所排的顺序,将每个 a_i 加到当前 b 中任一最小值上。求操作后 b 中最大值最大可以是多少。

数据范围:多测,t\le 10^4,n\le 18,\sum 2^n\le 2^{18}。

做法〇

Solution from @RockyYue.

这位大佬暴搜剪枝获得了 62 ms 的好成绩,感兴趣的可以看看,写在这里仅作膜拜用。

做法一

The same solution as the official editoral.

> **断言** 最大值放最后一项一定最优。 比较简略的证明(不难发现忽略了大量情况,但都不是重点):我们设 $q$ 是 $a$ 中最大值。假设存在一种方案使得最后操作的值为 $p$,$p$ 加到的 $b$ 的项最终为 $u+p$,$q$ 加到的 $b$ 的项最终为 $v+q$($p,q$ 加到同一项显然无意义)。考虑交换 $p,q$ 的加入时间。显然,在本题中,**$b$ 的极差不会超过 $q$**(证明里会频繁涉及,不要忘了),故交换后的答案一定是加了 $q$ 的一项。而且,由于 $q$ 先被加入,故根据题意 $u>v$。 - 若 $v+p\ge u$,则答案变为 $u+q$,因为 $u>v$ 且 $q>p$ 所以 $u+q$ 既大于 $v+q$ 也大于 $u+p$,一定更优。 - 否则,因为 $v$ 加上 $p$ 后还是最小的,故新的答案是 $v+p+q$。 - 若原来的答案是 $v+q$,则新答案自然更大。 - 若原来的答案是 $u+p$,则因为 $v+q>u$,所以 $v+q+p>u+p$,还是更大。 而最大值会被放在放它之前 $b$ 的最小值处。故问题变为了“最小值最大”的形式,且显然答案具备单调性,所以考虑二分答案。 我们希望 $b$ 的最小值不小于我们 check 的点 $x$,就是希望所有值都 $\ge x$,而 $b$ 中的每个值又都是 $a$ 中若干个元素相加得到的,故我们猜想,直接将 $a$(除去最大值,下同)分为 $k$ 个元素和 $\ge v$ 的部分,能分出来就是对的。感性理解一下,如果当前要放在 $b_i$,则取出第 $i$ 部分中未放的最大值即可,这样即使是最后到达 $x$ 的部分也总能取到 $x$。 这样 check 函数的写法就很显然了:设 $dp_{s,i}$ 表示已经用过了 $s$,分出了 $i$ 部分时最多剩下多少,然后暴力转移即可。单组复杂度 $O(n2^n\log V)$,$V$ 是值域。 额不过这个状态是我脑抽写的,其实完全可以把 $i$ 也存到 $dp$ 值里,都是一样的。 :::info[Code] ```cpp //Please ignore those headfiles.I write them just because //DEV-C++ won't support completion if i use <bits/stdc++.h> #pragma GCC optimize(3) #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #include<cctype> #include<string> #include<algorithm> #include<vector> #include<map> #include<unordered_map> #include<set> #include<queue> #define ll long long #define ull unsigned long long #define lf double #define ld long double #define LL __int128 #define ui unsigned int using namespace std; ll T,n,k,a[20],f[300010],g[300010]; bool check(ll now){ for(int i=1;i<(1<<n-1);i++){ f[i]=0; g[i]=0; for(int j=0;j<n-1;j++){ if(i&(1<<j)){ ll tmp=(i-(1<<j)); if(f[tmp]+(g[tmp]+a[j]>=now)>f[i]||f[tmp]+(g[tmp]+a[j]>=now)==f[i]&&(g[tmp]+a[j])*(g[tmp]+a[j]<now)>g[i]){ f[i]=f[tmp]+(g[tmp]+a[j]>=now); g[i]=(g[tmp]+a[j])*(g[tmp]+a[j]<now); } } } } return f[(1<<n-1)-1]>=k; } int main(){ ios::sync_with_stdio(0); cin>>T; while(T--){ cin>>n>>k; for(int i=0;i<n;i++){ cin>>a[i]; } if(n==1){ cout<<a[0]<<endl; continue; } sort(a,a+n); ll l=1,r=18000000000; while(l<=r){ ll mid=(l+r>>1); if(check(mid)){ l=mid+1; } else{ r=mid-1; } } cout<<l-1+a[n-1]<<endl; } return 0; } ``` ::: ### 做法二 > Solution from @[phsads](https://codeforces.com/blog/entry/153948?#comment-1367018). > > 洛谷题解区同样有大佬提及。 学到了新 trick。 首先还是用做法一中的性质。 但是我们不二分。我们想要最大化最小和,故我们将全部 $2^{n-1}$ 个集合按子集和降序排序。设 $dp_s$ 表示 $s$ 至多可以被多少个已加入的集合表示出来,每次加入一个集合时,更新包含它的所有集合,直至某次更新一个 $dp$ 值时其为 $k$。于是这本质上是在枚举每个集合的子集,故单组复杂度为 $O(3^n)$,而且实际上 $n$ 还减了 $1$,可以通过。 为了防止有和我一样没见过的选手不知道复杂度怎么来的,简单写一下证明。 $$\sum_{s\in U}2^{|s|}=\sum_{i=1}^{n-1}\binom{n}{i}2^i=(1+2)^{n-1}=3^{n-1}$$ ### 做法三 > Solution from @[Nachia](https://codeforces.com/blog/entry/153948?#comment-1367191). 首先还是用做法一中的性质。 既然我们只关心最小和,那我们可以考虑降序分出每个集合,则最后一个是这种分法里最小的。这意味着,对于当前选出的一个集合 $s$,设未选的集合为 $r$,还(不算 $s$)需要分出 $k$ 个,则应该有 $$k\sum_{i\in s}a_i\ge\sum_{i\in r}a_i$$ 于是状态和做法一是一样的,不过转移的条件改成了这个。但是这样我们就不用二分了,故复杂度是优秀的 $O(n2^n)$! ### 做法四 > Solution from @[okwedook](https://codeforces.com/blog/entry/153948?#comment-1367195). 洛谷的一位大佬在他的退火题解中提到: > 由于测试数据组数很多,我们对于 $n>5$ 的数据跑模拟退火,对于 $n≤5$ 的数据直接跑暴力。 而 CF 的这位佬想了另一个办法:写一个类似爬山的东西,即每次先 shuffle,然后枚举每个对,看看交换是否更优;然后每轮并行处理所有数据以方便卡时。但是那个佬称他 FST 了,故我将枚举对的过程也改成了随机顺序。经测,这个东西过的概率还挺大的: ![](https://cdn.luogu.com.cn/upload/image_hosting/p8or900l.png) :::info[Code] ```cpp //Please ignore those headfiles.I write them just because //DEV-C++ won't support completion if i use <bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #include<cctype> #include<string> #include<algorithm> #include<vector> #include<map> #include<unordered_map> #include<set> #include<queue> #define ll long long #define ull unsigned long long #define lf double #define ld long double #define LL __int128 #define ui unsigned int using namespace std; struct node{ ll n,k,a[20],b[20],ind[20],ans; void init(){ cin>>n>>k; for(int i=0;i<n;i++){ cin>>a[i]; ind[i]=i; } } ll calc(){ ll mx=0; memset(b,0,sizeof(b)); for(int i=0;i<n;i++){ ll mn=999999999999999999ll,mni=0; for(int j=0;j<k;j++){ if(b[j]<mn){ mn=b[j]; mni=j; } } b[mni]+=a[i]; mx=max(mx,b[mni]); } return mx; } void proc(){ random_shuffle(a,a+n); random_shuffle(ind,ind+n); ll now=calc(); for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ swap(a[ind[i]],a[ind[j]]); ll tmp=calc(); if(tmp>now){ now=tmp; } else{ swap(a[ind[i]],a[ind[j]]); } } } ans=max(ans,now); } }; node a[10010]; ll T; int main(){ ios::sync_with_stdio(0); srand(time(0)); cin>>T; for(int i=0;i<T;i++){ a[i].init(); } while(clock()*1.0/CLOCKS_PER_SEC<1.4){ for(int i=0;i<T;i++){ a[i].proc(); } } for(int i=0;i<T;i++){ cout<<a[i].ans<<endl; } return 0; } ``` :::