1. 程式人生 > 其它 >POJ-2586 Y2K Accounting Bug(貪心)

POJ-2586 Y2K Accounting Bug(貪心)

題目傳送門

題意

  一家公司,如果盈利,則金額為\(s\),如果虧損,則金額為\(d\),該公司每連續五個月公佈一次總財報,已知公佈的八次財報全為虧損,問是否盈利,如果盈利則輸出最多能盈利的金額,否則輸出\(Deficit\)

思路

  這題主要的困難在於理解題目的題意,連續五個月說明是在\(1、2345,23456,34567,45678,56789,\dots\)公佈財報,也就是說任意連續五個月,虧損的金額均大於盈利的金額,要想使得盈利最大,就只要讓虧損的月數最少。如果畫一下圖,就會發現,前十個月的情況是一樣的,重要的是\(11、12\)月,包含的時候也是需要和前\(5\)個月一樣的虧損月數。

參考程式碼

點此展開
//Author:Daneii
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

#define in(x) scanf("%d",&x)
#define lin(x) scanf("%lld",&x)
#define din(x) scanf("%lf",&x)

typedef long long ll;
typedef long double ld;
typedef pair<int,int> PII;

const int N=100010;

int main()
{
    #ifdef LOCAL
    freopen("D:/VSCodeField/C_Practice/.input/a.in", "r", stdin);
    freopen("D:/VSCodeField/C_Practice/.input/a.out", "w", stdout);
    #endif

    ll s,d;
    while(cin>>s>>d)
    {
        ll ned=1;
        while(s*(5-ned)>=d*ned)
        {
            ned++;
        }
        if(ned==5)//全部都要是虧損
        {
            cout<<"Deficit"<<endl;
            continue;
        }

        ll left=2*(5-ned)+min(5-ned,2ll);
        ll res=s*left-d*2ll*ned-(ned>3?d:0);
        //cout<<res<<endl;
        if(res>0)
            cout<<res<<endl;
        else
            cout<<"Deficit"<<endl;
    }

    return 0;
}