#10010. 「一本通 1.1 练习 6」糖果传递

【题目描述】

原题来自:HAOI 2008

有 n 个小朋友坐成一圈,每人有 ai​ 颗糖果。每人只能给左右两人传递糖果。每人每次传递一颗糖果的代价为 1 。求使所有人获得均等糖果的最小代价。

【输入格式】

第一行有一个整数 n ,表示小朋友个数;

在接下来 n 行中,每行一个整数 ai​。

【输出格式】

输出使所有人获得均等糖果的最小代价。

【样例输入】

4
1
2
5
4


【样例输出】

4


【数据范围与提示】

对于 30% 的数据,n≤1000;

对于 100% 的数据,n≤10^6,保证答案可以用 64位有符号整数存储。

大致思路

原证法是方程:

  • 首先,最终每个小朋友的糖果数量可以计算出来,等于糖果总数除以n,假设C1=A1减去最终每个小朋友的糖果数量,设Xi表示第i个小朋友给了第i-1个小朋友Xi颗糖果,

  • 我们希望Xi的绝对值之和尽量小,即|X1| + |X1-C1| + |X1-C2| + ……+ |X1-Cn-1|要尽量小。注意到|X1-Ci|的几何意义是数轴上的点X1到Ci的距离,所以问题变成了:给定数轴上的n个点,找出一个到他们的距离之和尽量小的点,而这个点就是这些数中的中位数,证明应该都懂

我的理解是每个数减去平均数再加上上一个传递给本次的(传递可为负)。经过排序后取中位数(若为偶数取(n+1)/2),类似于最大值传递给最小值,次大值传递给次小值。最后由于到达各点距离最小点在中位数(mid)上,所以ans+=b[i]-mid;因为b[i]-mid可能为负,而传递只能是正,取绝对值abs(),即ans+=abs(b[i]-mid)

代码实现

#include<bits/stdc++.h>
using namespace std;
long long int n,a[1000055],b[1000055],c[1000055];
long long int k,m,ans=0,mid,sum=0;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	sum=sum/n;
	for(long long int i=1;i<=n;i++){
		b[i]=b[i-1]-a[i]+sum;
		//b[i-1]-(a[i]-sum);
	}
	sort(b+1,b+1+n);
	mid=b[(n+1)/2];
	for(long long int i=1;i<=n;i++){
		//cout<<b[i]<<" "<<mid<<endl;
		ans+=abs(b[i]-mid);
	}
	cout<<ans;
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
import numpy as np import random from resourse import ICA, Country, Empire, Colony, decode,evaluate import matplotlib.pylab as plt import time start = time.time() ica = ICA() best1 = [10, 16, 17, 1, 4, 7, 13, 11, 12, 5, 8, 2, 14, 9, 15, 3, 6, 3, 4, 3, 1, 2, 2, 3, 5, 4, 1, 1, 2, 4, 4, 4, 2, 3] best2 = [1, 5, 12, 11, 14, 2, 10, 4, 6, 7, 17, 16, 3, 8, 13, 9, 15, 5, 4, 1, 4, 1, 1, 1, 3, 3, 5, 2, 4, 3, 5, 2, 4, 3] best3 = [7, 10, 17, 12, 4, 14, 1, 2, 11, 15, 13, 9, 16, 6, 5, 8, 3, 4, 4, 5, 3, 1, 3, 2, 3, 1, 3, 4, 4, 1, 4, 2, 2, 5] bestlst = [best1,best2, best3] country = ica.createCountries() # for num, blst in enumerate(bestlst): # ica.countries[num].ind = blst # ica.countries[num].cost= evaluate(blst) ica.createEmpires() empire = ica.empires # """ index = 1 plt.ion() while len(empire) != 1: empire = ica.empires ica.moveAction() ica.compare() ica.compete() ica.weedOut() ica.revolution() empiresCost = [e.getempirecost() for e in empire] print("第{}伦 -- {}".format(index, empiresCost)) print("帝国数: {} , 殖民地数: {}".format(len(ica.empires), len(ica.colonies))) minEmpiresCost = np.argmin(empiresCost) marsEmpire = empire[minEmpiresCost] # print("第{}伦 -- 战神 :{}".format(index, marsEmpire.ind)) index += 1 print() # plt.pause(0.05) # plt.show() print("cost: {} ,最终坐标: {} ".format(empire[0].cost, empire[0].getPosit())) print(decode(empire[0].getPosit())) # plt.show() # plt.ioff() # """ end = time.time() print("程序process_1的运行时间为:{}s".format(end - start))
最新发布
04-19

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值