Luogu-P2512 [HAOI2008]糖果传递 贪心

传送门:https://www.luogu.org/problemnew/show/P2512

题意:

有n个小朋友坐成一圈,每人有$a[i]$个糖果。每人只能给左右两人传递糖果。每人每次传递一个糖果代价为1。问使得每个人手中糖果个数相同的最小代价。

思路:

如果不是环。这道题要考虑前 i 个人 和前(i+1)个人的转移,前i个人必须从第i+1个人中拿到$sum(1~i) - i * ave$的个数。

所以总费用就是n个前缀和相加(这里的前缀和已经减去平均值)。因为是环,可以发现以第K个人开始记录的前缀和可以从原来的前缀和推过来。

假定我们切开后的顺序是

$$A[k+1],A[k+2],...,A[M],A[1],...,A[k]A[k+1],A[k+2],...,A[M],A[1],...,A[k] $$

那么其前缀和也会有所变化,即

$$S[k+1]−S[k],S[k+2]−S[k],...,S[M]−S[k],.S[1]+S[M]−S[k],...,S[M]S[k+1]S[k],S[k+2]S[k],...,S[M]S[k],.S[1]+S[M]S[k],...,S[M]$$。

又因为$S[M]$等于0。所以就是求$sum(S[i] - S[k])$的最小值,可以发现这个k为中位数时,取到最小值。

 

//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
        const int maxn = 1e6+9;
        ll a[maxn];
        ll pre[maxn];
int main(){
        int n;ll sum = 0;  scanf("%d", &n);
        for(int i=1; i<=n; i++){
            scanf("%lld", &a[i]);
            sum += a[i];
        }
        sum = sum / n;
        ll ans = 0;
        for(int i=1; i<=n; i++){
            a[i] -= sum;
            pre[i] = pre[i-1] + a[i];
        }

        int m = (n+1)/2;
        nth_element(pre+1,pre+m,pre+1+n);

        ll tp = pre[m];
        for(int i = 1; i<=n; i++){
            ans += abs(pre[i] - tp); 
        }
        printf("%lld\n", ans);
        return 0;

}
View Code

 

posted @ 2018-10-19 18:16  ckxkexing  阅读(262)  评论(0编辑  收藏  举报