QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#344895#3807. Canvas PaintingKhNURE_KIVI#AC ✓132ms22516kbC++233.1kb2024-03-05 17:52:542024-03-05 17:52:55

Judging History

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

  • [2024-03-05 17:52:55]
  • 评测
  • 测评结果:AC
  • 用时:132ms
  • 内存:22516kb
  • [2024-03-05 17:52:54]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl

template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }

#else
#define DEBUG while (false)
#define LOG(...)
#endif

mt19937 rng(4242);

const int max_n = -1, inf = 1000111222;

void solve() {
    int n;
    cin >> n;
    vector<int> c(n);
    for(auto& x : c) cin >> x;
    set<pair<int, ll> > av;
    set<pair<ll, int> > av2;
    set<pair<ll, pair<int, int> > > av_pair;
    for(int i = 0; i < n; i++) {
        av.insert({i, c[i]});
        av2.insert({c[i], i});
        if(i - 1 >= 0) av_pair.insert({c[i - 1] + c[i], {i - 1, i}});
    }
    ll ans = 0;
    while(len(av2) > 1) {
        auto A = (*av2.begin());
        av2.erase(A);
        auto B = (*av2.begin());
        av2.erase(B);
        ans += A.fi + B.fi;
        av2.insert({A.fi + B.fi, A.se});
    }
    cout << ans << '\n';
    return;
    while(!av_pair.empty()) {
        auto cur_pair = (*av_pair.begin());
        ans += cur_pair.fi;
        av_pair.erase(cur_pair);
        auto it_fir = av.lower_bound({cur_pair.se.fi, 0});
        pair<int, ll> prv = {-1, -1};
        if(it_fir != av.begin()) {
            auto it_prv = it_fir; it_prv--;
            prv = (*it_prv);
            av_pair.erase({(*it_prv).se + (*it_fir).se, {(*it_prv).fi, (*it_fir).fi}});
        }
        auto it_sec = it_fir; it_sec++;
        auto it_nxt = it_sec;
        it_nxt++;
        pair<int, ll> nxt = {-1, -1};
        if(it_nxt != av.end()) {
            nxt = (*it_nxt);
            av_pair.erase({(*it_sec).se + (*it_nxt).se, {(*it_sec).fi, (*it_nxt).fi}});
        }
        pair<int, ll> a_fir = (*it_fir);
        pair<int, ll> a_sec = (*it_sec);
        pair<int, ll> new_guy = {a_fir.fi, a_fir.se + a_sec.se};
        av.erase(a_fir);
        av.erase(a_sec);
        av.insert(new_guy);
        if(prv.fi != -1) {
            av_pair.insert({prv.se + new_guy.se, {prv.fi, new_guy.fi}});
        }
        if(nxt.fi != -1) {
            av_pair.insert({new_guy.se + nxt.se, {new_guy.fi, nxt.fi}});
        }
    }
    cout << ans << '\n';
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;

    cin >> t;

    while (t--) solve();

}

/*
KIVI
*/

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3556kb

input:

100
5
5 25 6 29 8
9
8 13 17 6 13 19 4 15 22
2
26 21
17
9 17 1 12 2 2 16 10 17 28 13 2 17 28 3 25 5
16
26 15 28 20 4 20 21 13 15 3 6 25 7 23 9 20
19
15 30 19 24 13 5 14 29 23 4 21 17 12 16 13 11 3 15 24
4
5 22 5 25
6
20 13 10 14 13 29
6
7 12 5 17 3 22
4
15 9 18 16
6
8 8 27 8 11 12
7
6 1 23 13 24 18 3...

output:

147
357
47
767
977
1267
99
248
155
116
183
295
252
513
153
1048
992
202
398
520
834
197
100
0
406
570
446
782
32
671
514
824
615
1296
253
617
557
669
0
775
569
1257
1041
108
1129
95
64
183
135
129
100
199
865
0
54
1347
0
1193
399
1350
671
975
610
300
205
26
590
15
133
481
955
99
490
149
196
1385
312...

result:

ok 100 lines

Test #2:

score: 0
Accepted
time: 75ms
memory: 22292kb

input:

1
100000
100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 1000...

output:

166892800000

result:

ok single line: '166892800000'

Test #3:

score: 0
Accepted
time: 107ms
memory: 22324kb

input:

1
100000
96267 94065 93345 1 1 94511 93671 94073 93267 1 1 98391 96191 98043 99559 1 93107 97741 97981 97795 95475 92925 99701 1 1 1 1 96313 98517 99853 96731 1 98963 93555 99175 99811 97815 99301 96835 98221 93587 92867 94445 94717 1 94683 1 95363 94471 1 1 93307 1 98539 99123 96435 1 1 94059 1 996...

output:

102523538776

result:

ok single line: '102523538776'

Test #4:

score: 0
Accepted
time: 116ms
memory: 12688kb

input:

