题面
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.
题意
给两个数 s 和 d,s表示每月的盈利s元,d表示每月亏损d元。已知连续五月的亏损都是相同的(必是亏损),求一年最大的盈利。
分析
我们可以假设1~5月分别盈利或亏损 a,b,c,d,e元,因为sum(1~5)=sum(2~6) 根据容斥可得第六月盈利或亏损a元,同理可得剩下月份的亏损和盈利。因为每月只有两种选择:盈利s元或亏损d元,因此我们可以用二进制枚举a,b,c,d,e,当a+b+c+d+e < 0 时,取2*(a+b+c+d+e)+a+b的最大值
CODE
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define inf 0x3f3f3f3f #define N 1005 #define LL long long LL s,d; int main(){ while(scanf("%lld%lld",&s,&d)!=EOF){ LL ans=-1; for(int i=0;i<(1<<5);++i){ LL now=0; for(int j=0;j<5;++j){ if(i&(1<<j)) now+=s; else now-=d; } if(now>=0) continue; now*=2; now+=(i&1?s:-d); now+=(i&2?s:-d); ans=max(ans,now); } if(ans>-1) printf("%d\n",ans); else puts("Deficit"); } return 0; }View Code