欢迎来到 xht37 的博客

CF1119B Alyona and a Narrow Fridge

题目地址:CF1119B Alyona and a Narrow Fridge

\(O(n^2)\) 暴力枚举+贪心

从小到大枚举答案

假设枚举到 \(i\) ,将 \(a_1\)\(a_i\) 排序,从大到小贪心的放。

如果高度超过给定的高度,答案为 \(i-1\)

如果一直到 \(n\) 都没超过,答案为 \(n\)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 1e3 + 6;
ll n, m, a[N], b[N];

int main() {
    cin >> n >> m;
    for (ll i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (ll i = 1; i <= n; i++) {
        for (ll j = 1; j <= i; j++)
            b[i] = a[i];
        sort(b + 1, b + i + 1);
        ll now = 0;
        for (ll j = i; j > 0; j -= 2) {
            now += b[j];
        }
        if (now > m) {
            cout << i - 1 << endl;
            return 0;
        }
    }
    cout << n << endl;
    return 0;
}
posted @ 2019-04-11 00:42 xht37 阅读(...) 评论(...) 编辑 收藏