1. 程式人生 > >POJ2586 -- Y2K Accounting Bug

POJ2586 -- Y2K Accounting Bug

報表 解題思路 mount pst nta nbsp 四個月 條件 water

Y2K Accounting Bug
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16395 Accepted: 8251

Description

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

Sample Output

116
28
300612
Deficit

Source

Waterloo local 2000.01.29 題意: 有一個公司由於某個病毒使公司贏虧數據丟失,但該公司每月的 贏虧是一個定數,要麽一個月贏利s,要麽一月虧d。現在ACM只知道該公司每五個月有一個贏虧報表,而且每次報表贏利情況都為虧。在一年中這樣的報表總共有8次(1到5,2到6,…,8到12),現在要編一個程序確定當贏s和虧d給出,並滿足每張報表為虧的情況下,全年公司最高可贏利多少,若存在,則輸出盈利額,若不存在,輸出"Deficit"。
題意分析: 公司每五個月報一次表,都為虧損,說明12個月中任意連續五個月總計為虧損。 因為讓求全年公司最高可盈利多少,所以在滿足連續五個月總計為虧損的條件下,應該盡可能的盈利。即為貪心算法。 解題思路: 首先考慮第一個月,贏利s,虧d 若s>=4d,即有四個月虧損一個月盈利無法滿足連續五個月總計為虧損的條件,所以必須要連續五個月全都虧損,這樣全年不會有盈利。Deficit s<4d && 2s>=3d,即有三個月虧損兩個月盈利無法滿足連續五個月總計為虧損的條件,連續五個月中,最多有一個月為盈利,所以12個月的狀態應為sddddsddddsd,全年的利潤為3s-9d 2s<3d && 3s>=2d,即有兩個月虧損三個月盈利無法滿足連續五個月總計為虧損的條件,連續五個月中,最多有2個月為盈利,所以12個月的狀態為ssdddssdddss,全年的利潤為6s-6d 3s<2d && 4s>=d,連續五個月中,最多有3個月盈利,所以12個月的狀態為sssddsssddss,全年的利潤為8s-4d 4s<d,即連續5個月中,有四個月盈利一個月虧損,滿足連續五個月總計虧損的條件,最多有4個月盈利,所以12個月狀態為ssssdssssdss,全年的利潤為10s-2d 不能連續5個月盈利。
#include<iostream>
using namespace std;
int main()
{
    int s,d;
    while(cin>>s>>d)
    {
        int total;
        if(s >= 4*d)
        {
            total = -1;
        }else if(2*s>=3*d)
        {
            total = 3*s - 9*d;
        }else if(3*s>=2*d)
        {
            total = 6*s - 6*d;
        }else if(4*s>=d)
        {
            total = 8*s-4*d;
        }else{
            total = 10*s-2*d;
        }

        if(total > 0)
        {
            cout<<total<<endl;
        }else{
            cout<<"Deficit"<<endl;
        }

    }

    return 0;
}

  

POJ2586 -- Y2K Accounting Bug