POJ - 2586 Y2K Accounting Bug (找规律)

Posted Styx-ferryman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

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=1,tmp=0,bit=0,a[12],cnt[12],sum=0;
    memset(a,0,sizeof(a));
    memset(cnt,0,sizeof(cnt));
    while(x)
    {
        if(x%2==1)
        {
            tmp++;
            a[bit]=1;
        }
        bit++;
        x>>=1;
    }
    sum=a[0]+a[1]+a[2]+a[3]+a[4];
    for(int i=0;i<=6;i++)
    {
        if(sum<target)
        {
            flag=0;
        }
        sum-=a[i];
        sum+=a[i+5];
    }
    if(sum<target)
    {
        flag=0;
    }
    if(flag)
    {
        ans=min(ans,tmp);
    }
}

int main()
{
    scanf("%d",&target);
    for(int sta=0;sta<=(1<<12)-1;sta++)
    {
        check(sta);
    }
    printf("%d\n",ans);
}
View Code
接着,只需要根据上述结论打模拟即可.
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int ans,tmp,d,s;

int main()
{
    int x=0;
    while(scanf("%d%d",&s,&d)==2)
    {
        for(int i=5; i>=0; i--)
        {
            if(s*i<d*(5-i))
            {
                x=i;
                break;
            }
        }
        x=5-x;
        if(x==0)
        {
            ans=s*12;
        }
        if(x==1)
        {
            ans=s*10-d*2;
        }
        if(x==2)
        {
            ans=s*8-d*4;
        }
        if(x==3)
        {
            ans=s*6-d*6;
        }
        if(x==4)
        {
            ans=s*3-d*9;
        }
        if(x==5)
        {
            ans=0-d*12;
        }
        if(ans>=0)
        {
            printf("%d\n",ans);
        }
        else
        {
            puts("Deficit");
        }
    }
}

//0 0
//1 2
//2 4
//3 6
//4 9
//5 12

 

 


以上是关于POJ - 2586 Y2K Accounting Bug (找规律)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2586 Y2K Accounting Bug

poj 2586 Y2K Accounting Bug

POJ2586 -- Y2K Accounting Bug

POJ - 2586 Y2K Accounting Bug (找规律)

POJ 2586 Y2K Accounting Bug(枚举大水题)

poj 2586 Y2K Accounting Bug贪心刷题计划