部分背包问题

#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); }
计算机小白记录学习过程,喜欢就点个推荐和关注吧O(∩_∩)O哈哈~

浙公网安备 33010602011771号