Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
题意:公司每五个月进行一次财务统计(盈余、亏损),由题意可知公司一年下来的统计都是亏损的,1-5,2-6,3-7,4-8,,,8-12.
题中规定若是本月盈余则所有盈余月都是这个数字,比如输入的s=59,也就是说所有盈余月的盈利数都为59,亏损数237同上。最终要
求的是全年利润,若无则输出Deficit。
方法1:可以用贪心法解决不过由于十二个月数字并不多也完全可以枚举一下,为了达到最大利润肯定是让每五个月中亏损月份尽可能最少
ssssd ssssd ss由这组数据可以看出亏损月份应尽可能放在每五个月的最后,因此就可以一一举例为:
1.ssssd ssssd ss亏损两个月
2.sssdd sssdd ss亏损四个月
3.ssddd ssddd ss亏损六个月
4.sdddd sdddd sd亏损九个月
5.ddddd ddddd dd亏损
1 #include<stdio.h> 2 int main() 3 { 4 int n,m,s; 5 while(~scanf("%d%d",&n,&m)) 6 { 7 if(4*n<m)s=10*n-2*m; 8 else if(3*n<2*m)s=8*n-4*m; 9 else if(2*n<3*m)s=6*n-6*m; 10 else if(n<4*m)s=3*n-9*m; 11 else s=-1; 12 if(s<0)printf("Deficit "); 13 else printf("%d ",s); 14 15 }return 0; 16 }
方法2贪心算法实现:
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int sur,def,month[12]; 6 while(~scanf("%d%d",&sur,&def)) 7 { 8 memset(month,0,sizeof(month)); 9 int counts=0,countd=0; 10 for(int i=0; i<8; i++) 11 { 12 while(1) 13 { 14 countd = month[i]+month[i+1]+month[i+2]+ 15 month[i+3]+month[i+4]; 16 counts = 5-countd; 17 if(counts*sur>=countd*def)//贪心实现每组五个数满足亏损 18 { 19 for(int j=i+4; j>=0; j--) 20 { 21 if(month[j]!=1) 22 { 23 month[j]=1; 24 break; 25 } 26 } 27 } 28 else 29 break; 30 } 31 } 32 countd=0; 33 for(int i=0; i<12; i++) 34 { 35 countd += month[i]; 36 } 37 counts = 12-countd; 38 int ans = counts*sur-countd*def; 39 if(ans>0) 40 printf("%d ",ans); 41 else 42 printf("Deficit "); 43 } 44 return 0; 45 }