Y2K Accounting Bug
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11253 | Accepted: 5665 |
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
点击打开链接
题意为连续5个月后公司总是亏本,且相同的可以分为8组,
1-5
2-6
3-7
4-8
5-9
6-10
7-11
8-12
这八次都亏本问全年是否有最大利润s在前且数量越多获得的利润越大的可能性就越高;
如ssssdssssdss 共12个无论从哪里开头满足后面还有4个字母的话,结果亏顺的钱数均相同。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #define L long long using namespace std; L f(L s,L d) { if(4*s-d<0)//分别枚举各种情况 return 10*s-2*d;//算出此种情况的年利润 else if(3*s-2*d<0) return 8*s-4*d; else if(2*s-3*d<0) return 6*s-6*d; else if(s-4*d<0) return 3*s-9*d; return -1;//如果不符合题意跳出 } int main() { L s,d; ios::sync_with_stdio(false); while(cin>>s>>d) { if(f(s,d)<0) cout<<"Deficit"<<endl;//用puts()会WA 不知why else cout<<f(s,d)<<endl; } return 0; }
再次回味:
首先要保证在连续5个月,亏并且全年的获利最大,所以月份的s,d排列时尽量让亏本在后边因为,它还会对后面的月份造成影响使得下一个月亏顺,但是对整体的影响较小,所以尽量让s在前那么就有如下方式排列。
ssssd
sssdd
ssddd
sdddd
ddddd(做题时忘了考虑- -)
找到一种既可以跳出。
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<string> #include<queue> #include<map> #include<cmath> #define L1 long long #define L2 __int64 #define inf 0x3f3f3f3f #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; //const int m1=1001000; //const int m2=1010; //int head[m1],vex[m1],arr[m1],cnt; int main() { int n,m,i,j,k; while(~scanf("%d%d",&n,&m)) { int ans=0; for(i=0;i<4;i++) { ans+=n; } for(i=4;i<5;i++) { ans-=m; } if(ans<0) { ans*=2; ans+=(2*n); if(ans>=0) { printf("%d\n",ans);continue; } else { printf("Deficit\n");continue; } } ans=0; for(i=0;i<3;i++) { ans+=n; } for(i=3;i<5;i++) { ans-=m; } if(ans<0) { ans*=2; ans+=(2*n); if(ans>=0) { printf("%d\n",ans);continue; } else { printf("Deficit\n");continue; } } ans=0; for(i=0;i<2;i++) { ans+=n; } for(i=2;i<5;i++) { ans-=m; } if(ans<0) { ans*=2; ans+=(2*n); if(ans>=0) { printf("%d\n",ans);continue; } else { printf("Deficit\n");continue; } } ans=0; for(i=0;i<1;i++) { ans+=n; } for(i=1;i<5;i++) { ans-=m; } if(ans<0) { ans*=2; ans+=n;ans-=m; if(ans>=0) { printf("%d\n",ans);continue; } else { printf("Deficit\n");continue; } } else { printf("Deficit\n");continue; } } return 0; }