题意

有n个小朋友坐成一圈,每人有ai个糖果。每人只能给左右两人传递糖果。每人每次传递一个糖果代价为1。

思路

假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就可以算出来:

an
an+k a1-k a2
代价:|k| x a1+a2-x-k a3
代价:|a1-x-k| x a1+a2+a3-2x-k
代价:|a1+a2-2x-k| x
代价:|a1+a2+a3-3x-k|

令bi=sum(a1..i)-ix,则总代价=sum|bi-k|。易知k为中位数时此值最小。问题转化为求中位数……

用自己快速选择第K大模板TLE……sort直接过了……

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;

const int maxn = 1000005;
int a[maxn];
long long sum[maxn], b[maxn];
int main(){
int n;
scanf("%d", &n);
sum[0] = 0;
REP(i, 1, n){
scanf("%d", &a[i]);
sum[i] = sum[i-1] + a[i];
}
long long average = sum[n] / n;
REP(i, 0, n-1){
b[i] = sum[i] - (i) * average;
}
sort(b, b+n);
long long res = 0;
REP(i, 0, n-1){
res += abs(b[i] - b[n/2]);
}
printf("%lld\n", res);
return 0;
}
[/cpp]

BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)的更多相关文章

  1. BZOJ 1045: [HAOI2008] 糖果传递 数学

    1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...

  2. bzoj 1045: [HAOI2008] 糖果传递 贪心

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1812  Solved: 846[Submit][Stat ...

  3. [BZOJ 1045] [HAOI2008] 糖果传递

    题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...

  4. bzoj 1045 [HAOI2008] 糖果传递——设变量推式子

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 费用流TLE. #include<iostream> #include&l ...

  5. bzoj 1045 [HAOI2008] 糖果传递 —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 好像是贪心...但这是一个环... 看博客:http://hzwer.com/2656 ...

  6. bzoj 1045: [HAOI2008] 糖果传递【瞎搞】

    感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...

  7. 【BZOJ 1045】 1045: [HAOI2008] 糖果传递

    1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...

  8. 【BZOJ】1045: [HAOI2008]糖果传递(中位数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1045 白书上有讲 没ac的坑点在,数据范围n<=1,000,000 #include < ...

  9. 1045: [HAOI2008] 糖果传递 - BZOJ

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...

随机推荐

  1. STL: fill,fill_n,generate,generate_n

    fill Assigns the same new value to every element in a specified range. template<class ForwardIter ...

  2. JAVA_HOME is not defined correctly

    这是个神奇的问题.系统运行着运行着,突然就挂了.各种Java包丢失. 1.检查maven配置 .bash_profile   2.检查运行调取文件  .mavenrc 运行 java -version ...

  3. PL/SQL 创建用户及权限操作

    1.创建User create user user01 identified by user01, 2.赋予连接数据库的权限 grant connect to user01; 3.把user00的表E ...

  4. 懒加载 js----例子------图片

    转载自:https://www.jianshu.com/p/9b30b03f56c2 懒加载工具类 <script type="text/javascript"> // ...

  5. 三张图看遍Linux性能监控、测试、优化工具

    Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg在最近的 LinuxCon NA 2014 大 ...

  6. apache服务器设置

    服务器目录 目录说明 bin: apache常用的一些命令 cgi-bin:存放一些脚本语言命令 conf:apache配置文件 error:错误记录 htodcs存放站点文件 logs:记录日志 m ...

  7. Mysql 数据类型及选择原则

    MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 数据库类型的选择对数据库的性能影响很大 1 . 数据类型会影响存储空间的开销 2 . 数据类型会影响 ...

  8. JavaWeb项目中各种路径的获取

    以工程名为/DemoWeb为例: 访问的jsp为: http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工 ...

  9. Flash Builder4注册机

    我的Eclipse下的Flash Builder 4正式版已经过期,之前在网上找到的注册码,都不能用了, 花了很久时间,才找到这个注册机. Flash Builder 4 注册机 Serial Cra ...

  10. centos安装xdebug 和 phpstorm+Xdebug断点调试PHP

    转载地址:http://www.2cto.com/os/201304/206058.html   CentOS下安装xdebug   在CentOS 6.x 的系统中,是集成xdebug 的,   y ...