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.
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 300612Deficit
题目大意:一个公司 五个月会有一次账目报表 要求没次报帐的时候都是亏损(即这五个月总盈利为负) 每月盈利或者亏损都是
固定的金额 s 和 d;让我们求出一年总盈利的最大值;
刚开始的时候无法理解题意 每次报账都是亏损 最后怎么能盈利 然后仔细理解了一下题意 说的是五个月报一次 不是每隔五个
月报一次 所以报帐表的时间段应该是1-5 2-6 3-7 4-8 5-9 6-10 7-11 8-12 一共八个时间段
这里可以用一个贪心的思想 每次报账必须是亏损 而每个月亏损和盈利又是固定值 所以可以利用报账时间的重合部分 使每次
报账的亏损相同且最小 可以通过使每次报账区间月份亏损月份的数目相同来实现
可以分成
SSSSDSSSSDSS
SSSDDSSSDDSS
SSDDDSSDDDSS
SDDDDSDDDDSD这四种情况 然后找出四种情况中盈利最大的那种情况就好了
#include<iostream> #include<cstdio> #include<cstring> int main() { int s,d; while(~scanf("%d %d",&s,&d)) { int ans = -1; if(s*4 < d) if(s*10 - 2*d>ans) ans = s*10 - 2*d; if(s*3 < 2*d) if(s*8 - 4*d>ans) ans = s*8 - 4*d; if(s*2 < 3*d) if(s*6 - 6*d>ans) ans = s*6 - 6*d; if(s <4* d) if(s*3 - 9*d>ans) ans = s*3 - 9*d; if(s*4 < d) if(s*10 - 2*d>ans) ans = s*10 - 2*d; if(ans < 0) printf("Deficit\n"); else printf("%d\n",ans); } return 0; }