其他分享
首页 > 其他分享> > 【天梯赛】L2-003 月饼 (25 point(s))*

【天梯赛】L2-003 月饼 (25 point(s))*

作者:互联网

穿越隧道

求每种月饼的每顿价格,对其进行排序。
注意需要double型的数组不论吨数还是售价

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
double k[N];
int n,d;
double ty[N];//double 
double sale[N];//double数组决定了第三个样例 
struct node{
    double k;
    int idx;//元素种类所对应的下标
}kk[N];
bool cmp(node a, node b){
    return a.k >= b.k;
}
int main(){
    cin >> n >> d;
    for(int i = 0; i < n; i++){
        cin >> ty[i];
    }
    for(int i = 0; i < n; i++){
        cin >> sale[i];
    }
    for(int i = 0; i < n; i++){
        kk[i].k = sale[i]*1.0/ty[i];
        kk[i].idx = i;
    }
    sort(kk,kk + n,cmp);
//    for(int i = 0; i < n; i++){
//    	cout << kk[i].k <<" " ;
//    	cout << kk[i].idx <<endl;
//	}
//	cout <<endl<<"----" << endl;
    double ans = 0;
    for(int i = 0; i < n; i++){
        if(ty[kk[i].idx] > d){
            ans += (d*1.0/ty[kk[i].idx]) * sale[kk[i].idx];
            break;
        }
        else{
            ans += sale[kk[i].idx];
            d -= ty[kk[i].idx];
//            cout <<"d = " << d << endl;
        }
    }
    printf("%.2f",ans);
    return 0;
}

标签:25,idx,point,int,double,ty,sale,003,kk
来源: https://blog.csdn.net/qq_45866781/article/details/123637779