QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#114874#6194. Knapsack Problemstd_absWA 1271ms3572kbC++204.9kb2023-06-23 20:15:282023-06-23 20:15:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-23 20:15:29]
  • 评测
  • 测评结果:WA
  • 用时:1271ms
  • 内存:3572kb
  • [2023-06-23 20:15:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 998244353, N = 100005;

struct Simplex {
    using T = long double;
    static const int N = 16, M = 8;
    const T eps = 1e-8;
    int n, m;
    int Left[M], Down[N];
    T a[M][N], b[M], c[N], v, sol[N];
    bool eq(T a, T b) {return fabs(a - b) < eps;}
    bool ls(T a, T b) {return a < b && !eq(a, b);}
    void init(int _n, int _m) {
        n = _n, m = _m, v = 0;
        for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
            a[i][j] = 0;
        }
        for (int i = 0; i < m; ++i) {
            b[i] = 0;
        }
        for (int i = 0; i < n; ++i) {
            c[i] = sol[i] = 0;
        }
    }
    void pivot(int x, int y) {
        swap(Left[x], Down[y]);
        T k = a[x][y]; a[x][y] = 1;
        vector <int> nz;
        for (int i = 0; i < n; ++i) {
            a[x][i] /= k;
            if (!eq(a[x][i], 0)) {
                nz.pb(i);
            }
        }
        b[x] /= k;
        for (int i = 0; i < m; ++i) {
            if (i == x || eq(a[i][y], 0)) {
                continue;
            }
            k = a[i][y], a[i][y] = 0;
            b[i] -= k * b[x];
            for (int j : nz) {
                a[i][j] -= k * a[x][j];
            }
        }
        if (eq(c[y], 0)) {
            return;
        }
        k = c[y], c[y] = 0, v += k * b[x];
        for (int i : nz) {
            c[i] -= k * a[x][i];
        }
    }
    int solve() {
        for (int i = 0; i < n; ++i) {
            Down[i] = i; 
        }
        for (int i = 0; i < m; ++i) {
            Left[i] = n + i;
        }
        while (1) {
            int x = -1, y = -1;
            for (int i = 0; i < m; ++i) if (ls(b[i], 0) && (x == -1 || b[i] < b[x])) {
                x = i;
            }
            if (x == -1) {
                break;
            }
            for (int i = 0; i < n; ++i) if (ls(a[x][i], 0) && (y == -1 || a[x][i] < a[x][y])) {
                y = i;
            }
            if (y == -1) return 1;
            pivot(x, y);
        }
        while (1) {
            int x = -1, y = -1;
            for (int i = 0; i < n; ++i) if (ls(0, c[i]) && (y == -1 || c[i] > c[y])) {
                y = i;
            }
            if (y == -1) {
                break;
            }
            for (int i = 0; i < m; ++i) if (ls(0, a[i][y]) && (x == -1 || b[i] / a[i][y] < b[x] / a[x][y])) {
                x = i;
            }
            if (x == -1) {
                return 2;
            }
            pivot(x, y);
        }
        for (int i = 0; i < m; ++i) {
            if (Left[i] < n) {
                sol[Left[i]] = b[i];
            }
        }
        return 0;
    }
} solver;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int k;
        cin >> k;
        int n = (1 << k) - 1;
        solver.init(n, k * 2);
        vector <int> c(n);
        for (int i = 0; i < n; ++i) {
            cin >> c[i];
            solver.c[i] = c[i]; 
        }
        vector <int> a(k);
        for (int i = 0; i < k; ++i) {
            cin >> a[i];
            for (int j = 0; j < n; ++j) {
                solver.a[i * 2][j] = ((j + 1) >> i & 1 ? 1 : 0);
                solver.a[i * 2 + 1][j] = ((j + 1) >> i & 1 ? -1 : 0);
                solver.b[i * 2] = a[i];
                solver.b[i * 2 + 1] = a[i];
            }
        }
        int x = solver.solve();
        assert(x == 0);
        vector <ll> res(n);
        for (int i = 0; i < n; ++i) {
            res[i] = solver.sol[i];
        }
        vector <ll> cur(n - k, -1);
        ll best = 0;
        while (true) {
            int now = 0;
            vector <ll> nxt = res;
            for (int i = 0; i < n; ++i) {
                if (i == 0 || i == 1 || i == 3 || i == 7) {
                    continue;
                }
                nxt[i] += cur[now], now++;
            }
            for (int i = 0; i < k; ++i) {
                int j = (1 << i) - 1;
                ll tot = 0;
                for (int x = 0; x < n; ++x) if ((x + 1) >> i & 1 && x != j) {
                    tot += nxt[x];
                }
                nxt[j] = a[i] - tot;
            }
            if (*min_element(all(nxt)) >= 0) {
                ll ans = 0;
                for (int i = 0; i < n; ++i) {
                    ans += c[i] * nxt[i];
                }
                best = max(best, ans);
            }
            int pt = n - k - 1;
            while (~pt && cur[pt] == 1) {
                cur[pt] = -1, --pt;
            }
            if (pt == -1) {
                break;
            }
            cur[pt]++;
        }
        cout << best << '\n';
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 11ms
memory: 3544kb

