Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
3996 | 52525ioiop | 纪念品分组 | C | Running & Judging | 0 MS | 0 KB | 902 | 2024-07-24 09:29:02 |
#include <stdio.h> #include <stdlib.h> #define MAXN 1000 // 比较函数,用于 qsort 函数排序 int compare(const void *a, const void *b) { return (*(int*)a - *(int*)b); } int main() { int w, n; int prices[MAXN]; // 读取输入 scanf("%d", &w); // 价格之和的上限 scanf("%d", &n); // 纪念品的总件数 // 读取每件纪念品的价格 for (int i = 0; i < n; i++) { scanf("%d", &prices[i]); } // 排序 qsort(prices, n, sizeof(int), compare); int i = 0, j = n - 1; int groups = 0; // 分组 while (i <= j) { // 尝试将最便宜和最贵的两个物品放在同一组 if (prices[i] + prices[j] <= w) { i++; } j--; groups++; } // 输出最少的分组数目 printf("%d\n", groups); return 0; }