题意:将给定的n堆员工重新平均分成k堆,每次只能合并相邻两堆或者将一堆分成相邻的两堆,不可行输出-1或者输出最小操作数。
分析:很明显只需要从左往右模拟即可,比平均值多就分出一堆,小了就合并到下一堆。
代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-8;
const int N=1e5+10;
const int M=2e5+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
ll d,k,sum,a[N],ans;
int main()
{
int i,n,ca,T;
scanf("%d", &T);
for (ca=1;ca<=T;ca++) {
scanf("%d%I64d", &n, &k);
sum=0ll;
for (i=1;i<=n;i++) scanf("%I64d", &a[i]),sum+=a[i];
printf("Case #%d: ", ca);
if (sum%k!=0) printf("-1\n");
else {
d=sum/k;ans=0;
for (i=1;i<=n;i++)
if (a[i]<d) {
ans++;a[i+1]+=a[i];
} else if (a[i]==d) continue ;
else {
while (a[i]>d) ans++,a[i]-=d;
if (a[i]==d) continue ;
else ans++,a[i+1]+=a[i];
}
printf("%I64d\n", ans);
}
}
return 0;
}