贪心算法 贪心是用来解决一类最优化问题,一般就是问最多,最少,最大值,最小值,最短,最长,只要这个题考最。。。。或者问怎么才能使。。。。。我们就可以考虑贪心算法。
1. 简单贪心 求解一类最优化问题的方法,它总是考虑在当前状态下局部最优(或较优)的策略,来使全局的结果达到最优(或较优)。而要获得最优结果,则要求中间的每步策略都是最优的,因此在严谨的使用贪心法求解最优问题,需要对采取的策略进行证明。证明的一般思路是使用反正法及数学归纳法。
下面两个使用贪心的小例子: 1. 求最大收益 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。
输入格式: 每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出格式: 对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。
输入样例:
输出样例:
代码实现: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <cstdio> #include <algorithm> using namespace std ;struct Goods { double store; double sell; double price; }goods[1010 ]; bool cmp (Goods a,Goods b) { return a.price > b.price; } int main () { int n; double d,total = 0 ; scanf ("%d%lf" ,&n,&d); for (int i = 0 ; i < n; i++){ scanf ("%lf" ,&goods[i].store); } for (int i = 0 ; i < n; i++){ scanf ("%lf" ,&goods[i].sell); goods[i].price = goods[i].sell / goods[i].store; } sort(goods,goods + n,cmp); for (int j = 0 ; j < n; j++){ if (d >= goods[j].store){ total = total + goods[j].sell; d = d - goods[j].store; }else { total = total + d * goods[j].price; break ; } } printf ("%.2f\n" ,total); return 0 ; }
2. 求最小组成的数 给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。
现给定数字,请编写程序输出能够组成的最小的数。
输入格式: 输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 9 的个数。整数间用一个空格分隔。10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字。
输出格式: 在一行中输出能够组成的最小的数。
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <cstdio> int main () { int count[10 ]; for (int i = 0 ; i<10 ; i++){ scanf ("%d" ,&count[i]); } for (int i = 1 ; i < 10 ; i++){ if (count[i] > 0 ){ printf ("%d" ,i); count[i]--; break ; } } for (int i = 0 ; i<10 ; i++){ for (int j = 0 ; j<count[i]; j++){ printf ("%d" ,i); } } return 0 ; }
2. 区间贪心 区间不相交问题:给出N个开区间(x, y),从中选择尽可能多的开区间,使得这些开区间两两没有交集。
看到区间比大小,我们第一个应该想起这个题,应该就是这个题的本质,根据端点去判断,这就是区间题的突破口。
图a, 区间包含 ,应该选择I1,这样的话就可以给其他区间腾出很多的空闲位置;
图b,区间重叠 ,总是选择左端点最大的区间,即I1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <cstdio> #include <algorithm> using namespace std ;struct Inteval { int x,y; }I[110 ]; bool cmpLeft (Inteval a,Inteval b) { if (a.x != b.x){ return a.x > b.x; }else { return a.y < b.y; } } bool cmpRight (Inteval a,Inteval b) { if (a.y != b.y){ return a.y < b.y; }else { return a.x > b.x; } } int main () { int n; while (scanf ("%d" ,&n), n != 0 ){ for (int i = 0 ; i < n; i++){ scanf ("%d%d" , &I[i].x, &I[i].y); } sort(I, I + n, cmpLeft); int ans = 1 , lastX = I[0 ].x; for (int i = 1 ; i < n; i++){ if (I[i].y <= lastX){ lastX = I[i].x; ans ++; } } printf ("%s\n" ,"==========================" ); printf ("%d\n" ,ans); sort(I, I + n, cmpRight); int ansr = 1 , lastRX = I[0 ].y; for (int i = 1 ; i < n; i++){ if (I[i].x <= lastRX){ lastRX = I[i].y; ansr ++; } } printf ("%d\n" ,ansr); } return 0 ; }
欢迎访问 chenyawei 的博客, 若有问题或者有好的建议欢迎留言,笔者看到之后会及时回复。 评论点赞需要github账号登录,如果没有账号的话请点击 github 注册, 谢谢 !
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !