贪心算法就是在解决问题时,总是做出当前看来最好的选择,不从整体最优上进行考虑,做出的是局部意义上的最优解。这样看来好像贪心算法有很大的风险,只考虑当前的情况是否能得到总体上的最优解,实际上,贪心算法要求当前做出的每一步都是独立的,不能对以后的状态产生影响,只与当前状态有关。
先看一个题目:
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
输入:
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
输出:
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
样例输入:
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出:
13.333
31.500
题目很好理解,一只老鼠有 M 元,它想尽可能多的与猫交换东西,东西彼此只有数量和价钱的差别,老鼠不挑,有 N 件东西,第 i 个东西有 j 个,第 i 个东西的总价值是 m 元, j / m 是每元能买多少件第 i 个东西, 求最多可以买多少东西。贪心的策略就是买每元能买最多个那个第 i 件东西,第 i 个买完后,再买次之的。直到把 M 元花完。按照 j / m进行排序,然后再加到 M 元,算出总共买到的东西即可。
代码要注意的是,输入 -1 , -1 后跳出循环,这一步该怎么写,j 和 m, M 都应该是 double 型的,注意如何表达到达了M元,代码为:
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
struct E {
double j;
double m;
double c;
}buf[1000];
bool cmp(E a, E b)
{
return a.c > b.c;
}
int main()
{
double M;
int N;
while (scanf("%lf%d", &M,&N) != EOF)
{
if (M == -1 && N == -1)
break;
for (int i = 0; i < N; i++)
{
scanf("%lf%lf", &buf[i].j, &buf[i].m);
//j和m都必须是double型,否则仅仅是int 型到double型的强制转换,不会有0.333的尾数
buf[i].c = buf[i].j / buf[i].m;
}
sort(buf, buf + N, cmp);
double sum = 0;
double res = 0;
for (int i = 0; ; i++)
{
sum += buf[i].m;
if (sum > M)
{
sum -= buf[i].m;
res += (M - sum) * buf[i].c;
break;
}
else
res += buf[i].j;
}
printf("%.3lf\n", res);
}
return 0;
}
运行结果为:
5 3
7 2
4 3
5 2
13.333
20 3
25 18
24 15
15 10
31.500
-1 -1
请按任意键继续. . .
-1,-1 被输入后,不仅跳出了循环,还自动完成了换行操作。
再来看一个例子,今年暑假不AC:
题目描述:
“ 今 年 暑 假 不 AC ? ”“ 是 的 。 ”“ 那 你 干 什 么 呢 ? ”“ 看 世 界 杯 呀 , 笨蛋!”“@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多 ACMer也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记
关心国家大事)、非常 6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
输入:
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是 n 行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第 i 个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0 表示输入结束,不做处理。
输出:
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
样例输入:
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
样例输出:
5
题目很好理解,要求求出最多可以观看多少个不冲突的电视节目,按照贪心算法的思想,经过试验最早开始的节目顺序,时间最短的节目顺序都不满足,只有最早结束的不冲突的节目顺序满足要求,按照最早结束的思想可以写出代码,这里证明一下是按照最早结束的时间顺序,假设顺序中的第一个A不是最早结束时间的B,那么A可以被B取代掉,而不影响最多观看的节目数量,即A,B放在第一位是等价的,因为在A顺序后的节目必在B后,所以是不影响的。
代码为:
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
struct program {
int startTime;
int endTime;
}buf[1000];
//因为不能直接对结构体的stime进行排列,所以要自定义函数,升序排列
bool cmp(program a, program b)
{
return a.endTime < b.endTime;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
if (n == 0)
break;
for (int i = 0; i < n; i++)
scanf("%d %d", &buf[i].startTime, &buf[i].endTime);
//说明sort可以对不同类型的a[]进行排列,只有cmp函数的区别
sort(buf, buf + n, cmp);
int currentTime = 0;
int cnt = 0;
for (int i = 0; i < n; i++)
{
//=不能省略
if (currentTime <= buf[i].startTime)
{
//这句话非常重要,必须能够不冲突,才能加入观看序列
currentTime = buf[i].endTime;
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}
运行结果:
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
5