【BZOJ】1045: [HAOI2008]糖果传递(中位数)
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
1
2
5
4
Sample Output
HINT
数据规模
30% n<=1000
100% n<=100000
Source
【BZOJ】1045: [HAOI2008]糖果传递(中位数)的更多相关文章
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
- bzoj 1045: [HAOI2008] 糖果传递 贪心
1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1812 Solved: 846[Submit][Stat ...
- [BZOJ 1045] [HAOI2008] 糖果传递
题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...
- BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)
题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...
- bzoj 1045 [HAOI2008] 糖果传递——设变量推式子
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 费用流TLE. #include<iostream> #include&l ...
- bzoj 1045 [HAOI2008] 糖果传递 —— 贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 好像是贪心...但这是一个环... 看博客:http://hzwer.com/2656 ...
- bzoj 1045: [HAOI2008] 糖果传递【瞎搞】
感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...
- 【BZOJ 1045】 1045: [HAOI2008] 糖果传递
1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...
- 1045: [HAOI2008] 糖果传递 - BZOJ
Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...
随机推荐
- QtGui.QCheckBox
A QtGui.QCheckBox is a widget that has two states: on and off. It is a box with a label. Check boxes ...
- ReadOnly field saved with NULL value
On CRM opportunity form view, i added readonly="1" for probability field. When i saved, wh ...
- alarm 和 sleep
http://blog.sina.com.cn/s/blog_6a1837e90100uhl3.html alarm也称为闹钟函数,alarm()用来设置信号SIGALRM在经过参数seconds指定 ...
- Java中被你忽视的四种引用(转)
转载自:http://blog.csdn.net/u010425776/article/details/50760053 Java的数据类型分为两类:基本数据类型.引用数据类型. 基本数据类型的值存储 ...
- 【laravel54】composer install与composer update的区别
1.基础概念: 我们需要明白laravel项目里面有2个配置文件,composer.json和composer.lock文件,前者是下载的依赖包配置文件,后者是锁定的包版本信息. 使用之前,需要cd ...
- KISS My YAGNI,KISS (Keep It Simple, Stupid)和 YAGNI (You Ain’t Gonna Need It)软件开发原则
http://www.aqee.net/kiss-my-yagni/我们都知道KISS (Keep It Simple, Stupid)和 YAGNI (You Ain’t Gonna Need It ...
- .net core webapi参数绑定处理
在 Startup的ConfigureServices方法中添加: services.Configure<ApiBehaviorOptions>(options => { optio ...
- 对象的序列化存储:Serializable 和 Parceable
在进行Android开发的时候我们有时候需要用到数据的持久化存储,或者在进程之间传递数据.其中就可能需要用到对象的序列化,经过序列化的对象之后可以通过Intent或者Boundle来传输了.接下来还是 ...
- 初始化列表(const和引用成员)、拷贝构造函数
一.构造函数初始化列表 推荐在构造函数初始化列表中进行初始化 构造函数的执行分为两个阶段 初始化段 普通计算段 (一).对象成员及其初始化 C++ Code 1 2 3 4 5 6 7 8 9 1 ...
- unity, 不要change Default sharedMaterial
假设在场景中加一个sprite,其材质使用默认的Sprites-Default. 若调用: Color color=sprite.GetComponent<SpriteRenderer>( ...