目录
1,题目描述
Sample Input:
3 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45
题目大意
现在囤积的有许多种类的月饼,每类月饼都有一定的售价,市场有一定量的需求,问如何能获得最大利益?(肯定是先卖贵的鸭!)
2,思路
将每种月饼按照获利能力的高低,从大到小排序,挑贵的卖。
3,AC代码
#include<bits/stdc++.h>
using namespace std;
struct node{
float amount, price, pre;
};
bool cmp1(node a, node b){
return a.pre > b.pre;//性价比高
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, D;
cin>>N>>D;
vector<node> data(N);
float amount, price;
for(int i = 0; i < N; i++){
scanf("%f", &amount);
data[i].amount = amount;
}
for(int i = 0; i < N; i++){
scanf("%f", &price);
data[i].price = price;
data[i].pre = data[i].price / data[i].amount;
}
sort(data.begin(), data.end(), cmp1);
float ans = 0;
int index = 0;
while(D > 0 && index < data.size()){
if(data[index].amount < D){
D -= data[index].amount;
ans += data[index].price;
}else{
ans += D * data[index].pre;
D = 0;
}
index++;
}
printf("%.2f", ans);
return 0;
}
4,解题过程
一发入魂。
数据量也不大,题意也比较明显,直接写就完事了。