提出 #69511973


ソースコード 拡げる

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <map>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());

    // Group songs by their B_j requirement and sort by C_j descending
    vector<vector<long long>> songs_by_b(n + 1);
    for (int i = 0; i < m; ++i) {
        int b;
        long long c;
        cin >> b >> c;
        if (b <= n) {
            songs_by_b[b].push_back(c);
        }
    }

    for (int b = 0; b <= n; ++b) {
        sort(songs_by_b[b].rbegin(), songs_by_b[b].rend());
    }

    // Precompute R(p) values
    vector<long long> r(m + 1, 0);
    for (int p = 1; p <= m; ++p) {
        long long sum_a_lt_p = 0;
        long long count_a_ge_p = 0;
        for (int val : a) {
            if (val >= p) {
                count_a_ge_p++;
            } else {
                sum_a_lt_p += val;
            }
        }
        r[p] = (long long)p * count_a_ge_p + sum_a_lt_p;
    }

    // dp[p] is a map from sum_of_b to max_excitement
    vector<map<int, long long>> dp(m + 1);
    dp[0][0] = 0; // Base case: 0 songs, 0 requirement sum, 0 excitement

    // Iterate B values from high to low
    for (int b = n; b >= 0; --b) {
        if (songs_by_b[b].empty()) {
            continue;
        }

        vector<map<int, long long>> next_dp = dp;
        long long current_c_sum = 0;

        // Consider taking k songs of requirement b
        for (int k = 1; k <= songs_by_b[b].size(); ++k) {
            current_c_sum += songs_by_b[b][k - 1];
            
            // Combine with existing states (p0 songs with B > b)
            for (int p0 = 0; p0 <= m - k; ++p0) {
                if (dp[p0].empty()) continue;

                for (auto const& [s0, val0] : dp[p0]) {
                    // Validity check for adding k songs of requirement b
                    // to a state of p0 songs with requirements > b.
                    // This is s0 + i*b <= R(p0+i) for i=1...k
                    // Due to concavity, we only need to check endpoints i=1 and i=k.
                    if (s0 + (long long)b > r[p0 + 1]) {
                        // The condition fails for k=1, so it will fail for any larger k too.
                        // We can break from the s0 loop and go to the next p0.
                        // However, a simple continue is also correct.
                        continue;
                    }
                    if (s0 + (long long)k * b > r[p0 + k]) {
                        continue;
                    }

                    int p = p0 + k;
                    int s = s0 + k * b;
                    
                    // Update DP table for the new state
                    if (next_dp[p].find(s) == next_dp[p].end() || next_dp[p][s] < val0 + current_c_sum) {
                        next_dp[p][s] = val0 + current_c_sum;
                    }
                }
            }
        }
        dp = next_dp;
    }

    long long max_excitement = 0;
    for (int p = 0; p <= m; ++p) {
        for (auto const& [s, val] : dp[p]) {
            max_excitement = max(max_excitement, val);
        }
    }

    cout << max_excitement << endl;

    return 0;
}

提出情報

提出日時
問題 G - Set list
ユーザ SegMan123
言語 C++ 17 (gcc 12.2)
得点 625
コード長 3460 Byte
結果 AC
実行時間 752 ms
メモリ 24324 KiB

コンパイルエラー

Main.cpp: In function ‘int main()’:
Main.cpp:66:27: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   66 |         for (int k = 1; k <= songs_by_b[b].size(); ++k) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 625 / 625
結果
AC × 2
AC × 71
セット名 テストケース
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.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, 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, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, random_43.txt, random_44.txt, random_45.txt, random_46.txt, random_47.txt, random_48.txt, random_49.txt, random_50.txt, random_51.txt, random_52.txt, random_53.txt, random_54.txt, random_55.txt, random_56.txt, random_57.txt, random_58.txt, random_59.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 3528 KiB
example_01.txt AC 1 ms 3460 KiB
hand_00.txt AC 1 ms 3444 KiB
hand_01.txt AC 1 ms 3512 KiB
hand_02.txt AC 1 ms 3492 KiB
hand_03.txt AC 1 ms 3464 KiB
hand_04.txt AC 1 ms 3468 KiB
hand_05.txt AC 752 ms 24324 KiB
hand_06.txt AC 1 ms 3440 KiB
hand_07.txt AC 1 ms 3544 KiB
hand_08.txt AC 1 ms 3516 KiB
random_00.txt AC 516 ms 19712 KiB
random_01.txt AC 630 ms 21744 KiB
random_02.txt AC 582 ms 21224 KiB
random_03.txt AC 573 ms 20944 KiB
random_04.txt AC 581 ms 20164 KiB
random_05.txt AC 208 ms 9888 KiB
random_06.txt AC 217 ms 10808 KiB
random_07.txt AC 221 ms 10480 KiB
random_08.txt AC 252 ms 12440 KiB
random_09.txt AC 205 ms 10280 KiB
random_10.txt AC 129 ms 8192 KiB
random_11.txt AC 133 ms 8460 KiB
random_12.txt AC 351 ms 14288 KiB
random_13.txt AC 176 ms 10040 KiB
random_14.txt AC 322 ms 14004 KiB
random_15.txt AC 281 ms 12116 KiB
random_16.txt AC 195 ms 9636 KiB
random_17.txt AC 171 ms 9228 KiB
random_18.txt AC 187 ms 9828 KiB
random_19.txt AC 131 ms 8436 KiB
random_20.txt AC 295 ms 12944 KiB
random_21.txt AC 228 ms 10564 KiB
random_22.txt AC 285 ms 12772 KiB
random_23.txt AC 197 ms 9880 KiB
random_24.txt AC 283 ms 12924 KiB
random_25.txt AC 126 ms 8180 KiB
random_26.txt AC 292 ms 12060 KiB
random_27.txt AC 139 ms 8740 KiB
random_28.txt AC 179 ms 9984 KiB
random_29.txt AC 358 ms 14444 KiB
random_30.txt AC 110 ms 7372 KiB
random_31.txt AC 432 ms 16156 KiB
random_32.txt AC 173 ms 10128 KiB
random_33.txt AC 289 ms 13140 KiB
random_34.txt AC 378 ms 14824 KiB
random_35.txt AC 283 ms 11632 KiB
random_36.txt AC 347 ms 14244 KiB
random_37.txt AC 292 ms 13596 KiB
random_38.txt AC 235 ms 10680 KiB
random_39.txt AC 406 ms 15336 KiB
random_40.txt AC 280 ms 12540 KiB
random_41.txt AC 133 ms 8140 KiB
random_42.txt AC 252 ms 11756 KiB
random_43.txt AC 258 ms 12532 KiB
random_44.txt AC 334 ms 12208 KiB
random_45.txt AC 267 ms 11556 KiB
random_46.txt AC 281 ms 11976 KiB
random_47.txt AC 285 ms 12240 KiB
random_48.txt AC 208 ms 10424 KiB
random_49.txt AC 370 ms 12968 KiB
random_50.txt AC 23 ms 5128 KiB
random_51.txt AC 28 ms 5588 KiB
random_52.txt AC 26 ms 5336 KiB
random_53.txt AC 20 ms 5164 KiB
random_54.txt AC 28 ms 5644 KiB
random_55.txt AC 19 ms 4360 KiB
random_56.txt AC 22 ms 4524 KiB
random_57.txt AC 26 ms 4656 KiB
random_58.txt AC 15 ms 4196 KiB
random_59.txt AC 30 ms 4788 KiB