POJ 2586 贪心 最易理解的解释

探讨一家公司在遭遇Y2K问题后丢失关键年度报告数据的解决方案,通过编写程序确定公司全年是否出现赤字,以及可能的最大盈余。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意:一个公司 五个月会有一次账目报表 要求没次报帐的时候都是亏损(即这五个月总盈利为负) 每月盈利或者亏损都是

固定的金额 s 和 d;让我们求出一年总盈利的最大值;

刚开始的时候无法理解题意 每次报账都是亏损 最后怎么能盈利 然后仔细理解了一下题意 说的是五个月报一次 不是每隔五个

月报一次 所以报帐表的时间段应该是1-5 2-6 3-7 4-8 5-9 6-10 7-11 8-12 一共八个时间段

这里可以用一个贪心的思想 每次报账必须是亏损 而每个月亏损和盈利又是固定值 所以可以利用报账时间的重合部分 使每次

报账的亏损相同且最小 可以通过使每次报账区间月份亏损月份的数目相同来实现

可以分成

SSSSDSSSSDSS

SSSDDSSSDDSS

SSDDDSSDDDSS

SDDDDSDDDDSD

这四种情况 然后找出四种情况中盈利最大的那种情况就好了

#include<iostream>
#include<cstdio>
#include<cstring>

int main()
{
    int  s,d;
    while(~scanf("%d %d",&s,&d))
    {
        int ans = -1;
        if(s*4 < d)
            if(s*10 - 2*d>ans)
            ans = s*10 - 2*d;
      if(s*3 < 2*d)
            if(s*8 - 4*d>ans)
            ans = s*8 - 4*d;
        if(s*2 < 3*d)
            if(s*6 - 6*d>ans)
            ans = s*6 - 6*d;
            if(s <4* d)
            if(s*3 - 9*d>ans)
            ans = s*3 - 9*d;
            if(s*4 < d)
            if(s*10 - 2*d>ans)
            ans = s*10 - 2*d;
            if(ans < 0)
                printf("Deficit\n");
            else
                printf("%d\n",ans);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值