Y2K Accounting Bug POJ - 2586
题目链接:https://vjudge.net/problem/POJ-2586
题意:一家公司,每个月不是盈利s 就是亏损d 现在只知道这家公司1至5月,2至6月,…,8至12月都是亏损的但不知道亏了多少。现在给定s和d,问:这一年是盈利还是亏损,若盈利最多能盈利多少?
看懂题了就很简单。^ ^
思路:
枚举12个月份的收入情况,(00…00~11…11 ,0代表亏损,1代表盈利)复杂度为
2
12
2^{12}
212
对每一个情况判断是否满足连续5个月亏损。满足的话贪心保留最大值。
具体见代码:
#include <iostream>
#include<cmath>
using namespace std;
int b[13];
const int inf=1e9+7;
bool is_5_deficit(){
int i,j;
for(i=1;i<=8;i++){
int res=0;
for(j=i;j<=i+4;j++){
res+=b[j];
}
if(res>0)return false;
}
return true;
}
int gersplus(){
int res=0;
int i;
for(i=1;i<=12;i++)res+=b[i];
return res;
}
int main()
{
int s,d;
while(cin>>s>>d){
int ans=-inf;
//0000~1111
int i;
for(i=0;i<=pow(2,12)-1;i++){
//get bit
int tem=12;
int ii=i;
while(1){
b[tem]=ii&1;
ii>>=1;
tem--;
if(tem==0)break;
}
// for(int j=1;j<=12;j++){
// cout<<b[j]<<" ";
// }
// cout<<endl;
//get_b
for(int j=1;j<=12;j++){
if(b[j]==0){//de
b[j]=-d;
}
else{
b[j]=s;
}
}
if(!is_5_deficit()){
continue;
}
int per=gersplus();
//cout<<"per="<<per<<endl;
ans=max(ans,per);
}
if(ans<0){
cout<<"Deficit\n";
}
else cout<<ans<<endl;
}
return 0;
}