QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394983#1362. Bad PackingMahmoudBassemWA 25ms20768kbC++202.7kb2024-04-20 23:52:312024-04-20 23:52:31

Judging History

你现在查看的是最新测评结果

  • [2024-04-20 23:52:31]
  • 评测
  • 测评结果:WA
  • 用时:25ms
  • 内存:20768kb
  • [2024-04-20 23:52:31]
  • 提交

answer

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// less_equal here isn't related to order_of_key, but to how elements are stored
// swapping here works like this : ors1.swap(ors2);
// NOTE : default order is ascending with no repetition.
template<typename K, typename V, typename Comp = less<K>>
using order_statistic_map = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;

template<typename K, typename Comp = less<K>>
using order_statistic_set = order_statistic_map<K, null_type, Comp>;

#define int long long
#define ld long double
#define el '\n'
#define fi first
#define se second

#define Nine_seconds ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 1e17;
const int N = 1e3 + 7;
const int C = 1e5 + 7;
int n, c;
map<int, int> cnt;

struct state {
    int pos;
    int rem;
};

bool relax(int &a, int b) {
    if (a < b) return false;
    a = b;

    return true;
}

void run_case(int tc) {
    cin >> n >> c;
    vector<int> w(n);
    for (int i = 0; i < n; i++) {
        cin >> w[i];
        cnt[w[i]] += 1;
    }
sort(w.begin(),w.end());

    int dp[n + 1][c + 1];
    state par[n + 1][c + 1];

    for (int i = 0; i < n + 1; i++)
        for (int j = 0; j < c + 1; j++)
            dp[i][j] = INF, par[i][j] = {-1, -1};

    dp[0][c] = 0;

    for (int i = 0; i < n; i++) {
        for (int rem = c; rem >= 0; rem--) {   // rem
            if(relax(dp[i + 1][rem - w[i]], dp[i][rem] + w[i])) { // take i
                par[i + 1][rem - w[i]] = {i, rem};
            }
            if(relax(dp[i + 1][rem], dp[i][rem])) { // leave i
                par[i + 1][rem] = {i, rem};
            }
        }
    }

    int ans = INF;
    for (int rem = 0; rem <= c; rem++) {
        state cur = {n, rem};
        while(~cur.pos) {
            int wt = par[cur.pos][cur.rem].rem - cur.rem;
            //if (rem == 2) cout << cur.pos << ' ' << cur.rem << ' ' << dp[cur.pos][cur.rem] << ' ' << wt << el;
            cnt[wt] -= 1;
            cur = par[cur.pos][cur.rem];
        }

        bool bad = false;
        for (auto [f, s]: cnt) {
            if (s > 0 && f <= rem) {
                bad = true;
                break;
            }
        }

        if (!bad)
            relax(ans, dp[n][rem]);

        for (int i = 0; i < n; i++)
            cnt[w[i]] = 0;

        for (int i = 0; i < n; i++)
            cnt[w[i]] += 1;
    }

    cout << ans << el;
}


int32_t main() {
//    Nine_seconds        /*Turn Off for Interactive Problems*/

    int _t = 1;
    //cin >> _t;
    for (int i = 1; i <= _t; i++) {
        run_case(i);
    }

}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

4 10
9
6
5
7

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

10 25
1
1
1
2
2
3
3
4
2
1

output:

20

result:

ok single line: '20'

Test #3:

score: -100
Wrong Answer
time: 25ms
memory: 20768kb

input:

77 9383
203
6771
1608
6509
3213
3597
3416
3011
2241
740
5564
3113
360
3229
5819
5589
5210
4519
5270
6067
10
9147
4171
920
8325
263
8097
3400
9214
3927
8804
4805
8388
1211
523
3799
1124
8573
7491
5527
8026
8529
2510
6430
6171
1405
4820
7662
2449
7264
1419
6320
7272
3327
7042
1517
8326
881
2199
4664
9...

output:

8654

result:

wrong answer 1st lines differ - expected: '8240', found: '8654'