input:

3
2
1 2 4
4 5
3
3226252 19270256 2430652 1187613 12496062 10286165 17494834
24 85 34
4
901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555
529321239 218214127 92942310 207467810

output:

18
1949753378
7832404777567179

result:

ok 3 number(s): "18 1949753378 7832404777567179"

Test #2:

score: 0
Accepted
time: 1271ms
memory: 3528kb

input:

100
4
4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493
200477197 258791439 590664017 982949628
4
5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...

output:

16156444320083421
24565535429631234
29418802996321901
30685727712782520
21026118053726857
27272339796311010
28831028447377015
34577176834491128
9058894962996375
21525085254465501
6657186892229297
16750628911275484
11217846865301187
12674726744043780
33873234230125448
3890237279573366
319038768614465...

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 1263ms
memory: 3528kb

input:

100
4
11618334 18295966 29914477 5116382 16734869 23412289 35030567 15002473 26620994 33298395 44916782 20118655 31737127 38414456 50032872
52573649 733715025 105771527 204824011
4
2142433 13679527 15822005 7304783 9447231 20984363 23126840 6271380 8413827 19950941 22093291 13576168 15718643 2725577...

output:

17648887427676796
21625331519839488
5123483163674640
20332898488476516
10882410276737710
25799406290369942
9021794681785918
14079661025657823
19651479225495798
19108340996997031
7502197529195960
14859286025000990
26534486448012504
21196964793845212
17222108748663918
19112873745810973
150116234459639...

result:

ok 100 numbers

Test #4:

score: 0
Accepted
time: 1267ms
memory: 3572kb

input:

100
4
19912840 5584811 25497404 10173442 30086113 15758284 35670730 10055784 29968204 15640365 35553036 20228929 40141706 25813818 45726645
199637398 648830099 620879036 721665690
4
7571016 7507237 15078004 19283701 26854580 26790914 34361725 2809630 10380467 10316661 17887650 22093187 29664080 2960...

output:

21172351446279597
12216316207781859
31685774482329793
36066732654517313
19340822375673782
17094361082328640
19075803268324489
23946411427126244
15750520513128720
10839993372177143
28038252774564427
12098359408203383
16661717061725678
16141878677697603
10422881545500853
23195259150880456
161145733965...

result:

ok 100 numbers

Test #5:

score: 0
Accepted
time: 1265ms
memory: 3568kb

input:

100
4
16976932 14037962 31014864 10197492 27174564 24235473 41212439 18912005 35888926 32949719 49926702 29109585 46086485 43147299 60124270
491925338 268977876 990762353 88764265
4
12999234 10104744 23104060 1196740 14196165 11301451 24300745 3216693 16215855 13321348 26320916 4413380 17412748 1451...

output:

23909367397934013
13788681506670460
23733351389222421
14554685971742458
21700214066873395
14675080284915330
33895419440563729
29492673616768327
11303756472296747
12482610599433273
14710253245102988
30271020171688425
9134330469412563
33975992434656396
17398716116891428
24545212510786538
3194464768874...

result:

ok 100 numbers

Test #6:

score: 0
Accepted
time: 1264ms
memory: 3556kb

input:

100
4
19074076 1326772 20400846 10221739 29295815 11548536 30622598 8932278 28006379 10259062 29333181 19154053 38228108 20480879 39554936
638989087 594158358 505869863 605605944
4
4624862 3932439 8557338 1945542 6570437 5877999 10502871 4787691 9412557 8720153 13345006 6733262 11358122 10665669 152...

output:

23556800109725722
10537032002627174
14660751629073194
20011815419177543
22625965024389053
11033496376968227
17546765754659334
27441279970436335
34448070575909396
25397224562147617
10757782582321602
16008699146950819
10145064828196714
41523392253608928
20626446413714925
32072152342903207
720635465608...

result:

ok 100 numbers

Test #7:

score: 0
Accepted
time: 1264ms
memory: 3572kb

input:

100
4
1171312 9779739 10950939 15278965 16449994 25058569 26229921 2821649 3992612 12601354 13772604 18100248 19271474 27880146 29051284
786052835 69081944 20977372 827480328
4
15086110 17760131 32846218 13924280 29010738 31684453 46770885 15128745 30215077 32888809 47975302 29053233 44139622 468134...

output:

