Algorithm-Renewed
This commit is contained in:
46
Algorithm/Greedy/P2240 【深基12.例1】部分背包问题.cpp
Normal file
46
Algorithm/Greedy/P2240 【深基12.例1】部分背包问题.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
struct cmp {
|
||||
bool operator()(const tuple<double, double, double>& a,
|
||||
const tuple<double, double, double>& b) const {
|
||||
return get<2>(a) > get<2>(b); // <20><><EFBFBD>Լ۱Ƚ<DBB1><C8BD><EFBFBD>
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
double t;
|
||||
cin >> n >> t;
|
||||
|
||||
vector<tuple<double, double, double>> arr(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
double a, b;
|
||||
cin >> a >> b;
|
||||
arr[i] = make_tuple(a, b, b / a); // (<28><><EFBFBD><EFBFBD>, <20><>ֵ, <20><>λ<EFBFBD><CEBB>ֵ)
|
||||
}
|
||||
|
||||
sort(arr.begin(), arr.end(), cmp());
|
||||
|
||||
double value = 0;
|
||||
int p = 0;
|
||||
|
||||
while (t > 0 && p < n) {
|
||||
if (t >= get<0>(arr[p])) {
|
||||
value += get<1>(arr[p]); // ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ
|
||||
t -= get<0>(arr[p]); // <20>ȼ<EFBFBD><C8BC><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
||||
p++;
|
||||
} else {
|
||||
value += t * get<2>(arr[p]); // ȡһ<C8A1><D2BB><EFBFBD><EFBFBD>
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << fixed << setprecision(2) << value << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user