Run ID:136252

提交时间:2025-11-09 22:43:00

#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; }