首页 > 代码库 > oj---pat--b1020

oj---pat--b1020

两种情况:把月饼全部买完(库存刚好满足需求,或库存小于需求);买了部分月饼。

#include<cstdio>
#include<algorithm>
using namespace std;
struct Mcake{
    int price;
    int vol;
    bool operator < (const Mcake &A) const{
    if((price*1.0/vol)==(A.price*1.0/A.vol)) return vol>A.vol;
    else return price*1.0/vol>A.price*1.0/A.vol;
    }
}mcake[1005];
int main(){
    int n,th;
    scanf("%d %d",&n,&th);
    for(int i=0;i<n;i++) scanf("%d",&mcake[i].vol);
    for(int i=0;i<n;i++) scanf("%d",&mcake[i].price);
    sort(mcake,mcake+n);
    int cnt=0;
    double res=0;
    int i=0;
    while((cnt+mcake[i].vol)<=th&&i<n){
        cnt+=mcake[i].vol;
        res+=mcake[i].price;
        i++;
    }
    
    if(i<=n-1&&cnt<th)    res+=(1.0*(th-cnt))/mcake[i].vol*mcake[i].price;
    
    printf("%.2f",res);
    return 0;
}

 

oj---pat--b1020