zoukankan      html  css  js  c++  java
  • 【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Recently a dog was bought for Polycarp. The dog’s name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.

    Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times.

    Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, …, an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).

    Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times.

    Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, …, bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.

    Input
    The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.

    The second line contains integers a1, a2, …, an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.

    Output
    In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days.

    In the second line print n integers b1, b2, …, bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.

    Examples
    input
    3 5
    2 0 1
    output
    4
    2 3 2
    input
    3 1
    0 0 0
    output
    1
    0 1 0
    input
    4 6
    2 4 3 5
    output
    0
    2 4 3 5

    【题解】

    只要前一天和今天的次数和没有大于等于k,则增加当前天的次数,直至前一天和今天的次数和等于k。
    勇敢贪吧。

    #include <cstdio>
    
    int a[600];
    int n, k;
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        scanf("%d%d", &n, &k);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        a[0] = k; a[n + 1] = k;
        int add = 0;
        for (int i = 1; i <= n; i++)
        {
            int num = a[i - 1] + a[i];
            if (num < k)
            {
                add += (k - num);
                a[i] += (k - num);
            }
        }
        printf("%d
    ", add);
        for (int i = 1; i <= n; i++)
            printf("%d%c", a[i], i == n ? '
    ' : ' ');
        return 0;
    }
  • 相关阅读:
    各种算法时空复杂度
    Python文本处理(1)
    数学之路(3)-机器学习(3)-机器学习算法-欧氏距离(3)
    为什么要选择cdn加速
    数据库中操作XML(openXML)
    HDU 3308 LCIS
    Android有效解决加载大图片时内存溢出的问题
    Pathchirp—有效的带宽估计方法(二)
    php三元运算
    C# MVC 自学笔记—4 添加视图
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632140.html
Copyright © 2011-2022 走看看