poj 2586 Y2K Accounting Bug

Posted -citywall123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2586 Y2K Accounting Bug相关的知识,希望对你有一定的参考价值。

Y2K Accounting Bug

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18534   Accepted: 9346

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

题意:有一家公司,每个月不是亏损d就是盈利s,每个月发布一次盈利报表(包含近5个月的盈亏情况),一年一共发布8次,每次发布报表的营业总量都是亏损的,现在问这家公司全年的最高盈利是多少。
第一次:1~5
第二次:2~6
第三次:3~7
第四次:4~8
第五次:5~9
第六次:6~10
第七次:7~11
第八次:8~12


解法:贪心,要求年收入最大值也就是要收入为负的月份最少,且每个收入为负的月份都要最大化利用,即包括在数量最多的五个月内,首先先把所有月份的收入初始化成正的,然后五个月五个月的遍历,如果收入和是正的就将该月收入改成负的,重要的是进行每五个月的遍历的时候一定要逆序,也就是说优先把后面的月份置为负值,这样可以最大化的利用负值。

 

#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
int a[15];
int main()

    int s,d,sum;
    while(cin>>s>>d)
    
        for(int i=1;i<=12;i++)//初始化每月都是盈余
            a[i]=s;
        for(int i=1;i<=8;i++)//一年发布8次
        
            sum=0;
            for(int j=0;j<5;j++)//每次包含五个月,且总和为亏损
                sum=sum+a[i+j];
            for(int k=i+4;sum>=0;k--)
            
                if(a[k]>=0)
                
                    a[k]=-1*d;
                    sum=sum-s-d;
                
                else
                    continue;
            
        
        sum=0;//mmp,记得置零
        for(int i=1;i<=12;i++)
            sum=sum+a[i];
        if(sum>=0)//等于0也算是盈余
            cout<<sum<<endl;
        else
            cout<<"Deficit"<<endl;
    
    return 0;

 


以上是关于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贪心刷题计划