pat甲级 1070 Mooncake (25 分) (贪心)

题目链接:传送门

思路:物品是可分割的,于是每次加上单价最高的物品即可。

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn =  1005;

struct node {
	double d;
	double p;
	bool operator < (const node b)const {
		return p / d > b.p / b.d;
	}
}a[maxn];

int main() {
	int n , d;
	cin >> n >> d;
	for(int i = 0 ; i < n ; i++) {
		cin >> a[i].d;
	}
	for(int i = 0 ; i < n ; i++) {
		cin >> a[i].p;
	}
	sort(a , a + n);
	double ans = 0;
	for(int i = 0 ; i < n ; i++) {
		if(a[i].d >= d) {
			ans += d * a[i].p / a[i].d;
			break;
		}
		else {
			ans += a[i].p;
			d -= a[i].d;
		}
	}
	printf("%.2f\n" , ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39475280/article/details/103083764
今日推荐