码迷,mamicode.com
首页 > 编程语言 > 详细

bzoj 1045 [HAOI2008] 糖果传递 排序

时间:2018-08-14 22:54:50      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:min   复杂度   names   char   dig   void   排序   lin   etc   

题面

题目传送门

解法

环形均分纸牌

这道题是一模一样的

时间复杂度:\(O(n\ log\ n)\)

代码

#include <bits/stdc++.h>
#define int long long
#define N 1000010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
int a[N];
main() {
    int n, sum = 0; read(n);
    for (int i = 1; i <= n; i++)
        read(a[i]), sum += a[i];
    for (int i = 1; i <= n; i++) a[i] -= sum / n;
    for (int i = 1; i <= n; i++) a[i] += a[i - 1];
    sort(a + 1, a + n + 1); int k = (n + 1) / 2;
    int ans = 0;
    for (int i = 1; i <= n; i++) ans += abs(a[i] - a[k]);
    cout << ans << "\n";
    return 0;
}

bzoj 1045 [HAOI2008] 糖果传递 排序

标签:min   复杂度   names   char   dig   void   排序   lin   etc   

原文地址:https://www.cnblogs.com/copperoxide/p/9478264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!