贪心 | POJ2586.Y2K Accounting Bug
Link: Y2K Accounting Bug
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
题意
此题对于英语不好的人来说题意比较难读懂
微软公司每个月都会公布财政报告,内容包括盈余s或者亏损d。微软公司公布每连续5个月的报告,一年公布8次(1-5月,2-6月. . . 8-12月)。由于计算机发生Y2K问题,只知道1999年公布的8次报告中都发生了亏损。问1999年一整年可能的盈利的最大数额是多少。
思路
分析: 看第一个例子,5个月中,至多盈利3个月,才能产生亏损。所以5个月的中3个月盈利,2个月亏损可得到最大盈余。所以12个月的序列可以为 s s s d d s s s d d s s 或者 d d s s s d d s s s d d 或者其他形式。容易看出将亏损放在5个月中的后几个月,并且后面5个月完全照抄前五个月即可得到最大盈余的序列。
分析结果:: 由ns - (5-n)d < 0 可得出 n =
⌊
5
d
s
+
d
⌋
\lfloor \frac{5d}{s+d} \rfloor
⌊s+d5d⌋ , 并且一年的最大盈余为 [ns-(5-n)d ]*2 + 后两个月盈亏。
代码
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int s, d;
int n, last2, Surplus;
while( cin >> s >> d )
{
n = (5*d)/(s+d);
if( n >= 2 )
last2 = 2*s;
else if( n == 1 )
last2 = s-d;
else
{
cout << "Deficit" << endl;
continue;
}
Surplus = (n*s-(5-n)*d)*2+last2;
if( Surplus>0 )
cout << Surplus << endl;
else
cout << "Deficit" << endl;
}
}