POJ 2586 贪心

来源:互联网 发布:八爪盒子 知乎 编辑:程序博客网 时间:2024/05/01 01:24

Y2K Accounting Bug
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11867 Accepted: 6001

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

此题题意很费解。 。看了半天明白是说每五个月检查一次(1-5,2-6 。。8-12共八次),每次这五个月的收入总和都是亏的,每个月要么亏d元,要么赚s元。问这一年可能最大的盈利额是多少。如果是负的就输出deficit。
有次可知,应该每五个的亏损应该设置为最小。假设有一个五月亏损不是最小,那可知设置为最小的情况不会比这中不是最小的情况差。
直观上感觉是贪心。即把亏的月数尽可能放在每五个月的最后面。证明一下:如果有一个状态亏的月数不是放在五个月的最后面,那有可能在亏损后的五个月比放在最后面的后面五个月能达到的范围小(假设接下来的五个月开始都是赚的,再放亏的月份)。因此,亏损月数放在最后的操作能更快的覆盖所有的12个月。所以,至少这种放法不会比其他方法得到的和差。然后分类讨论即可。

#include <iostream>#include <algorithm>#include <cstdio>using namespace std;int main(){    double s,d;    while(scanf("%lf%lf",&s,&d)!=EOF){        double k = s/d;        int tol;        if(3.0/2.0<k&&k<=4.0) tol = s*3-d*9;        else if(2.0/3.0<k&&k<=3.0/2.0) tol = s*6-d*6;        else if(1.0/4.0<k&&k<=2.0/3.0) tol = s*8-d*4;        else if(0.0<=k&&k<=1.0/4.0) tol = s*10-d*2;        else tol = -1;        if(tol<0) printf("Deficit\n");        else printf("%d\n",tol);    }    return 0;}
0 0
原创粉丝点击