(2016年中国大学生程序设计竞赛(杭州)-重现赛) ArcSoft's Office Rearrangement 模拟
2016-10-29 18:37
381 查看
ArcSoft’s Office Rearrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
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 a1, a2, ⋯, aN, indicating the size of current blocks.
Limits
1≤T≤100
1≤N≤105
1≤K≤105
1≤ai≤105
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
3
1 3
14
3 1
2 3 4
3 6
1 2 3
Sample Output
Case #1: -1
Case #2: 2
Case #3: 3
题意:
公司有n组人,每组有ai个人,现在要将所有人平分为k组,你可以合并相邻的两组或者拆分一组,为最少操作多少次?
分析:
因为最后是所有的组人数一样,所以一定是不够的合并,多了的拆分。
所以直接模拟一遍就可以了。不过要小心考虑last=0,和刚好能平分的情况。
AC代码:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
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 a1, a2, ⋯, aN, indicating the size of current blocks.
Limits
1≤T≤100
1≤N≤105
1≤K≤105
1≤ai≤105
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
3
1 3
14
3 1
2 3 4
3 6
1 2 3
Sample Output
Case #1: -1
Case #2: 2
Case #3: 3
题意:
公司有n组人,每组有ai个人,现在要将所有人平分为k组,你可以合并相邻的两组或者拆分一组,为最少操作多少次?
分析:
因为最后是所有的组人数一样,所以一定是不够的合并,多了的拆分。
所以直接模拟一遍就可以了。不过要小心考虑last=0,和刚好能平分的情况。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long LL; const int maxn = 100010; LL a[maxn]; int main() { LL t,n,k; scanf("%lld",&t); int kase = 1; while(t--) { scanf("%lld%lld",&n,&k); long long sum = 0; for(int i=0;i<n;i++) { scanf("%lld",&a[i]); sum += a[i]; } if(sum%k != 0) { printf("Case #%d: -1\n",kase++); continue; } LL b = sum/k; LL ans = 0; LL last = 0; for(int i=0;i<n;i++) { LL tmp = last + a[i]; if(last == 0) { if(tmp < b) { last = tmp; } else { if(tmp%b == 0) { ans += tmp/b-1; last = 0; } else { ans += tmp/b; last = tmp%b; } } } else { if(tmp < b) { ans++; last = tmp; } else { if(tmp%b == 0) { ans += tmp/b; last = 0; } else { ans += tmp/b+1; last = tmp%b; } } } } printf("Case #%d: %lld\n",kase++,ans); } return 0; }
相关文章推荐
- HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州) A ArcSoft's Office Rearrangement(贪心)
- HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州)-重现赛【01,02,03,06】
- 2016年中国大学生程序设计竞赛(杭州)-重现赛
- HDU5933 ArcSoft's Office Rearrangement (模拟) 2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(杭州)-重现赛
- (2016年中国大学生程序设计竞赛(杭州)-重现赛) Car 二分 (解决精度问题)
- HDU 5938 Four Operations(贪心+细节)——2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(合肥)-重现赛1008 HDU 5968
- 2016年中国大学生程序设计竞赛(杭州) F Four Operations(暴力枚举)
- 2016年中国大学生程序设计竞赛(合肥)-重现赛1001 HDU 5961
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州)C Car(贪心算法+分数处理)
- 2016年中国大学生程序设计竞赛(合肥)-重现赛
- hdu 5936 二分好题 2016年中国大学生程序设计竞赛(杭州)
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州)