惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
G
Google Developers Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Check Point Blog
J
Java Code Geeks
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
量子位
A
About on SuperTechFans
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
Uva 11300.Spreading the Wealth|OhYee 博客
2016-08-21 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

Description

A Communist regime is trying to redistribute wealth in a village.
They have have decided to sit everyone around a circular table.
First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village.
Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins.
Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.

Input

There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the
village. n lines follow, giving the number of coins of each person in the village, in counterclockwise
order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

Output

For each input, output the minimum number of coins that must be transferred on a single line.

Sample Input

3
100
100
100
4
1
2
5
4

Sample Output

0
4

题解

n 个人循环给金币,使大家最后的金币数一样

直接模拟非常麻烦
根据数学推导
假设第 i 个人原有 Ai 个金币,他会给第 i+1 个人(最后一个人给第 1 个) xi 个金币
金币平均数为 M

  • 对于第 1 个: M = A1 - x1 + xn
  • 对于第 2 个: M = A2 - x2 + x1
    可化为: x2 = A2 - M + x1
  • 对于第 3 个: M = A3 - x3 + x2
    可化为: x3 = A3 - M + x2 = A2 + A3 - 2M + x1
  • ……
  • 对于第 n 个: xn = A2 + …… + An - (n-1)M + x1

要求的就是 |x1| + |x2| + |x3| + …… + |xn| 的最小值
也即 |x1| + |A2 - M + x1| + |A2 + A3 - 2M + x1| + …… + |A2 + …… + An - (n-1)M + x1
其中每部分非 x1 的部分全部都是常数,可化成 |x1 - c1| + |x1 - c2| + …… + |x1 - cn|
只需求出距离坐标轴上 c1 c2 c3 …… cn 距离和最短的点
显然最中间的点就是答案,可以取中间点(或中间两个点中间任意一个点),分别求出其到各点的和就是答案

需要使用 long long

状态不佳,简直药丸

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

const int maxn = 1000005;

long long a[maxn];

bool Do() {
    int n;
    if(!(cin >> n))
        return false;

    long long sum = 0;
    for(int i = 0;i < n;i++) {
        long long t;
        cin >> t;
        sum += t;
        if(i == 0)
            a[0] = t;
        else
            a[i] = sum - a[0];
    }
    sum /= n;
    for(int i = 0;i < n;i++) {
        if(i == 0)
            a[0] = 0;
        else
            a[i] -= i*sum;
    }
    sort(a,a + n);

    sum = 0;
    for(int i = 0;i < n;i++)
        sum += abs(a[i] - a[n / 2 - 1]);

    cout << sum << endl;
    return true;
}

int main() {
    while(Do());
    return 0;
}