// 纪念品分组 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格之和不能超过一个给定的整数。为了保证在尽量短的时间内发完所有纪念品,乐乐希望分组的数目最少。
#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;
}
全部评论
相关推荐
查看28道真题和解析 点赞 评论 收藏
分享
11-18 22:06
门头沟学院 golang
牛客28967172...:毕业工作,考研,考公是完全不同的方向。
99%的人拼尽全力也只能把一个做好(能做好都已经是佼佼者了,比如进进大厂,考985或者考公)
如果你确定要考研可以不用学任何就业技术框架,也不用实习经验,刷题背知识点就行,但注意必须考92院校起步,因为这个年代双非硕毕业后完全不如双非本(互联网行业),可以说双非硕在互联网就业完全是负收益 点赞 评论 收藏
分享
