HAOI2008 糖果傳遞

來源:互聯網
上載者:User

標籤:傳遞   最小   ret   ble   https   getch   string   sort   main   

傳送門

一道很有趣的貪心題(bin哥看了1s就會了%%%)

覺得本題似乎是環形均分紙牌,但是其實我們不需要再枚舉斷點。首先每個人最後分到的糖果數是固定的,我們設x[i]表示第i個人給了ta左邊的人多少顆糖果(第一個人就給到最後一個人),a[i]表示小朋友原來有多少糖果。那麼就有a[i] - x[i] + x[i+1] = ave,也就得到了x[i+1] = x[i] + ave - a[i].令c[i] = a[i] - ave,也就是x[i+1] = x[i] - c[i]。我們就能把所有的x[i]計算出來。我們的目的是讓這個和最小,所以我們只要把所有的c[i]計算出來然後取中位元即可。

看一下代碼。

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<set>#include<queue>#define rep(i,a,n) for(int i = a;i <= n;i++)#define per(i,n,a) for(int i = n;i >= a;i--)#define enter putchar(‘\n‘)using namespace std;typedef long long ll;const int M = 1000005;const int INF = 1000000009;const ll mod = 1e9+7;int read(){    int ans = 0,op = 1;    char ch = getchar();    while(ch < ‘0‘ || ch > ‘9‘)    {    if(ch == ‘-‘) op = -1;    ch = getchar();    }    while(ch >= ‘0‘ && ch <= ‘9‘)    {    ans *= 10;    ans += ch - ‘0‘;    ch = getchar();    }    return ans * op;}ll a[M],sum[M],n,ans,ave,cur;int main(){    n = read();    rep(i,1,n) a[i] = read(),ave += a[i];    ave /= n;    rep(i,1,n) a[i] -= ave,sum[i] = sum[i-1] + a[i];    sort(sum+1,sum+1+n);    cur = sum[(n+1) >> 1];    rep(i,1,n) ans += abs(sum[i] - cur);    printf("%lld\n",ans);    return 0;}

 

HAOI2008 糖果傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.