// 纪念品分组 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格之和不能超过一个给定的整数。为了保证在尽量短的时间内发完所有纪念品,乐乐希望分组的数目最少。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int w, n;
std::cin >> w;
std::cin >> n;
std::vector<int> prices(n);
for (int i = 0; i < n; ++i) {
std::cin >> prices[i];
}
std::sort(prices.begin(), prices.end());
int left = 0, right = n - 1;
int groups = 0;
while (left <= right) {
if (prices[left] + prices[right] <= w) {
++left;
--right;
} else {
--right;
}
++groups;
}
std::cout << groups << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int w, n;
std::cin >> w;
std::cin >> n;
std::vector<int> prices(n);
for (int i = 0; i < n; ++i) {
std::cin >> prices[i];
}
std::sort(prices.begin(), prices.end());
int left = 0, right = n - 1;
int groups = 0;
while (left <= right) {
if (prices[left] + prices[right] <= w) {
++left;
--right;
} else {
--right;
}
++groups;
}
std::cout << groups << std::endl;
return 0;
}
全部评论
相关推荐
点赞 评论 收藏
分享
