码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ 2586] Y2K Accounting Bug (贪心)

时间:2015-01-22 23:09:20      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://poj.org/problem?id=2586

题目大意:(真难读懂啊)给你两个数,s,d,意思是MS公司每个月可能赚钱,也可能赔钱,如果赚钱的话,就是赚s元,如果赔钱的话,就是陪d元。现在告诉你只要是连续的5个月,就亏钱(亏多少不知道),问你MS公司全年能够赚多少钱,如果赚不了钱的话,就输出“Deficit”

 

贪心:我们说,输出的最终结果是f(x,y)=x*s-y*d,也就是说现在要求得x,y使得f(x,y)具有最大值。

当s,d,x+y给定时,x越大,则赚的钱越多。

连续的5个月一共有8个:

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12

我们希望亏损的月份更加集中,也就是说一个月能够尽量的贯穿更多的连续5个月。

因此应该是从右上角往左下角走,贴着最右边。

 

这样就能够出现不多的几种情况了。。(切勿想当然的想着对称)

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char imp[] = "Deficit";
 5 
 6 int main(){
 7     int s,b;
 8 
 9     while(EOF!=scanf("%d%d",&s,&b)){
10         if( 4*s<b ){
11             if( 10*s-2*b<0 ) puts(imp);
12             else printf("%d\n",10*s-2*b);
13         } else if( 3*s<2*b ){
14             if( 8*s-4*b<0 ) puts(imp);
15             else printf("%d\n",8*s-4*b);
16         } else if( 2*s<3*b ){
17             if( 6*s-6*b<0 ) puts(imp);
18             else printf("%d\n",6*s-6*b);
19         } else if( s<4*b ){
20             if( 3*s-9*b<0 ) puts(imp);
21             else printf("%d\n",3*s-9*b);
22         } else puts(imp);
23     }
24 
25     return 0;
26 }

 

[POJ 2586] Y2K Accounting Bug (贪心)

标签:

原文地址:http://www.cnblogs.com/llkpersonal/p/4242880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!