uva 11300 分金币(利用绝对值加和进行求出最小值)
//qq 767039957 welcome
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//这里有点类似于隔离分析法,设 xi为第i个人给上一个人的金比数可正可负
//Di为第1个人到第i个人的金币数总和,M 为每个人的最终金币数
//有
//-x1+xi+Di-1==(i-1)*M
// xi=(i-1)*M+x1-Di-1
// 重要的就是 Di-1-(i-1)*M
//依据这个式子 用x1表示所有人的xi 最后绝对值相加求中位数 (既是绝对值和最小值
vector<LL> g;
vector<LL>S;// 保存Di-1 - (i-1)*M
int main(){
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
LL n,t;
ULL gold_sum=0,M,D,m;
while(~scanf("%lld",&n)){
S.clear();
g.clear();
gold_sum=0;
for(int i=0;i<n;i++){
scanf("%lld",&t);
g.push_back(t);
gold_sum+=t;
}
M=gold_sum/n;
D=0;
m=0;
for(vector<LL>::iterator iter=g.begin();iter!=g.end();iter++){
// cout<<D-m<<"\n";
S.push_back(D-m);
D+=*(iter);
m+=M;
}
nth_element(S.begin(),S.begin()+S.size()/2,S.end());
LL key=S[S.size()/2];
ULL ans=0;
for(vector<LL>::iterator iter=S.begin();iter!=S.end();iter++){
ans+=abs(key-*(iter));
}
printf("%llu\n",ans);
}
return 0;
}
uva 11300 分金币(利用绝对值加和进行求出最小值)的更多相关文章
- cogs 1430. [UVa 11300]分金币
1430. [UVa 11300]分金币 ★☆ 输入文件:Wealth.in 输出文件:Wealth.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 圆桌旁坐着 ...
- UVa 11300 分金币
https://vjudge.net/problem/UVA-11300 题意: 圆桌上有n个人,每个人都有一定的初始金币,每个人可以给他旁边的人一些金币,最终使每个人的金币数相等.计算最少需要转手的 ...
- NLR:利用非线性回归,梯度下降法求出学习参数θ,进而求得Cost函数最优值——Jason niu
import numpy as np import random def genData(numPoints,bias,variance): x = np.zeros(shape=(numPoints ...
- 编写一个应用程序,利用数组或者集合, 求出"HELLO",“JAVA”,“PROGRAM”,“EXCEPTION”四个字符串的平均长度以及字符出现重复次数最多的字符串。
public class Number { public static void main(String[] args) { String[] arr = { "HELLO", & ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- UVa 11300 Spreading the Wealth 分金币
圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...
- UVA.11300 Spreading the Wealth (思维题 中位数模型)
UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...
- BZOJ3293: [Cqoi2011]分金币(数学)
3293: [Cqoi2011]分金币 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1596 Solved: 969[Submit][Status ...
- UVA - 11300 Spreading the Wealth(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
随机推荐
- MYSQL常用命令(转)
1.导出整个数据库mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)mysqldum ...
- C#跨域
//在ConfigureServices中配置 #region 跨域 var urls = "*";//Configuration["AppConfig:Cores&qu ...
- ValueError: Variable conv1/weights already exists.
跑TensorFlow程序的过程中出现了错误,解决之后再次跑时,报如下错误: ValueError: Variable conv1/weights already exists, 原因: 这是因为我在 ...
- 设置listContrl中指定行的颜色
在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...
- elasticsearch 过滤器的种类
elasticsearch之查询过滤 elasticsearch elastic-search xixicat 2月13日发布 推荐 1 推荐 收藏 2 收藏,289 浏览 序 本文主要记录es的查询 ...
- 2019-10-31-C#-dotnet-获取整个局域网的-ip-地址
title author date CreateTime categories C# dotnet 获取整个局域网的 ip 地址 lindexi 2019-10-31 08:57:55 +0800 2 ...
- spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器
创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...
- php 网页内容抓取
最近抓的2个网站内容的代码 列表页抓取:第一种使用phpquery插件,可以快速获取,第二种它是api,所以直接获取 load_third("phpQuery.php"); /** ...
- Nginx 的常用命令
nginx命令,先来一波基本操作: start nginx // 启动Nginx nginx -t // 测试配置文件 nginx -v // 查看Nginx版本 nginx -V // 查看Ngin ...
- 微信小程序——简易动画案例
wxml: <view class="container"> <view animation="{{animation}}" class=&q ...