Submission #72534531


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 3e5 + 10;

ull a[N], pre_sum[N]; // 前缀和数组,pre_sum[i]是前i个杯子的体积和(降序)

int main() {
    int n, k;
    ull x;
    scanf("%d%d%llu", &n, &k, &x);
    for (int i = 1; i <= n; ++i) {
        scanf("%llu", &a[i]); // 用ull存储,避免输入溢出
    }
    
    // 降序排序
    sort(a + 1, a + n + 1, greater<ull>());
    
    // 预处理前缀和
    pre_sum[0] = 0;
    for (int i = 1; i <= n; ++i) {
        pre_sum[i] = pre_sum[i - 1] + a[i];
    }
    
    // 二分查找最小的m
    int l = 1, r = n, ans = -1;
    while (l <= r) {
        int m = (l + r) / 2;
        // 计算t:选中m个杯子时,最坏情况下能拿到的清酒杯子数
        int t = max(0, m + k - n);
        if (t == 0) {
            // 最坏情况拿不到清酒,和为0,不满足
            l = m + 1;
            continue;
        }
        // 前m个中最后t个的和 = pre_sum[m] - pre_sum[m - t]
        ull sum = pre_sum[m] - pre_sum[m - t];
        if (sum >= x) {
            ans = m;
            r = m - 1; // 找更小的m
        } else {
            l = m + 1;
        }
    }
    
    printf("%d\n", ans);
    return 0;
}

Submission Info

Submission Time
Task C - Sake or Water
User Queryme
Language C++23 (GCC 15.2.0)
Score 300
Code Size 1311 Byte
Status AC
Exec Time 36 ms
Memory 8588 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 33
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3852 KiB
example_01.txt AC 1 ms 3716 KiB
example_02.txt AC 1 ms 3864 KiB
hand_00.txt AC 22 ms 8528 KiB
hand_01.txt AC 21 ms 8388 KiB
hand_02.txt AC 34 ms 8336 KiB
hand_03.txt AC 35 ms 8404 KiB
hand_04.txt AC 35 ms 8472 KiB
hand_05.txt AC 34 ms 8540 KiB
hand_06.txt AC 34 ms 8516 KiB
hand_07.txt AC 1 ms 3736 KiB
hand_08.txt AC 1 ms 3856 KiB
hand_09.txt AC 1 ms 3708 KiB
hand_10.txt AC 1 ms 3652 KiB
hand_11.txt AC 22 ms 8404 KiB
random_00.txt AC 36 ms 8336 KiB
random_01.txt AC 36 ms 8532 KiB
random_02.txt AC 36 ms 8388 KiB
random_03.txt AC 36 ms 8508 KiB
random_04.txt AC 36 ms 8416 KiB
random_05.txt AC 36 ms 8376 KiB
random_06.txt AC 36 ms 8472 KiB
random_07.txt AC 36 ms 8532 KiB
random_08.txt AC 36 ms 8388 KiB
random_09.txt AC 36 ms 8516 KiB
random_10.txt AC 36 ms 8436 KiB
random_11.txt AC 36 ms 8444 KiB
random_12.txt AC 36 ms 8588 KiB
random_13.txt AC 35 ms 8496 KiB
random_14.txt AC 36 ms 8584 KiB
random_15.txt AC 35 ms 8404 KiB
random_16.txt AC 36 ms 8444 KiB
random_17.txt AC 36 ms 8536 KiB