HDU-5933-ArcSoft's Office Rearrangement

来源:互联网 发布:勒布朗詹姆斯最新数据 编辑:程序博客网 时间:2024/05/22 13:07

ACM模版

描述

描述

题解

给定 N 个数要求划分为 K 份,一共有两种操作,一种是将相邻两数合并,一种是将一个数拆开两部分。

很明显的贪心模拟,这场比赛好像比较钟爱贪心模拟,可是这个题好坑,因为题目中约定的数据不可能超过 int,却挂了,必须使用 longlong 才行。

代码

#include <iostream>#include <cstdio>#include <queue>using namespace std;const int MAXN = 1e5 + 10;long long N, K;long long a[MAXN];queue<long long> qi;int main(){    int T;    scanf("%d", &T);    for (int case_ = 1; case_ <= T; case_++)    {        printf("Case #%d: ", case_);        scanf("%lld%lld", &N, &K);        long long sum = 0;        for (int i = 0; i < N; i++)        {            scanf("%lld", a + i);            sum += a[i];        }        if (sum % K)        {            puts("-1");            continue;        }        while (!qi.empty())        {            qi.pop();        }        long long ans = 0, unit = sum / K, t;        for (int i = 0; i < N; i++)        {            if (!qi.empty())            {                t = qi.front();                qi.pop();                a[i] += t;                ans++;            }            if (a[i] > unit)            {                t = a[i] / unit;                ans += t;                t = a[i] % unit;                if (t)                {                    qi.push(t);                }                else                {                    ans--;                }            }            else if (a[i] < unit)            {                qi.push(a[i]);            }        }        printf("%lld\n", ans);    }    return 0;}