L2-003 月饼 (25 分)

L2-003 月饼 (25 分)

测试点2

注意月饼的库存数是正数不是正整数

Code

#include 
#include 
#include 

using namespace std;

const int N = 1010;

struct cake
{
    double sell;
    int num;
    bool operator< (const cake& a)const
    {
        return sell > a.sell;
    }
}a[N];

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].num;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].sell;
        a[i].sell /= a[i].num;
    }
    double ans = 0;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
    {
        if (m == 0) break;
        if (m >= a[i].num)
        {
            ans += a[i].num * a[i].sell;
            m -= a[i].num;
        }
        else
        {
            ans += m * a[i].sell;
            m = 0;
        }
    }
    cout << fixed << setprecision(2) << ans << endl;
    return 0;
}

你可能感兴趣的:(天梯赛,贪心算法,算法,c++)