北大acm2586 贪心算法

北大acm2586 贪心算法

题目如下:
Y2K Accounting Bug
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17884 Accepted: 9051
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

题目大意:本题的意思是一年一共十二个月的报告都有给出,任意连续五个月都是赤字,求最大盈利的问题,有点类似于贪心算法中的背包问题,本题需要分5种情况去讨论 :
(1) s>=4d(无论怎么取都是盈利,不合题意)
(2)s<4d sddddsddddsd (四个月的亏损大于一个月的盈利)
(3)2s<3d ssdddssdddss (三个月的亏损大于两个月的盈利)
(4)3s<2d sssddsssddss (两个月的亏损大于三个月的盈利)
(5)4s<d ssssdssssdss (一个月的亏损大于一个月的盈利)
需要注意的就是代码中判断条件的顺序排比,因为要确保的是盈利最大化,所以把s多的判定条件放在更上面
源码如下:

//首先要知道常识:盈余(s)是挣钱  赤字(d)是亏损
//任意五个月 八次报告都是赤字
#include<iostream>
using namespace std;
//分5种情况去讨论 (1) s>=4d (2)s<4d sddddsdddddsd (3)2s<3d ssdddssdddss (4)3s<2d sssddsssddss (5)4s<d ssssdssssdss

int main()
{
	int s,d;
	while(cin>>s>>d)
	{
		int profit=0;
		if((4*s) < d)//贪心算法 s多的优先判断 为了保证end最大
		{
			profit=10*s-2*d;
		}
		else if((3*s) < (2*d))
		{
			profit=8*s-4*d;
		}
		else if((2*s) < (3*d))
		{
			profit=6*s-6*d;
		}
		else if(s < (4*d))
		{
			profit=3*s-9*d;
		}		
		else
		{
			profit=-1;
		}

		if(profit > 0)
			cout<<profit<<endl;
		else
			cout<<"Deficit"<<endl;			
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/weixin_43626529/article/details/86683788