HDU 5933/思维

时间:2021-04-30 20:28:04

题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933];

题意:

  给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少。分的规则为:1、将一个堆分成相邻的两个堆。2、将相邻的两个堆合并。Power oj 。均分纸牌。

#include<bits/stdc++.h>
const int maxn = 1e5+;
typedef long long LL;
LL T,n,k;
LL a[maxn];
int main ()
{
int ic = ;
scanf("%lld",&T);
while(T--)
{
LL sum = ,ans = ;
scanf("%lld%lld",&n,&k);
for(int i = ;i <= n;i++)
{
scanf("%lld",&a[i]);
sum += a[i];
}
if(sum % k)
{
printf("Case #%d: -1\n",++ic);
}
else
{
sum /= k;
for(int i = ;i <= n;i++)
{
if(a[i]==sum||!a[i])
continue;
if(a[i]>sum)
{
ans++;
a[i]-=sum;
i--;
}
else
{
a[i+]+=a[i];
a[i]=;
ans++;
}
}
printf("Case #%d: %lld\n",++ic,ans);
} }
return ;
}