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.
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 题意:有一个公司,每个月或盈利或亏损,盈利都为s,亏损都为d,已每五个月(1-5月,2-6月,3-7月 etc.)中每个月的盈或亏加起来均小于0
求该公司能否盈利,如果能,输出盈利最大值,不能则输出"Deficit"
题解:如果五个月中有x个月赢,(5-x)个月亏,则应该满足约束x*s<d*(5-x)
期望求出盈利最大值,所以x要尽量大
然后就是求使每五个月中都有(5-x)个月亏所需要月份的最小值
自然可以手模,然而易错,所以可以打个程序暴搜一发
程序如下
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int sta,ans=0x3f3f3f3f,target; void check(int x)
{
int flag=,tmp=,bit=,a[],cnt[],sum=;
memset(a,,sizeof(a));
memset(cnt,,sizeof(cnt));
while(x)
{
if(x%==)
{
tmp++;
a[bit]=;
}
bit++;
x>>=;
}
sum=a[]+a[]+a[]+a[]+a[];
for(int i=;i<=;i++)
{
if(sum<target)
{
flag=;
}
sum-=a[i];
sum+=a[i+];
}
if(sum<target)
{
flag=;
}
if(flag)
{
ans=min(ans,tmp);
}
} int main()
{
scanf("%d",&target);
for(int sta=;sta<=(<<)-;sta++)
{
check(sta);
}
printf("%d\n",ans);
}
接着,只需要根据上述结论打模拟即可.
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int ans,tmp,d,s; int main()
{
int x=;
while(scanf("%d%d",&s,&d)==)
{
for(int i=; i>=; i--)
{
if(s*i<d*(-i))
{
x=i;
break;
}
}
x=-x;
if(x==)
{
ans=s*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=-d*;
}
if(ans>=)
{
printf("%d\n",ans);
}
else
{
puts("Deficit");
}
}
} //0 0
//1 2
//2 4
//3 6
//4 9
//5 12