HDU 5993 ArcSoft's Office Rearrangement 简单模拟

来源:互联网 发布:淘宝老七贸易苹果7 编辑:程序博客网 时间:2024/06/15 01:26

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5933

题意:有一个数列,每次可以合并相邻的两项得到它们的和,或分解相邻两项,两项和为原来项。问最小操作数使得数列的每个数值等于 sum / k.

解题思路:用双端队列进行简单模拟即可。

获得成就:Rank top -2。(摊手)

这里写图片描述

Rank top -2 AC code:

#include<cstdio>#include<deque>using namespace std;typedef long long ll;ll num[100005];int main(){    int t,n;    ll k;    scanf("%d", &t);    deque<ll> d;    for (int m = 1; m <= t; m++)    {        d.clear();        scanf("%d %I64d", &n, &k);        ll sum = 0;        for (int i = 0; i < n; i++)        {            scanf("%I64d", &num[i]);            sum += num[i];            d.push_back(num[i]);        }        if (sum % k != 0)        {            printf("Case #%d: -1\n",m);            continue;        }        ll p = sum / k;        int ans = 0;        while (!d.empty())        {            ll tmp = d.front();            d.pop_front();            if (tmp == p) continue;            else if (tmp > p)            {                if (tmp % p == 0)                    ans += (tmp / p - 1);                else                {                    ans += tmp / p;                    d.push_front(tmp % p);                }            }            else            {                ll tmp2 = d.front();                d.pop_front();                ll tmp3 = tmp2 + tmp;                ans++;                d.push_front(tmp3);            }        }        printf("Case #%d: %d\n",m,ans);    }    return 0;}
0 0
原创粉丝点击