Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
136252 彭士宝 纪念品分组 C++ Accepted 10 MS 388 KB 745 2025-11-09 22:43:00

Tests(11/11):


Code:

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int w, n; cin >> w >> n; vector<int> prices(n); for (int i = 0; i < n; ++i) { cin >> prices[i]; } // 对价格进行排序 sort(prices.begin(), prices.end()); int count = 0; // 分组数 int left = 0, right = n - 1; // 双指针 while (left <= right) { if (prices[left] + prices[right] <= w) { // 如果两个纪念品可以分为一组 left++; } right--; // 无论是否分组,都要移动右指针 count++; // 分组数加1 } cout << count << endl; // 输出最少的分组数目 return 0; }