题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933];
题意:
给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少。分的规则为:1、将一个堆分成相邻的两个堆。2、将相邻的两个堆合并。Power oj 。均分纸牌。
#include<bits/stdc++.h> const int maxn = 1e5+50; typedef long long LL; LL T,n,k; LL a[maxn]; int main () { int ic = 0; scanf("%lld",&T); while(T--) { LL sum = 0,ans = 0; scanf("%lld%lld",&n,&k); for(int i = 1;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 = 1;i <= n;i++) { if(a[i]==sum||!a[i]) continue; if(a[i]>sum) { ans++; a[i]-=sum; i--; } else { a[i+1]+=a[i]; a[i]=0; ans++; } } printf("Case #%d: %lld\n",++ic,ans); } } return 0; }
时间: 2024-09-28 19:26:23