部分背包问题

 

#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<double, double> p;
typedef multimap<double, p,greater<double> > per;//multimap/map值按照key值从大到小排序,multimap可重复因此此题用该函数
int main(void)
{
    per mp;
    int N, T;
    cin >> N >> T;
    double a, b,sum = 0;
    while (N--)
    {
        cin >> a >> b;
        mp.insert(make_pair(b / a, make_pair(a,b)));
    }
    for (map<double, p>::iterator it = mp.begin(); it != mp.end() && T != 0; it++)
    {
        if ((*it).second.first < T)
        {
            T -= (*it).second.first;
            sum += (*it).second.second;
        }
        else
        {
            sum += (*it).first * T;
            T = 0;
        }
    }
    printf("%.2lf", sum);
}

 

posted @ 2021-02-10 17:58  loliconsk  阅读(76)  评论(0)    收藏  举报