pat 1020. 月饼

原题链接:

思路:根据单价进行贪心,优先选择单价高的。我想吐槽一点:明明说的正整数,但是库存和总量却有浮点数,坑爹的测试点2,大家注意了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//测试的数据就是坑
typedef pair<double,double> P;
const int maxn=1005;
int n,d;
P cake[maxn];
bool cmp(P p1,P p2){
    return p1.second*p2.first>p1.first*p2.second;
}
void print(){
    for(int i=0;i<n;i++){
        cout<<cake[i].first<<" "<<cake[i].second<<endl;
    }
}
void input(){
    cin>>n>>d;
    for(int i=0;i<n;i++){
        cin>>cake[i].first;
    }
    for(int i=0;i<n;i++){
        cin>>cake[i].second;
    }
    sort(cake,cake+n,cmp);
    //print();
}
void solve(){
    double ans=0;
    /*if(!d){
        cout<<0<<endl;
        return;
    }*/
    for(int i=0;i<n;i++){
        if(d<=cake[i].first){
            ans+=cake[i].second*1.0/cake[i].first*d;
            break;
        }else{
            ans+=cake[i].second;
            d-=cake[i].first;
        }
    }
    printf("%.2f\n",ans);
}
int main(){
    //freopen("in.txt","r",stdin);
    input();
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值