Y2K Accounting Bug
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.
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
题意:题意一直没搞懂,理解题意其实很简单,就是贪心。
说的是一个公司一年中每个月要么盈利,要么亏损,而且每月盈利和每月亏损的金额是不变的,然后公司每年只有8次报账(1~5月的,2~6月的…….)现在知道每次报账都是亏损,现知道盈利s和亏损的金额d,问这一年有没有可能盈利,盈利的最大金额是多少,如果没法盈利就输出Deficit。
分析:当知道了题意其实很简单了,首先每连续的5个月都是亏损的,所以要想盈利最大,就首先得盈利的月数尽可能多,而最多的月份刚好满足0
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int s,d;
int main ()
{
while (scanf ("%d%d",&s,&d)!=EOF){
int cnt=0;//初始化亏损月数
long long ans=0;
for (int i=1;i<=4;i++){
long long tmp=d*i-s*(5-i);
if (tmp>0&&tmp<s){
cnt=i;
break;
}
}
if (cnt==0){//当没有符合的月数,说明没法盈利,输出Deficit
printf ("Deficit\n");
continue;
}
if (cnt<=3) {//当cnt<=3时,说明剩下的两个月都是盈利的
ans=s*(2*(5-cnt)+2)-d*2*cnt;//总共有两个5月分,每个5月中有cnt个亏损,5-cnt个盈利
}
else if (cnt>3){//大于3说明最后剩下的两个月一个盈利一个亏损
ans=s*(2*(5-cnt)+1)-d*(2*cnt+1);
}
if (ans<=0){//输出结果
printf ("Deficit\n");
}
else printf ("%lld\n",ans);
}
return 0;
}