Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Question link: http://poj.org/problem? Id = 2586
----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
Language:DefaultY2K accounting bug
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:9979 |
|
Accepted:4970 |
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 entiresyear 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 237375 743200000 8496942500000 8000000 Sample output 11628300612Deficit |
It is easier to understand the meaning of a question.
The general idea is that a company has 12 months, a fixed surplus S, or a fixed loss D.
However, you cannot remember the monthly surplus or the monthly loss. You can only remember the algebra and the loss for five consecutive months (<0 is a loss ), in a year, there are only eight consecutive five months, respectively 1 ~ 5, 2 ~ 6 ,..., 8 ~ 12
Can I make a profit all year round? If possible, the output may be the maximum profit amount; otherwise, the output will be "deficit ".
Idea: greedy thinking. In every five consecutive months, when the sum of the five months of operation is guaranteed to be a loss, the number of months of loss must be selected as far as possible, the number of months for profit should be selected as far as possible.
There are five cases:
The five conditions can be summarized as the criteria for determining S:
0 <= S <1/4d: at least 1 month for each 5 consecutive months D
1/4D <= S <2/3D: at least 2 months for each 5 consecutive months D
2/3D <= S <3/2D: at least 3 months for each 5 consecutive months D
3/2D <= S <4D each 5 consecutive months, at least 4 months D
4d <= s all-year-round monthly losses
The Code is as follows:
#include <iostream>using namespace std;int main(){int s, d, flag;while(cin>>s>>d){int sum = 0;flag = 0;if(0<=s && s<(1.0/4)*d){sum = 10*s - 2*d;}else if((1.0/4)*d<=s && s<(2.0/3)*d){sum = 8*s-4*d;}else if((2.0/3)*d<=s && s<(3.0/2)*d){sum = 6*s-6*d;}else if((3.0/2)*d<=s && s<4*d){sum = 3*s-9*d;}else if(4*d <= s){flag = 1;}if( sum < 0)flag = 1;if(flag)cout<<"Deficit"<<endl;elsecout<<sum<<endl;}return 0;}