zoukankan      html  css  js  c++  java
  • cf 教育场 45Commentary Boxes

    A. Commentary Boxes
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Berland Football Cup starts really soon! Commentators from all over the world come to the event.

    Organizers have already built nn commentary boxes. mm regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.

    If nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.

    Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.

    What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm)?

    Input

    The only line contains four integer numbers nnmmaa and bb (1n,m10121≤n,m≤10121a,b1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.

    Output

    Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm). It is allowed that the final number of the boxes is equal to 00.

    Examples
    input
    Copy
    9 7 3 8
    
    output
    Copy
    15
    
    input
    Copy
    2 7 3 7
    
    output
    Copy
    14
    
    input
    Copy
    30 6 17 19
    
    output
    Copy
    0
    
    Note

    In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.

    In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles for the each of them.

    In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 55 boxes.

    题意: 有 n 个箱子,m 只队,增加箱子每个花费为 a ,销毁箱子每个花费为b。求使 m 只队得到的箱子数相等的最少花费。

    思路: 简单贪心,分别找第一个比 n 大的 m 的倍数和第一个比 n 小的 m 的倍数,求一下花费。 

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int main(){
        ll n,m,a,b;
        cin>>n>>m>>a>>b;
        ll ans=n%m*b;
        if(ans) ans=min(ans,(m-n%m)*a);
        else ans=0;
        cout<<ans<<endl;
        return 0;
    }

  • 相关阅读:
    C#网络编程TCP通信实例程序简单设计
    C#网络编程TCP通信实例程序简单设计
    2329: 密码破解【数组】
    纸牌游戏小猫钓鱼
    认识栈
    认识队列
    2754: C++习题快速排序
    3047: 快速排序算法
    Problem A: C语言习题 链表建立,插入,删除,输出
    Problem C: 动态规划基础题目之数字三角形
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490310.html
Copyright © 2011-2022 走看看