2
50000
3375 9370 281 9600 9755 8660 1160 5528 6594 3340 2306 6104 3367 9711 9853 5395 5997 2634 953 1882 1020 8474 3329 6533 280 1399 978 3285 9638 7011 8637 3161 4632 8852 6875 4742 243 5884 7094 7833 8977 7151 6222 6153 7991 6438 8357 2119 9552 3180 6751 9364 2138 6325 3115 7325 1959 4590 8467 15...

output:

3822730184
3846218625

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 94ms
memory: 12656kb

input:

2
50000
4 42 83 59 32 80 40 100 27 85 1 87 7 63 29 15 2 47 31 5 7 73 42 49 63 19 42 65 48 48 52 1 62 52 69 27 88 12 40 56 63 98 93 68 27 54 62 23 64 76 85 61 64 93 73 89 14 13 87 86 36 80 71 73 61 1 44 83 42 52 27 34 99 63 84 27 94 46 54 1 100 2 67 49 40 96 11 84 16 85 54 22 73 62 82 95 100 99 96 47...

output:

38790615
39077924

result:

ok 2 lines

Test #6:

score: 0
Accepted
time: 132ms
memory: 22308kb

input:

1
100000
36827 68740 56301 25018 89059 83265 59618 61841 49994 14549 93689 5897 58167 98711 7611 96035 18767 7642 83672 37306 13097 99307 34986 73104 20872 21287 51067 88972 52464 7111 7934 71969 30606 81473 75829 70230 36115 41072 27605 36422 83822 97233 96186 1468 88216 31768 16958 8833 34832 3209...

output:

81830595089

result:

ok single line: '81830595089'

Test #7:

score: 0
Accepted
time: 126ms
memory: 22516kb

input:

1
100000
68179 6348 11415 56724 62188 35341 1780 18699 76161 88778 90911 63039 24489 16443 80605 84569 42292 73821 149 93403 79039 11651 46067 98013 6101 27627 12437 62056 83144 3007 39417 66446 45076 26153 81100 79710 33873 8364 81836 58098 13871 70616 26519 33146 31494 81422 52390 63764 62149 1353...

output:

81754360737

result:

ok single line: '81754360737'

Test #8:

score: 0
Accepted
time: 107ms
memory: 22352kb

input:

1
100000
169 69337 74834 74311 18651 5623 55711 17137 82765 89615 53302 37851 6482 46354 31492 58187 55607 82438 1348 22587 65376 27522 97336 70074 37181 85034 58274 41810 6960 52125 55717 24694 54908 19713 73335 48314 56751 91217 6648 32908 14871 47422 64 46879 97300 83649 46375 78509 6476 76300 29...

output:

81980009094

result:

ok single line: '81980009094'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

100
20
22 28 18 3 14 16 27 6 1 16 8 1 2 8 13 30 3 27 4 1
1
8
1
4
8
26 19 4 29 28 5 25 18
13
1 9 9 15 21 17 26 10 17 22 17 9 12
8
7 30 19 13 20 17 9 24
4
7 28 8 12
16
19 6 29 28 2 21 12 23 14 14 11 4 6 24 6 9
15
1 7 27 10 26 10 24 1 29 5 8 17 28 26 18
16
30 6 24 13 26 11 28 29 25 9 13 14 22 4 1 12
12...

output:

954
0
0
441
664
403
97
858
853
1014
501
5
303
1090
733
282
1032
918
1013
282
406
1044
749
290
54
211
1320
257
1071
1037
1008
1460
86
1000
139
830
795
602
1085
600
123
0
30
378
867
1066
308
664
174
657
242
103
0
715
279
684
859
1397
542
1012
1132
1166
897
1051
1007
131
401
818
974
1007
965
552
387
44...

result:

ok 100 lines

Test #10:

score: 0
Accepted
time: 89ms
memory: 9016kb

input:

16
29736
37259 86219 45426 69735 93508 48424 926 35023 80721 33718 52483 58934 40952 95839 49522 36041 29344 19924 48961 88554 60008 64522 48651 76691 28013 25674 53752 52053 14495 11647 73249 94734 72002 13243 3421 53399 1215 35380 52174 64284 14841 47392 65285 95376 15774 41742 74711 81027 19467 4...

output:

21695603180
10808524868
15381754349
2474639176
778417986
19047661271
153813613
32838270
100222249
203570
118500
709556
109556
0
94926
58681

result:

ok 16 lines

Test #11:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

100
16
8 10 4 3 4 9 1 3 7 10 3 10 3 8 6 2
17
2 10 1 4 4 7 5 9 5 8 9 8 8 5 4 1 7
9
9 6 9 7 9 3 2 5 4
11
5 6 4 6 3 5 4 2 7 5 10
11
4 10 10 5 7 9 7 8 9 5 3
19
7 7 4 1 9 8 5 2 4 8 10 3 2 5 9 6 10 2 10
18
2 4 9 9 4 8 6 8 3 8 7 2 7 2 10 10 7 9
2
3 6
12
4 8 4 8 8 2 5 7 5 7 10 5
2
5 4
15
3 2 9 5 6 9 3 1 10 ...

