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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章