跳到正文

题解

题解:P16747 [GKS 2020 #A] Allocation

rex2880约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月7日
更新
2026年6月20日
篇幅
约 2 分钟
题目
P16747

版本

  1. v1当前

题意

求在 AA 里最大的和不超过 BB 元的数的个数。

思路

先将 AA 排序(尽可能买价格便宜的),再计算和不超过 BB 元的数的个数。

AC 代码

C++
#include<bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T;
    cin >> T;
    for (int c = 1; c <= T; c++) {
        int n, b, a[100000], ans = 0, cnt = 0;
        cin >> n >> b;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a, a + n);
        for (int i = 0; i < n; i++) {
            if (a[i] + cnt <= b) {
                ans++;
                cnt += a[i];
            } else {
                break;
            }
        }
        cout << "Case #" << c << ": " << ans << '\n';
    }
    return 0;
}
原文
存档评论1 条抓取于