为啥只给了二十分,测试点全过了
查看原帖
为啥只给了二十分,测试点全过了
1194486
xaiozhangyu41654684楼主2023/12/31 15:42
#include <stdio.h>
#include <stdlib.h>

int cmp(const void* a, const void* b) {
    return *(int*)a - *(int*)b;
}

int computeGroupCount(int w, int n, int* prices) {
    qsort(prices, n, sizeof(int), cmp);

    int left = 0;
    int right = n - 1;
    int count = 0;

    while (left <= right) {
        if (prices[left] + prices[right] <= w) {
            left++;
            right--;
            count++;
        }
        else {
            right--;
            count++;
        }
    }

    return count;
}

int main() {
    int w;
    scanf("%d", &w);

    int n;
    scanf("%d", &n);

    int* prices = (int*)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {
        scanf("%d", &prices[i]);
    }

    int groupCount = computeGroupCount(w, n, prices);

    printf("%d\n", groupCount);

    free(prices);

    return 0;
}
2023/12/31 15:42
加载中...