每日一题(四):贪心算法

1.

Problem Description

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.

Input

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.

Output

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.

Sample Input

5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1

Sample Output

13.333 31.500

#include<stdio.h>
#include<algorithm>
using namespace std;
struct goods {
	double j;  //物品总重
	double f;  //物品总价值
	double s;  //物品性价比
	bool operator < (const goods &A) const {
		return s > A.s;
	}
}buf[1000];

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].f);
			buf[i].s = buf[i].j / buf[i].f;  //计算性价比

		}
		sort(buf, buf + n);
		int idx = 0;   //当前货物下标
		double ans = 0;//累加所能得到的总重量
		while (m > 0 && idx < n) {
			//物品剩余(idx<n)且钱剩余(m>0)时继续循环
			if (m > buf[idx].f) {  //若能买下全部物品
				ans += buf[idx].f;
				m -= buf[idx].f;
			}
			else {                 //若只能买下部分该物品
				ans += buf[idx].j*m / buf[idx].f;
				m = 0;
			}
			idx++;
		}
		printf("%.3lf\n", ans);
	}
	return 0;
}

Problem Description

“今年暑假不AC?”

“是的。”

“那你干什么呢?”

“看世界杯呀,笨蛋!”

“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。

作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

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

Sample Output

5

#include<iostream>
#include<algorithm>

using namespace std;
struct program {
	int starttime;
	int endtime;
	bool operator < (const program &A) const {
		return endtime<A.endtime;
	}
}buf[100];
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(buf, buf + n);
	int currenttime = 0, ans = 0;
	for (int i = 0; i < n; i++) {
		if (currenttime <= buf[i].starttime) {
			currenttime = buf[i].endtime;
			ans++;
		}
	}
	printf("%d\n", ans);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lyc44813418/article/details/86548612