Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
111381 | 汤奕硕 | 纪念品分组 | C++ | Wrong Answer | 0 MS | 284 KB | 931 | 2025-03-01 19:14:30 |
#include <iostream> #include <vector> #include <algorithm> // 用于 sort 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 groupCount = 0; // 分组数量 int i = 0, j = n - 1; // 两个指针,分别指向最小和最大的价格 while (i <= j) { // 如果两个价格之和不超过上限,将它们分到同一组 if (prices[i] + prices[j] <= w) { i++; j--; } else { // 否则,将较大的价格单独分到一组 j--; } groupCount++; } // 输出最少的分组数量 cout << groupCount << endl; return 0; }
------Input------
177 14 47 114 159 109 81 115 8 40 83 157 122 121 41 117
------Answer-----
9
------Your output-----