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;
}

posted @ 2021-08-11 20:18  Daneii  阅读(25)  评论(0编辑  收藏  举报