hdu 5933 ArcSoft's Office Rearrangement

来源:互联网 发布:c语言的延时函数 编辑:程序博客网 时间:2024/06/01 07:30

ArcSoft, Inc. is a leading global professional computer photography and computer vision technology company.

There are N working blocks in ArcSoft company, which form a straight line. The CEO of ArcSoft thinks that every block should have equal number of employees, so he wants to re-arrange the current blocks into K new blocks by the following two operations:

- merge two neighbor blocks into a new block, and the new block's size is the sum of two old blocks'.
- split one block into two new blocks, and you can assign the size of each block, but the sum should be equal to the old block.

Now the CEO wants to know the minimum operations to re-arrange current blocks into K block with equal size, please help him.
 

Input
First line contains an integer T, which indicates the number of test cases.

Every test case begins with one line which two integers N and K, which is the number of old blocks and new blocks.

The second line contains N numbers a1a2aN, indicating the size of current blocks.

Limits
1T100
1N105
1K105
1ai105
 

Output
For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the minimum operations.

If the CEO can't re-arrange K new blocks with equal size, y equals -1.
 

Sample Input
31 3143 12 3 43 61 2 3
Sample Output
Case #1: -1Case #2: 2Case #3: 3
 

Source
2016年中国大学生程序设计竞赛(杭州)


模拟。

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;typedef long long ll;int main(){int T;ll sum,n,k,a[100010];scanf("%lld",&T);for(int Count=1;Count<=T;Count++){sum=0;scanf("%lld%lld",&n,&k);for(int i=0;i<n;i++){scanf("%lld",&a[i]);sum+=a[i];}if(sum%k){printf("Case #%d: -1\n",Count);continue;}ll quantity=sum/k;ll temp=0,t;ll ans=0;for(int i=0;i<n;i++){if(temp==0){if(a[i]<quantity)temp=a[i];else{t=a[i]/quantity;if(a[i]%quantity)//刚好能除开和不能除开是两种情况...... ans+=t;elseans+=t-1;temp=a[i]%quantity;}}else{if(temp+a[i]<quantity){temp=temp+a[i];ans++;}else{t=(a[i]+temp)/quantity;if((a[i]+temp)%quantity)ans+=t+1;elseans+=t;temp=(a[i]+temp)%quantity;}}}printf("Case #%d: %lld\n",Count,ans);}return 0;}


原创粉丝点击