1070. Mooncake (25)解题报告

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
using namespace std;

struct mooncake {
    double inventory, price, perprice;
};

bool comp(mooncake m1, mooncake m2);

int main(void) {
    mooncake arr[1000];
    int i, n;
    double inventory, profit, d;
    scanf("%d %lf", &n, &d);
    for (i = 0; i < n; i++) {
        scanf("%lf", &arr[i].inventory);
    }
    for (i = 0; i < n; i++) {
        scanf("%lf", &arr[i].price);
        arr[i].perprice = arr[i].price / arr[i].inventory;
    }
    sort(arr, arr + n, comp);
    inventory = profit = 0;
    for (i = 0; i < n && inventory < d; i++) {
        if (inventory + arr[i].inventory <= d) {
            inventory += arr[i].inventory;
            profit += arr[i].price;
        }
        else {
            profit += arr[i].perprice * (d - inventory);
            inventory = d;
        }
    }
    printf("%.2lf\n", profit);
    return 0;
}
bool comp(mooncake m1, mooncake m2) {
    return m1.perprice > m2.perprice;
}

你可能感兴趣的:(PAT)