Codeforces 1452B Toy Blocks(Greedy)

Description:

题目大意:有 n 个盒子,第 i 个盒子有 ai个木块,有两步操作:

  1. 选择一个盒子
  2. 将选中的第 i 个盒子中的所有木块放多其他所有盒子中

希望将 n-1 个盒子中的木块数量都相等,不能从其他盒子拿出木块,要求放入最小的额外木块,使得无论选择哪一个盒子都能使 n-1 个盒子的木块数量相同。

解题思路:

算法标签:贪心
首先,如果 n-1 个盒子木块数相同,那么至少能被总共木块数 sum 整除。所以其他盒子中的木块数至少为 [ s u m n − 1 \frac{sum}{n-1} n1sum] 。因为不能移除木块,所以最大木块数不会改变,每个其他盒子木块数至少等于最大木块数 maxx。
当 sum ≤ \leq (n-1) × \times × maxx,添加的额外木块数即为:(n-1) × \times × maxx - sum
当 sum > > > (n-1) × \times × maxx,说明多出来了,只需在剩下的(n-1-sum%(n-1))个盒子各添加一个木块即可。

代码:

// TSWorld
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
using namespace std;
#define LL long long
const int N = 200005;

int main()
{
	int T = 0;
	int n = 0;
	int number = 0;
	int maxx = 0;
	LL sum = 0;
	cin>>T;

	while(T--) {
		maxx = 0,sum = 0;
		scanf("%d",&n);
		for(int i = 1;i <= n;i++) {
			scanf("%d",&number);
			sum += number;
			maxx = max(maxx,number);
		}

		if(sum <= 1LL*(n-1)*maxx) {
			cout<<1LL*(n-1)*maxx - sum<<endl;
		}
		else {
			LL add = sum%(n-1);
			if(add != 0) 
				cout<<1LL*(n-1)-add<<endl;
			else
				cout<<0<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值