题解 P6746 【『MdOI R3』Operations】

· · 题解

『MdOI R3』Operations

对着数据范围做题。

首先,当 a=0b=0 时,显然是不用代价的,即代价为 0

a=0 时,显然可以用一次 2 操作,使得 a 乘上一个很大的数字,b 除以一个很大的数字,这样 a 还是 0b 因为有下取整操作因此也会变成 0。当 b=0 时同理可做。代价是 d

a=b 时,显然可以用一次 1 操作,使得两数都减去 a,这样两个数字都变成 0 了,代价是 c。还有一个方法是让 a 乘上一个很大的数字,b 除以一个很大的数字,使得其变为 a\neq 0b=0 的形式,使用上面的方法即可处理,代价是 2 \times d,取最小值即可。

a \neq b 时,其实可以用一次 1 操作,使得 a 变为 0,然后再用 a=0b \neq 0 的方法去做(若 a>b,则让 b 变成 0),代价是 c+d。还有一个方法是让 a 乘上一个很大的数字,b 除以一个很大的数字,使得其变为 a\neq 0b=0 的形式,使用上面的方法即可处理,代价是 2 \times d,取最小值即可。

然后结合这些分析即可 AC 本题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <queue>
#include <vector>

using namespace std;

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}

int main()
{
    int a=read(),b=read(),c=read(),d=read();
    if (a==0 && b==0)
        puts("0");
    else if (a*b==0)
        cout << d << endl;
    else if (a==b)
        cout << min(c,2*d) << endl;
    else
        cout << min(c+d,2*d) << endl;
    return 0;
}