Codeforces Round #377 (Div. 2) 解题报告

来源:互联网 发布:电脑无法上淘宝网 编辑:程序博客网 时间:2024/06/09 14:30

    • A Buy a Shovel
    • 题意
    • 思路
    • 代码
    • B Cormen The Best Friend Of a Man
    • 题意
    • 思路
    • 代码

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;}
0 0
原创粉丝点击