您的位置:首页 > 其它

PAT 1070. Mooncake (25) 贪心

2018-01-19 15:24 330 查看
/*************************
题意:
给出每块月饼的质量,和这块月饼的价格
问商家怎么切掉并组合,才能组合出一个最贵的月饼
************************/

/***********************
贪心思想
即每次先消耗单位价格最贵的月饼即可。
*************************/
/***********************
笔记:

*********************/
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
using namespace std;
#define M 110000
#define INF 0x7fffffff

struct Mooncake{
double w;
double price;
};
Mooncake mooncake[M];

bool cmp(Mooncake a,Mooncake b){
return a.price/a.w > b.price/b.w;
}

int main(){
int n,i;
double d,w,price;
cin>>n>>d;
for(i=0;i<n;i++){
scanf("%lf", &mooncake[i].w);
}
for(i=0;i<n;i++){
scanf("%lf", &mooncake[i].price);
}
sort(mooncake,mooncake+n,cmp);

double ans = 0;
for(i=0;i<n;i++){
if(mooncake[i].w < d){
d -= mooncake[i].w;
ans += mooncake[i].price;
}
else if(mooncake[i].w >= d){
ans += d * mooncake[i].price / mooncake[i].w;
break;
}
}
printf("%.2lf\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: