贪心算法2

Y2K Accounting Bug
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10316   Accepted: 5136

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


连续五个月要亏损,这是边界条件,总共加起来要是盈利。这样的话,前五个月一定要把亏损的放在后面,这样12个月里,盈利的月就更多了。
eg: 1 2 3 4 5 6 7 8 9 10 11 12
s s s d d s s s d d s s 8s
d d s s s d d s s s d d 6s
这里的贪心就是尽量让12个月盈利最大,这样就要让亏损的月重复使用。
我是直接用枚举。反正只有4种情况,再判断(K*s-M*d)<0
 1 #include<cstdio>
 2 #include<string.h>
 3 using namespace std;
 4 int main()
 5 {
 6    int s,d;
 7    int ok=1;
 8    int x,y,z,e;
 9    while(scanf("%d%d",&s,&d)!=EOF)
10    {
11        x=s*10-2*d;
12        y=8*s-4*d;
13        z=6*s-6*d;
14        e=3*s-9*d;
15        if(x>0&&(4*s-d)<0)
16          printf("%d
",x);
17        else if(y>0&&(3*s-2*d)<0)
18         printf("%d
",y);
19        else if(z>0&&(2*s-3*d)<0)
20         printf("%d
",z);
21        else if(e>0&&(1*s-4*d)<0)
22         printf("%d
",e);
23        else printf("Deficit
");
24    }
25     return 0;
26 }
原文地址:https://www.cnblogs.com/angledamon/p/3869691.html