洛谷-P4447 [AHOI2018 初中组] 分组 题解
解题思路
考虑手玩样例找性质。先约定实力值为
对于这组样例:
7
1 2 2 3 3 4 5
我们以能力值为横轴画出柱状图。
如果划分成
而分成
于是我们可以知道,对于任意两组
因此最优解中的分组两两不包含,
考虑用队列维护每组的人数,并依次处理上面柱状图中的每一列。队列中小组的
设当前列高度为
- 若
h\ge len ,则入队h-len 个0 。 - 若
h<len ,则弹出len-h 个元素并更新答案。
最后队列中的元素整体 tag 维护即可。
细节:特判高度为
时间复杂度
Code
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i(a);i<b;++i)
#define rept(i,a,b) for(int i(a);i<=b;++i)
using namespace std;
constexpr int N=1e5+5,INF=2e9;
int a[N],q[N];
int n,ans=INF;
int l,r,tag;
int lst,cur,h;
void calc(){
// lst:上一列位置
// cur:当前列位置
// h:当前列高度
if(cur-lst>1){ // 中间有高度为0的列
while(l<r) ans=min(ans,q[l++]+tag);
rep(i,0,h) q[r++]=-tag;
++tag;
return;
}
if(h>=r-l) rep(i,0,h-(r-l)) q[r++]=-tag;
else rep(i,0,r-l-h) ans=min(ans,q[l++]+tag);
++tag;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
rept(i,1,n) cin>>a[i];
sort(a+1,a+n+1);
cur=a[1],h=1,lst=-INF;
rept(i,2,n){
if(a[i]==cur) ++h;
else{
calc();
lst=cur;
cur=a[i];
h=1;
}
}
calc();
while(l<r) ans=min(ans,q[l++]+tag); // 清空队列,更新答案
cout<<ans;
return 0;
}