【BZOJ 1045】 [HAOI2008] 糖果传递
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
思路来自hzwer..
设xi表示第i个人往左传递了xi个糖果。
(如果小于0表示旁边的人给他了糖果。
则ans=∑|xi|
最后所有人的糖果数都变成sum/n->avg
则
a1-x1+x2 = avg
a2-x2+x3= avg
...
然后可以用avg和x1来表示所有的x2...xn
比如
x2 = x1-(avg-a1)
x3 = x1-(2avg-a1-a2)
....
会发现x1右边的式子都是常量。
且都是x1-ci的形式
那么就相当于有n个点。
然后现在让你选择一个点x1.
使得∑|x1-ci|最小。
显然选择所有这些点里面的中位数就好。
(即ci的中位数
(ci可以观察形式,用递推的方法得到
【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 1e6;
int n;
int a[N+10],c[N+10];
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
scanf("%d",&n);
rep1(i,1,n)
scanf("%d",&a[i]);
LL sum = 0;
rep1(i,1,n) sum+=a[i];
sum/=n;
c[1] = sum-a[1];
rep1(i,2,n-1) c[i] = c[i-1] + sum-a[i];
c[n] = 0;
sort(c+1,c+1+n);
LL x1 = c[n/2+1];
sum = 0;
rep1(i,1,n)
sum+=abs(x1-c[i]);
printf("%lld\n",sum);
return 0;
}
【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]糖果传递 ★(环形等分:中位数)
题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...
- 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 求使所有人获得均等糖果的 ...
随机推荐
- 【原创】不重启was server重新加载应用class文件
类装入和更新检测(Class loading and update detection settings)菜单路径:Applications < Application Types < W ...
- Linux部署之批量自动安装系统之NFS篇
1. 编辑配置文件让远端设备可访问vim /etc/exports 2. 启动服务
- TP5 分页类,自定义样式
结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类. 一.在项目的 extend 目录,创建 cus 目录 二.创建 Page 分页类,代码如下 <?ph ...
- 不实例化一个 class 的时候使用它的property
class A: @property def name(self): " print(A.name) # <property object at 0x10d54cf98> cla ...
- pandas 5 导入导出 读取保存 I/O API
官网The pandas I/O API pickle格式是python自带的 from __future__ import print_function import pandas as pd da ...
- 紫书 例题8-11 UVa 10954 (优先队列)
解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列 #include<cstdio> #include<queue> #define REP(i, a, b) ...
- 【Henu ACM Round#24 C】Quiz
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定是这样 先放k-1个,然后空1个,然后再放k-1个.然后再空1个.. 以此类推. 然后如果(n/k)*(k-1)+n%k> ...
- A题之变态青蛙跳
一仅仅青蛙一次能够跳上1级台阶,也能够跳上2级--它也能够跳上n级. 求该青蛙跳上一个n级的台阶总共同拥有多少种跳法. 分析: 这是一个斐波拉契数列的引申问题,先来看看斐波拉契数列: n<=1, ...
- [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...
- leetCode(38):Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...