1. 程式人生 > >Codeforces Round #377 (Div. 2) 解題報告

Codeforces Round #377 (Div. 2) 解題報告

A. Buy a Shovel

time limit per test1 second
memory limit per test256 megabytes
input standard input
output standard output

Polycarp urgently needs a shovel! He comes to the shop and chooses an appropriate one. The shovel that Policarp chooses is sold for k burles. Assume that there is an unlimited number of such shovels in the shop.

In his pocket Polycarp has an unlimited number of “10-burle coins” and exactly one coin of r burles (1 ≤ r ≤ 9).

What is the minimum number of shovels Polycarp has to buy so that he can pay for the purchase without any change? It is obvious that he can pay for 10 shovels without any change (by paying the requied amount of 10-burle coins and not using the coin of r burles). But perhaps he can buy fewer shovels and pay without any change. Note that Polycarp should buy at least one shovel.

Input
The single line of input contains two integers k and r (1 ≤ k ≤ 1000, 1 ≤ r ≤ 9) — the price of one shovel and the denomination of the coin in Polycarp’s pocket that is different from “10-burle coins”.

Remember that he has an unlimited number of coins in the denomination of 10, that is, Polycarp has enough money to buy any number of shovels.

Output
Print the required minimum number of shovels Polycarp has to buy so that he can pay for them without any change.

Examples
input
117 3
output
9
input
237 7
output
1
input
15 2
output
2

Note
In the first example Polycarp can buy 9 shovels and pay 9·117 = 1053 burles. Indeed, he can pay this sum by using 10-burle coins and one 3-burle coin. He can’t buy fewer shovels without any change.

In the second example it is enough for Polycarp to buy one shovel.

In the third example Polycarp should buy two shovels and pay 2·15 = 30 burles. It is obvious that he can pay this sum without any change.

題意

Polycarp手頭有很多錢,其中包括若干張十元和一張面值為r元的錢。現在他要買一件單價為k元的物品,求出最少購買幾件該物品,他可以不用找零。

思路

這是一道簡單的數學題。由於Polycarp手上的10元是無數的,我們很容易想到宣告一個整數sum,迴圈將k的個位數與sum相加並對10整除,直到得到與r相等的值即可(其間記錄迴圈次數)。
這裡需要注意幾個小細節,當sum=0時,迴圈也應終止,因為這意味著我們可以只使用10元購買而達到不找零的目的。且當k個位數為5時,答案一定為5.
不過上述判斷可合併為一個if語句,參考程式碼如下。

程式碼

#include<iostream>
using namespace std;
int k,r;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>k>>r){
        int t=k%10,flag=0,num=0;
        while(num!=r){
            num+=t;
            num=num%10;
            flag++;
            if(!num) break;
        }
        cout<<flag<<"\n";
    }
    return 0;
}

B. Cormen — The Best Friend Of a Man

time limit per test1 second
memory limit per test256 megabytes
input standard input
output standard 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

題意

(發現#377好像熱衷於黑一個叫Polycarp的人)Polycarp最近買了一隻狗叫Cormen,他需要每天帶Cormen出去散步。可是這隻狗脾氣古怪,一定要保證連續兩天的散步時間大於等於一個值k,它才會安心。
現在給出Polycarp n天的遛狗計劃,每天給出一個遛狗時長ai。這個計劃可能不會讓Cormen開心,請你求出Polycarp至少需要在這n天內增加多少遛狗時間才能保證小狗不會不開心,並輸出新的遛狗計劃。

思路

這題資料範圍很小,O(n2logn)都是可以接受的,但是我們其實可以線上性時間(即O(n))內解決這個問題。
O(n)其實是貪心。對於本題,迴圈天數i(2in),每天用k-a[i]-a[i-1],如果這個值大於0,則說明這一天遛狗時間需要增加,直接在a[i]和ans上加上這個差值即可。
正確性證明略去,參考程式碼如下。

程式碼

#include<iostream>
using namespace std;
const int N = 505;
int a[N],b[N];
int n,k,ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    b[1]=a[1];
    for(int i=2,delta;i<=n;i++){
        delta=k-a[i]-a[i-1];
        if(delta>0){
            ans+=delta;
            a[i]+=delta;
        }
        b[i]=a[i];
    }
    cout<<ans<<"\n";
    for(int i=1;i<n;i++)
        cout<<b[i]<<' ';
    cout<<b[n]<<"\n";
    return 0;
}