output:

347
382
167
194
262
458
466
9
257
9
285
67
58
354
174
34
70
278
251
224
329
0
18
357
59
544
305
245
238
37
267
118
198
206
397
501
27
271
362
299
60
12
174
0
10
255
250
453
0
451
84
29
102
276
9
126
36
147
433
399
184
497
319
24
101
56
346
469
190
28
78
183
96
154
463
376
151
308
269
89
246
34
245
4...

result:

ok 100 lines

Test #12:

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

input:

100
2
9 7
8
8 7 1 8 9 10 5 3
3
2 7 10
1
1
3
3 7 10
20
5 1 3 3 10 5 7 10 7 1 5 3 6 10 10 1 2 7 4 9
6
3 8 8 9 4 5
13
1 7 4 3 8 7 5 3 1 4 10 10 6
4
9 4 1 5
16
9 4 1 5 6 2 3 10 10 1 2 6 5 9 5 4
10
1 8 10 5 2 2 6 10 5 8
4
10 4 6 7
9
9 10 1 7 7 1 10 9 3
9
6 6 3 6 1 5 3 9 7
13
2 6 6 5 7 2 4 9 8 5 4 9 2
14
...

output:

16
147
28
0
30
449
93
241
34
309
179
54
168
140
247
284
414
235
49
167
463
121
250
299
45
172
34
81
224
77
266
81
196
0
409
252
272
34
432
426
142
380
116
74
10
447
182
339
243
428
390
216
68
241
53
28
55
341
230
338
177
279
211
292
285
106
375
10
20
149
245
341
0
218
13
350
267
131
170
118
290
114
...

result:

ok 100 lines

Test #13:

score: 0
Accepted
time: 114ms
memory: 20976kb

input:

9
91781
9315 63671 2866 77541 26295 47873 27449 46506 1850 65163 31843 61567 6935 70163 34101 54535 18186 42590 97535 27509 52424 72300 21818 78430 18101 70559 73005 85132 75391 76777 19529 64701 50352 47248 94834 62760 12087 50141 16555 97400 56049 50097 3851 64245 87542 2883 44791 74402 66691 7031...

output:

74538301120
332733758
4502248332
60434174
24306639
9648311
2639860
2122213
596692

result:

ok 9 lines

Test #14:

score: 0
Accepted
time: 83ms
memory: 10984kb

input:

10
40966
19979 15452 84467 931 87933 90817 63433 55344 4561 36524 67060 14293 63030 38806 48713 12842 57681 72811 88624 55437 56429 95493 54454 85831 27797 56236 5064 25316 56383 49618 66448 61123 69754 66841 7796 89518 80420 67880 78086 53934 81468 92443 78315 97316 37973 83256 448 45883 58369 5420...

output:

30839635755
26570108926
6275695867
3499409260
2425691627
1888052128
121514532
14300924
3004855
63820

result:

ok 10 lines

Test #15:

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

input:

20
1
1
2
1 1
3
2 1 1
4
3 1 1 2
5
1 3 1 2 5
6
1 8 2 3 5 1
7
3 2 13 1 8 1 5
8
2 3 21 1 13 1 8 5
9
1 8 2 21 5 13 1 34 3
10
2 13 55 1 5 1 34 3 21 8
11
34 3 1 55 13 2 89 8 21 5 1
12
13 2 8 1 1 89 3 21 55 144 34 5
13
89 34 21 55 233 1 13 3 8 144 5 2 1
14
1 377 55 3 144 21 2 5 13 233 8 34 1 89
15
1 8 233 5...

output:

0
2
6
13
25
45
78
132
220
363
595
971
1580
2566
4162
6745
10925
17689
28634
46344

result:

ok 20 lines

Test #16:

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

input:

20
1
1
2
1 1
3
3 1 1
4
1 5 3 1
5
3 1 1 5 9
6
1 9 5 15 3 1
7
1 9 1 3 15 25 5
8
1 3 5 1 15 25 41 9
9
15 5 67 3 25 1 41 1 9
10
9 1 25 1 109 41 15 3 5 67
11
41 15 25 177 1 109 67 1 5 9 3
12
67 109 5 3 41 1 25 287 9 1 15 177
13
3 109 9 287 1 5 465 1 41 15 67 177 25
14
41 15 465 177 25 9 1 67 753 287 109 ...

output:

0
2
7
17
36
70
129
229
396
672
1125
1865
3070
5028
8205
13355
21698
35208
57079
92479

result:

ok 20 lines

Test #17:

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

input:

2
3
7 4 7
4
5 3 7 5

output:

29
40

result:

ok 2 lines