Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11271 | Accepted: 5672 |
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 300612 Deficit
这里翻译一下题目,原先没看懂题:
题目大意:有家公司每个月有盈余s 和亏损d但不知道具体是那个月 只知道一年中每个连续的五个月都是亏损的。要求求出该公司这一年里最大的盈利的可能金额 若不能盈利就输出Deficit
注意如果s>4d也是要输出Deficit的
我是利用贪心做的,依次检查1-5月,2-6月,。。。。先检查1-5月,将s放在前面,看看最多可以有几个月盈利,在检查2-6月,因为2-5月已经知道,所以看6月如果是s符合条件,那么6月是s,否则是d,依次找出所有月的盈利是s还是亏损d,算出是盈利还是亏损即可。
查网上还有一中简单方法,直接穷举,5个月统计一次,可能出现亏损的情况如下:
1. SSSSD -> SSSSDSSSSDSS
2. SSSDD -> SSSDDSSSDDSS
3. SSDDD -> SSDDDSSDDDSS
4. SDDDD -> SDDDDSDDDDSD
只有这几种情况,依次试一下即可
下面是我写的AC
1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 int s,d; 6 int res[12]; 7 int sum[12]; 8 while(cin>>s>>d){ 9 int tmp=0; 10 for(int i=1;i<6;i++){ 11 if(i*s-(5-i)*d<=0&&(i+1)*s-(5-i-1)*d>0){ 12 tmp=i; 13 break; 14 } 15 } 16 //cout<<tmp<<endl; 17 if(tmp==5){ 18 cout<<"Deficit"<<endl; 19 continue; 20 } 21 for(int i=0;i<tmp;i++){ 22 res[i]=s; 23 } 24 for(int i=tmp;i<5;i++){ 25 res[i]=0-d; 26 } 27 sum[0]=res[0]; 28 //cout<<sum[0]<<endl; 29 for(int i=1;i<5;i++){ 30 sum[i]=sum[i-1]+res[i]; 31 // cout<<sum[i]<<endl; 32 } 33 for(int i=5;i<12;i++){ 34 int tsum=0; 35 tsum=sum[i-1]-sum[i-5]; 36 if(tsum+s<=0){ 37 sum[i]=sum[i-1]+s; 38 }else{ 39 sum[i]=sum[i-1]-d; 40 } 41 //cout<<sum[i]<<endl; 42 } 43 if(sum[11]<=0){ 44 cout<<"Deficit"<<endl; 45 }else{ 46 cout<<sum[11]<<endl; 47 } 48 } 49 return 0; 50 }