4251688072802988
38416920079725055
6532611509976464
40289090747797420
5302425059666288
24716122297246780
40775392402414505
7056563165740518
17529843593302808
12819105623258024
20531058461804092
18722825798670288
33134881338491166
14337748949215833
28205390066848386
18508938475970114
1464607434360399...

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 1268ms
memory: 3568kb

input:

100
4
3268337 18232898 21501303 9105978 12374475 27338971 30607287 11677818 14946177 29910672 33178912 20783747 24052133 39016627 42285057
783373480 689229722 536084881 344322007
4
5547703 11587692 17135387 870321 6417974 12457985 18005620 15535900 21083520 27123601 32671264 16406183 21953851 279938...

output:

24029593865073835
17357370523557614
7754333149525536
24770903948059615
14829362656077913
13853154752037552
22856229602033323
23897428200156414
17506334124779784
16507611157738008
21743814085856734
27158737156121400
14411231424407837
12391527972875238
23916408781407438
24317152966273698
7400547834361...

result:

ok 100 numbers

Test #9:

score: 0
Accepted
time: 1270ms
memory: 3528kb

input:

100
4
5365456 5521877 10887385 9130162 14495850 14651940 20017468 6731144 12096612 12252742 17618423 15861269 21226812 21383080 26748653
930437229 309377499 905968199 566196390
4
17173130 5415382 22588448 1619174 18792328 7034555 24207585 12074036 29247238 17489345 34662518 13693236 30866361 1910850...

output:

18783561823107173
17174759455871110
20660176070779614
32551394415335213
14757437903189281
14353521145985327
8825454958613940
18626834204122338
23538161187858980
9768340101299130
10856044914259908
13798985739347606
28560306475402613
9937141276845409
22030208362253896
10007043951392399
932418252758094...

result:

ok 100 numbers

Test #10:

score: 0
Accepted
time: 1269ms
memory: 3524kb

input:

100
4
8626843 13974903 22601547 9154419 17781333 23129257 31755964 15587325 24214126 29562230 38188966 24741851 33368575 38716644 47343335
77500977 784301085 421075708 933294965
4
2601585 19242893 21844460 8564983 11166697 27808106 30409548 7448057 10049691 26690992 29292813 16013341 18614976 352562...

output:

30031441828206913
23706173942582594
35850620790374689
12248181406938398
3569227446883530
27644763176419295
20081336598385059
22229155823622873
32305575190424382
28752041038252008
7011854183413928
28284434195685860
23754711092334182
21112877189703804
21779236766630770
11439389176833922
16936798822777...

result:

ok 100 numbers

Test #11:

score: 0
Accepted
time: 1263ms
memory: 3480kb

input:

100
4
13688404 12628879 26317621 5351629 19040005 17980387 31669079 14469291 28157775 27098440 40786668 19820709 33509247 32449794 46138396
116132017 898003516 839972106 892258556
4
6839726 3899287 10739121 113756 6953512 4013022 10852729 18786521 25626326 22685885 29525686 18900424 25740120 2279973...

output:

30336258507082714
3257768626402136
28768287749346077
24983778209266624
33337982642789783
8256725694468078
22292037981448585
18920943294190842
19458942341010512
8558313401862953
15215499555800050
16557870271025505
19252729255629219
5089861441915246
20378861002078977
20236585760009299
1513702155613423...

result:

ok 100 numbers

Test #12:

score: 0
Accepted
time: 1268ms
memory: 3456kb

input:

100
4
818632 14885007 15703632 10408690 11227157 25293691 26112291 9522508 10341287 24407621 25226174 19931188 20749827 34816087 35634914
408419958 223183998 355079615 259357132
4
12268258 17726903 29995137 7059674 19327948 24786537 37054795 14160661 26428894 31887596 44155812 21220340 33488624 3894...

output:

9822123147255252
25706707261441731
30764683969228288
20463407949651199
13119143982680734
4511209144591007
17206988115593070
21613410637449924
6063335219524613
28247599960704248
20398515507252217
7805031270286406
43671110708013807
16822872474944611
26966106568765306
38046693018932635
2288946869140619...

result:

ok 100 numbers

Test #13:

score: -100
Wrong Answer
time: 1266ms
memory: 3476kb

input:

100
4
17882885 3338008 21220862 10432804 28315706 13770867 31653699 18378803 36261713 21716859 39599725 28811677 46694515 32149718 50032575
555483706 843331776 165154420 481231515
4
3893831 6521669 10415260 7808579 11702235 14330241 18223833 15731888 19625524 22253365 26147241 23540433 27434140 3006...

output:

23316214536490063
22139250765511441
22281944987058948
22368379936792866
6094559071158787
14179808411937036
19848270480774283
31800041401313086
24430928137055338
21245647160267298
28720402198184113
21139760374401571
21459062813217036
19167200380594251
9060291570904319
3342348642918590
372868618667565...

result:

wrong answer 60th numbers differ - expected: '12002450431833678', found: '12002450431833674'