注意细节
从左向右,贪心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,cnt=0;
scanf("%d",&t);
while(t--){
int n,k;
scanf("%d%d",&n,&k);
int a[100005];
long long sum=0;
long long ans=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum%k!=0) {
printf("Case #%d: -1\n",++cnt); continue;
}
long long temp=sum/k;
sum=a[0];
if(a[0]>=temp){
int t=a[0]/temp;
if(a[0]%temp==0) t--;
sum-=(a[0]/temp)*temp;
ans+=t;
}
for(int i=1;i<n;i++){
if(sum!=0) ans++;
sum+=a[i];
if(sum>=temp){
int t=sum/temp;
if(sum%temp==0) t--;
sum-=(sum/temp)*temp;
ans+=t;
}
}
printf("Case #%d: %I64d\n",++cnt,ans);
}
}