Y2K Accounting Bug POJ - 2586

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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值