http://www.lydsy.com/JudgeOnline/problem.php?id=1045

白书上有讲

没ac的坑点在,数据范围n<=1,000,000

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005;
ll a[N], c[N], x, ans, n; int main() {
read(n); ll sum=0;
for1(i, 1, n) read(a[i]), sum+=a[i];
sum/=n;
for1(i, 2, n) c[i]=c[i-1]+sum-a[i-1];
sort(c+1, c+1+n);
x=-c[(n+1)>>1];
for1(i, 1, n) ans+=abs(c[i]+x);
printf("%lld", ans);
return 0;
}

  


Description

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

Input

小朋友个数n 下面n行 ai

Output

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

Sample Input

4
1
2
5
4

Sample Output

4

HINT

数据规模

30% n<=1000

100% n<=100000

Source

 

【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]糖果传递 ★(环形等分:中位数)

    题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Xcode 生成 ipa包

    原地址:http://zengwu3915.blog.163.com/blog/static/2783489720136213239916/ app store的审核收费的需要二周,免费的需要一个月左 ...

  2. HDU4300-Clairewd’s message(KMP前缀匹配后缀)

    Clairewd's message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. Python模块学习 ---- logging 日志记录

    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cp ...

  4. Java之JVM调优案例分析与实战(1) - 高性能硬件上的程序部署策略

    本JVM系列均来源于<深入理解Java虚拟机>一书中,版权归该书作者所有. 环境:一个15万PV/天左右的在线文档类型网站最近更换了硬件系统,新系统硬件为4个CPU.16GB物理内存.OS ...

  5. 〖Linux〗不重复启动某应用程序的脚本

    cmd="/home/scue/bin/ipclient $ipclient" exist=$(ps aux | grep -v 'grep' | grep "$cmd& ...

  6. java反射--动态加载

    Class.forName("类的全称") 1)不仅表示类的类类型,还表示了动态加载类 2)请区分编译,运行 3)编译时刻加载类是静态加载类,运行时刻加载类是动态加载类 比如下面: ...

  7. Android Studio集成SVN报错:can&#39;t use subversion command line client : svn

    Android Studio集成SVN插件,check out出代码后.每次开启都会在右上角出现例如以下错误: Can't use Subversion command line client: sv ...

  8. websphere设置jvm参数

    http://www.quiee.com.cn/archives/592/ websphere 选择服务器-> 应用程序服务器-> Server1-> 进程定义->Java 虚 ...

  9. 面面具到!android重力传感器

    前两篇都是向大家介绍了很有意思的两种手势操作,嵌入我们游戏中,不得不说让游戏的自由度.可玩性和趣味性都增色不少!那么今天继续给大家介绍一亮点!传感器! 一:什么是传感器: 所谓传感器能够探测如光.热. ...

  10. tp请求和响应

    一.请求参数 use think\Request; 1.获取方法如下: http://w.tp.com/index/index/index/user/AAA $this->request-> ...