uva 11300 (代数分析)

来源:互联网 发布:南京行知中学高中部 编辑:程序博客网 时间:2024/06/05 12:44
/* * 11300.cpp * *  Created on: Dec 31, 2012 *      Author: joker */#define Main 1#define INPUT_REDIRECT "11300.test"#if Main#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;const int maxn = 1000000 + 10;long long a[maxn];long long c[maxn];int main(int argc, char **argv) {#ifndef ONLINE_JUDGEfreopen(INPUT_REDIRECT, "r", stdin);#endifint n;while (scanf("%d", &n) != EOF) {long long total = 0, tmp = 0;long long m;for (int i = 0; i < n; ++i) {scanf("%lld", &tmp);total += tmp;a[i] = tmp;}m = total / n;c[0] = 0;for (int i = 1; i < n; ++i) {c[i] = c[i - 1] + a[i] - m;}nth_element(c + 0, c + (n / 2), c + n);long long x1 = c[n/2];long long ans = 0;for (int i = 0; i < n; ++i)ans += abs(x1 - c[i]);printf("%lld\n", ans);}return 0;}#endif

原创粉丝点击