题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5933
题意:给你一个数组含有n个数,然后把这些数分为k份,每份都相等;有两个操作:合并相邻的两个数;把一个数分为两个数;
求最少的操作次数;
#include<stdio.h> #include<string.h> #include<iostream> #include<vector> using namespace std; typedef long long LL; const int N = 100615; LL a[N]; int n, k; int main() { int T, t = 1; scanf("%d", &T); while(T --) { scanf("%d %d", &n, &k); LL sum = 0; for(int i=1; i<=n; i++) { scanf("%I64d", &a[i]); sum += a[i]; } if(sum%k) { printf("Case #%d: -1 ", t++); continue; } LL num = sum/k, ans = 0, r = 0; for(int i=1; i<=n; i++) { if(r) ans ++; if((a[i]+r)%num == 0) ans += (a[i]+r)/num - 1; else ans += (a[i]+r)/num; r = (a[i]+r)%num; } printf("Case #%d: %I64d ", t++, ans); } return 0; }