1. 程式人生 > 其它 >POJ 2586 Y2K Accounting Bug

POJ 2586 Y2K Accounting Bug

題面

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