团体程序设计天梯赛 L2-003 月饼 (25分)

题目链接:

L2-003 月饼

思路:

按单价高的排序就行

代码:

#include<bits/stdc++.h>

using namespace std;

typedef pair<double, int> P;
const int maxn = 1005;
int n;
double w[maxn], p[maxn], tot; 

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	scanf("%d %lf", &n, &tot);
	for(int i = 0; i < n; i++) scanf("%lf", w + i);
	for(int i = 0; i < n; i++) scanf("%lf", p + i);
	vector<P> ans;
	for(int i = 0; i < n; i++) ans.push_back(P(p[i] / w[i], i));
	sort(ans.begin(), ans.end(), greater<P>());
	double res = 0;
	for(P & now : ans){
		int no = now.second;
		if(tot >= w[no]) tot -= w[no], res += p[no];
		else{ res += p[no] / w[no] * tot; break; }
	}
	printf("%.2f", res);
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308686.html