1. 程式人生 > 其它 >[HAOI2008]糖果傳遞

[HAOI2008]糖果傳遞

洛谷題面

和題解差不多,加了點證明。

題目大意

\(n\) 個小朋友坐成一圈,每人有 \(a_i\) 個糖果。每人只能給左右兩人傳遞糖果。每人每次傳遞一個糖果代價為 \(1\)

題目分析

\(ave\) 表示所有小朋友糖果數量的平均數,因為所有小朋友糖果總數不會變,所以最後每個人的糖果數都會變成平均數。設 \(num_i\) 表示第 \(i\) 個小朋友往左傳的糖果數。

\(num_i\) 為負數,表示向右傳。

可知:

\[a_1+num_2-num_1=ave \] \[a_2+num_3-num_2=ave \] \[\cdots \] \[a_n+num_1-num_n=ave \]

一般地:

\[a_i+num_{i+1}-num_i=ave(1\le i\lt n) \] \[a_i+num_{1}-num_{i}=ave(i=n) \]

我們只保留 \(num\)

\[num_{2}=ave+num_1-a_1 \] \[num_{3}=ave+num_2-a_2=ave+(ave+num_1-a_1)-a_2=2 \cdot ave+num_1-a_1-a_2\] \[\cdots \] \[num_1=ave+num_n-a_n=\dots=n\cdot ave+num_1-a_1-a_2-\dots-a_n \]

我們引入 \(c\) 陣列,定義:

\[c_i=(\sum\limits_{j=1}^na_j)-i\cdot ave \]

故有:

\[num_2=num_1-c_{1} \] \[num_3=num_1-c_2 \] \[\cdots \] \[num_n=num_1-c_n \]

一般地:

\[num_i=num_1-c_i \]

題目要求我們最小化 \(|num_1|+|num_2|+\cdots+|num_n|\),亦即最小化 \(|num_1-c_1|+|num_1-c_2|+\cdots+|num_1-c_n|\)

\(c_i\) 看作數軸上的點,問題轉換為找出一個點 \(num_1\)

使得所有點到 \(num_1\) 距離和最小。

結論:\(num_1\)\(c_i\) 的中位數時距離和最小。

證明:數學歸納法。

秩:區間內任意取值均使得全域性最優時,預設取左端點。

當有兩個數時,顯然 \(num_1\) 取在兩數之間的位置比較好,為了使模型一般化,我們的 \(num_1\) 取為 \(c_1\)。此時 \(num_1\)\(c\) 的中位數。

當有三個數時,假設排序後為 \(c_1,c_2,c_3\),那麼 \(num_1\) 一定在 \(c_1,c_3\) 之間,取哪裡呢?發現放在之間的話,\(|c_1-num_1|+|c_3-num_1|\) 不變,所以我們只需要最小化 \(|c_2-num_1|\)。易得 \(num_1=c_2\) 時最小,此時 \(num_1\)\(c\) 的中位數。

當有四個數時,抽象成兩個數時的模型,根據我們的“秩”,\(num_1\)\(c_2\)

以此類推。

\(\rm Q.E.D.\)

於是我們可以求出 \(num_1,num_2,num_3,\cdots,num_n\)\(|num_1|+|num_2|+\cdots+|num_n|\) 也就不難得到了。

程式碼

//2022/3/10
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#define int long long
#define enter() putchar(10)
#define debug(c,que) cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) cout << arr[i] << w;
#define speed_up() cin.tie(0),cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define Abs(x) ((x) > 0 ? (x) : -(x))
const int mod = 1e9 + 7;
inline int MOD(int x) {
	while (x < 0) x += mod;
	while (x >= mod) x -= mod;
	return x;
}
namespace Newstd {
	char buf[1 << 21],*p1 = buf,*p2 = buf;
	inline int getc() {
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
	}
	inline int read() {
		int ret = 0,f = 0;char ch = getc();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getc();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getc();
		}
		return f ? -ret : ret;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;
using namespace std;

const int ma = 1e6 + 5;
int a[ma],sum[ma],c[ma];
int n,ave;
#undef int
int main(void) {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
    #define int long long
	n = read();
	for (register int i = 1;i <= n; ++ i) a[i] = read(),ave += a[i];
	ave /= n;
	for (register int i = 1;i <= n; ++ i) {
		sum[i] = sum[i - 1] + a[i];
		c[i] = sum[i] - i * ave;
	}
	sort(c + 1,c + n + 1);
	int ans = 0,standard = c[n / 2];
	for (register int i = 1;i <= n; ++ i) {
		ans += Abs(standard - c[i]);
	}
	printf("%lld\n",ans);

	return 0;
}