BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)
题意
有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]糖果传递 ★(环形等分:中位数)的更多相关文章
- 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] 糖果传递——设变量推式子
题目: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& ...
- 【BZOJ】1045: [HAOI2008]糖果传递(中位数)
http://www.lydsy.com/JudgeOnline/problem.php?id=1045 白书上有讲 没ac的坑点在,数据范围n<=1,000,000 #include < ...
- 1045: [HAOI2008] 糖果传递 - BZOJ
Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...
随机推荐
- mysql数据库从删库到跑路之mysql多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 company.employeecompany.department #建表 create table department( id ...
- 62. Unique Paths (走棋盘多少种不同的走法 动态规划)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Connecting to a Remote Serial Port over TCP/IP
https://www.acmesystems.it/socat This article illustrates how to realize a lan to serial gateway Rem ...
- 2016-2017 National Taiwan University World Final Team Selection Contest A - Hacker Cups and Balls
题目: Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figur ...
- 如何做好Web接口测试
说说我在测试接口时遇到的一些需要注意的点: 1.接口返回:数据格式是否与预期一致.例如:要求返回json格式的数据,json数据的key命名是否正确,对应的value是否与数据库一致.需要转换的数据是 ...
- [2012-12-18 14:59:31]AS3 常用正则表达式的总结-不用google了,我帮收集的很多了
正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里转摘出了一些常用的正则表达式语句,大家用到的时候就不用自己写了.红色字体为常用正则:下面这个类是我自己写的一个示例,想必大 ...
- LocalStorage存储JSON对象的问题
LocalStorage存储JSON对象的问题 localStorage - 没有时间限制的数据存储 1 var arr=[1,2,3]; 2 localStorage.setItem(" ...
- Linux各目录缩写含义
Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世纪是相对于计算机技术的产生和发展来说的.在过去的时间里,Unix和它的子分支Linux收集有许多的历史和一些完全古老的语言.在这篇技巧文章 ...
- 虚拟中没有eth0
进行虚拟机的软拷贝和硬拷贝,或直接从一台机器上拷贝虚拟机硬盘文件到另一台机子的虚拟机上时,发现通过修改/etc/network/interfaces配置的IP没用,输入ifconfig,发现根本就没有 ...
- 20145314郑凯杰 《Java程序设计》第8周学习总结
20145314郑凯杰 <Java程序设计>第8周学习总结 教材学习内容总结 代码已托管 第十五章 通用API ①日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用到日志信息 使 ...