
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct SkiBoard {
int weight;
int length;
};
bool compareLength(const SkiBoard &a, const SkiBoard &b) {
return a.length > b.length;
}
int main() {
int n, G;
cin >> n >> G;
vector<SkiBoard> boards(n);
for (int i = 0; i < n; ++i) {
cin >> boards[i].weight >> boards[i].length;
}
// 按滑雪板长度从大到小排序
sort(boards.begin(), boards.end(), compareLength);
int total_length = 0;
int i = 0;
while (i < n) {
int current_max_L = boards[i].length;
int current_sum = boards[i].weight;
total_length += 2 * current_max_L;
i++;
// 尽可能多地加入后续滑雪板
while (i < n && current_sum + boards[i].weight <= G) {
current_sum += boards[i].weight;
i++;
}
}
cout << total_length << endl;
return 0;
}
1605

被折叠的 条评论
为什么被折叠?



