BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)
生活随笔
收集整理的這篇文章主要介紹了
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| |
代碼
[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]轉載于:https://www.cnblogs.com/AbandonZHANG/p/4114124.html
總結
以上是生活随笔為你收集整理的BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 4738 桥
- 下一篇: 使用Mockito时遇到的一些问题