Y2K Accounting Bug POJ - 2586(貪心)

Y2K Accounting Bug POJ - 2586(貪心)

題目

參考博客:https://blog.csdn.net/lyy289065406/article/details/6642603#commentBox

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite.

Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.

input

Input is a sequence of lines, each containing two positive integers s and d.

output

For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output Deficit if it is impossible.

sample

input

59 237
375 743
200000 849694
2500000 8000000

output

116
28
300612
Deficit

在做poj貪心專題那幾道題的時候,由於這個題意生澀難懂,然後交了2發wa了之後,放棄了。今天網上看了看題解,理解題意後不難,就是一貪心思想。

題目大致意思:
已知一個公司在某一年中,【每個月要麼固定盈利s、要麼固定虧損d】。
但是具體哪個月盈利、那個月虧損卻不得而知。
不過可以肯定的是,這一年中,【任意的連續5個月盈虧和必定是虧損(< 0)】。
問這年是否存在盈利的可能,若可能盈利,【最大的盈利額】是多少?

分以下大致5種情況:
當 s < 4d (但2s >= 3d) 時 (即1個月的盈利剛好小於4個月的虧損),
此時只需保證每連續5個月至少有4個月是虧損即可,
根據貪心邏輯,全年盈虧月份爲:sddddsddddsd
即盈利月份爲 1、6、11
虧損月份爲 [2-5]、[7-10]、12
全年最大利潤爲: 3s - 9d

當 2s < 3d (但3s >= 2d)時 (即2個月的盈利剛好小於3個月的虧損),
此時只需保證每連續5個月至少有3個月是虧損即可,
根據貪心邏輯,全年盈虧月份爲:ssdddssdddss
即盈利月份爲 [1-2]、[6-7]、[11-12]
虧損月份爲 [3-5]、[8-10]
全年最大利潤爲: 6s - 6d

當 3s < 2d (但4s >= d)時 (即3個月的盈利剛好小於2個月的虧損),
此時只需保證每連續5個月至少有2個月是虧損即可,
根據貪心邏輯,全年盈虧月份爲:sssddsssddss
即盈利月份爲 [1-3]、[6-8]、[11-12]
虧損月份爲 [4-5]、[9-10]
全年最大利潤爲: 8s - 4d

當 4s < d 時 (即4個月的盈利剛好小於1個月的虧損),
此時只需保證每連續5個月至少有1個月是虧損即可,
根據貪心邏輯,全年盈虧月份爲:ssssdssssdss
即盈利月份爲 [1,4]、[6-9]、[11-12]
虧損月份爲 5、10
全年最大利潤爲: 10s - 2d

這就有以下代碼:

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
using namespace std;
#define FOPI freopen("input.in", "r", stdin)
#define DOPI freopen("output.out", "w", stdout)
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 1e3+100;
int lowbit(int x){return x&(-x);}
int main()
{
    ios::sync_with_stdio(0);
    int s,d;
    while(cin>>s>>d)
    {
        ll sum=0;
        if(s>=4*d)//dddddddddddd
        {
            sum-=d*12;
        }
        else if(s<4*d&&2*s>=3*d)//sddddsddddsd
        {
            sum+=(3*s-9*d);
        }
        else if((2*s<3*d)&&(3*s>=2*d))//ssdddssdddss
        {
            sum+=(6*s-6*d);
        }
        else if((3*s<2*d)&&(4*s>=d))//sssddsssddss
            sum+=(8*s-4*d);
        else if(4*s<d)//ssssdssssdss
            sum+=(10*s-d*2);
        if(sum<0)
            cout<<"Deficit"<<endl;
        else
            cout<<sum<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章