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

题目描述
原题来自:HAOI 2008
有 n 个小朋友坐成一圈,每人有 ai 颗糖果 每人只能给左右两人传递糖果。每人每次传递一颗糖果的代价为 1 求使所有人获得均等糖果的最小代价
输入格式
第一行有一个整数 n ,表示小朋友个数
在接下来 n 行中,每行一个整数 ai
输出格式
输出使所有人获得均等糖果的最小代价。
提示
不开long long见祖宗(bushi
AC代码

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long a[1000005],b[1000005];
int main()
{
	long long n;
	cin>>n;
	long long s=0;
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	s/=n;
	for(long long i=1;i<=n;i++){
		b[i]=b[i-1]-a[i]+s;//b[i-1]-a[i]+s
	}
	sort(b+1,b+1+n);
	long long sum=0;
	long long mid=b[(1+n)/2];
	for(long long i=1;i<=n;i++){
		sum+=abs(b[i]-mid);//最小代价
	}
	cout<<sum;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值