您的位置:首页 > 编程语言 > C语言/C++

poj2586--Y2K Accounting Bug

2017-01-19 14:20 351 查看
Y2K一看到这个题目本来以为会是计算与千年虫有关的东西,看完之后发现跟千年虫好像还真没什么事,只是一道入门贪心的时候做的一道很典型的题

题目大意是某公司电脑中了千年虫,造成了数据丢失 不过这个公司在12个月内每月盈亏都为s,亏损都为d,但是哪个月盈利哪个月亏损不知道,只知道每五个月的代数和总是亏损(1-5 2-6 … 8-12),让求全年的最大最大盈利可能,如果不能盈利输出Deficit

下面是原题 后面有详细点的题解

Y2K Accounting Bug

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14356 Accepted: 7225
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


题解:每五个月固定亏算 拿在连续的五个月中必定至少有一个月为亏损才能保证符合题意 而经过仔细思考可以发现利用贪心的思想一次讨论下面的几种情况就好:(十二个月盈亏情况)

赚赚赚赚亏赚赚赚赚亏赚赚(ssssdssssdss):保证每五个月中有一个亏

SSSDDSSSDDSS:保证每五个月中有两个亏

SSDDDSSDDDSS:保证每五个月中有三个亏

SDDDDSDDDDSD:保证每五个月中有四个亏

还有一种全亏的情况但是那种情况一定会输出Deficit 所以直接让结果随便等于一个负数就行,不用特意计算

我记得当初做这道题的时候有人问过我为什么第二种不能SSSDDSSSSDSS 这样盈利会多一点,确实会多,但是在第一种情况每五个月中有一个亏不满足连续五个月亏损的话 那他所说那种情况一定也不成立

AC源代码:
#include <iostream>
using namespace std;

int main()
{
int s,d;
int res;
while(cin>>s && cin>>d)
{
if(d>4*s)res=10*s-2*d;
else if(2*d>3*s)res=8*s-4*d;
else if(3*d>2*s)res=6*(s-d);
else if(4*d>s)res=3*(s-3*d);
else res=-1;
if(res<0)cout<<"Deficit"<<endl;
else cout<<res<<endl;
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息