QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#76091#1397. Growing Vegetables is Funnot_so_organic100 ✓71ms12156kbC++143.4kb2023-02-07 16:42:172023-02-07 16:42:19

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-07 16:42:19]
  • Judged
  • Verdict: 100
  • Time: 71ms
  • Memory: 12156kb
  • [2023-02-07 16:42:17]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;


#define INF 0x3f3f3f3f3f3f3f3f
#define int long long
#define endl "\n"


#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << "\n";}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << "   ";
    err(++it, args...);
}
template <typename T>inline void rd(T &t) {
    int s = 0, w = 1;
    char ch = getchar();

    while (ch < '0' || ch > '9') {
        if (ch == '-')
            w = -1;

        ch = getchar();
    }

    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();

    t = s * w;
}
template <typename T, typename... Args> inline void rd(T &t, Args &... args) {
    rd(t);
    rd(args...);
}




typedef double db;
typedef long long ll;
typedef unsigned long long ull;

int qmi(int a, int k, int p) {
    int res = 1;

    while (k) {
        if (k & 1)
            res = (ll)res * a % p;

        a = (ll)a * a % p;
        k >>= 1;
    }

    return res;
}
int qpow(int a, int b) {
    int res = 1;

    while (b) {
        if (b & 1)
            res *= a;

        b >>= 1;
        a *= a;
    }

    return res;
}
int mo(int x, int p) {
    return x = ((x % p) + p) % p;
}
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}


const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-6;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

int T = 1, N, M, K, Q;


template <typename T>
struct Fenwick {
    const int n;
    std::vector<T> a;
    Fenwick(int n) : n(n), a(n) {}
    void add(int x, T v) {
        for (int i = x; i < n; i += i & -i) {
            a[i] += v;
        }
    }
    T sum(int x) {
        T ans = 0;

        for (int i = x; i > 0; i -= i & -i) {
            ans += a[i];
        }

        return ans;
    }
    T rangeSum(int l, int r) {
        return sum(r) - sum(l - 1);
    }
};



void solve() {
    cin >> N;
    vector<int> H(N + 1), id(N + 1);

    for (int i = 1; i <= N; i ++) {
        cin >> H[i];
        id[i] = i;
    }

    sort(id.begin() + 1, id.end(), [&](int i, int j) {
        return H[i] > H[j];
    });

    Fenwick<int> t(N + 1);
    int now = 0, ans = 0;

    for (int i = 1; i <= N; i ++) {
        if (i < N && H[id[i]] == H[id[i + 1]]) {
            vector<int> all;

            for (int j = i; j <= N && H[id[j]] == H[id[i]]; j++) {
                all.push_back(id[j]);
            }

            for (auto x : all) {
                ans += min(t.sum(x), now - t.sum(x));
            }

            for (auto x : all) {
                t.add(x, 1);
                // debug(x);
            }

            now += all.size();
            i += all.size() - 1;
            // debug(ans);
            continue;
        }

        int r = t.sum(id[i]);
        int l = now - r;
        ans += min(l, r);
        t.add(id[i], 1);
        now ++;
        // debug(ans);
    }

    cout << ans << endl;


}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // cin >> T;
    for (int i = 1; i <= T; i ++)
        solve();

    return (0 - 0); //<3
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 3364kb

input:

5
2
8
3
5
4

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3356kb

input:

8
8
5
1
4
15
51
11
28

output:

6

result:

ok single line: '6'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3468kb

input:

8
100
200
50
20
200
100
800
999

output:

7

result:

ok single line: '7'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3492kb

input:

7
1
4
7
7
7
4
1

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3420kb

input:

8
99
99
99
99
99
99
99
99

output:

0

result:

ok single line: '0'

Test #6:

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

input:

8
96000000
84000000
72000000
60000000
48000000
36000000
24000000
12000000

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3336kb

input:

8
601322016
20074950
155956323
324319474
941696870
997306693
403450092
502381552

output:

4

result:

ok single line: '4'

Test #8:

score: 0
Accepted
time: 2ms
memory: 3488kb

input:

8
400000000
400000000
263539424
400000000
400000000
458424839
414860359
502762549

output:

3

result:

ok single line: '3'

Test #9:

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

input:

8
90000000
70000000
50000000
10000000
30000000
50000000
70000000
90000000

output:

12

result:

ok single line: '12'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3412kb

input:

7
307518180
383138743
400195098
300476710
2014
2014
408432459

output:

3

result:

ok single line: '3'

Subtask #2:

score: 20
Accepted

Dependency #1:

100%
Accepted

Test #11:

score: 20
Accepted
time: 2ms
memory: 3492kb

input:

12
20
80
30
55
30
80
90
10
30
70
85
90

output:

14

result:

ok single line: '14'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

14
10
30
30
80
200
400
400
150
80
30
30
30
10
5

output:

0

result:

ok single line: '0'

Test #13:

score: 0
Accepted
time: 2ms
memory: 3340kb

input:

11
100000000
200000000
300000000
400000000
500000000
600000000
700000000
800000000
900000000
900000000
1000000000

output:

0

result:

ok single line: '0'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3340kb

input:

16
38020
28510
59109
60500
10002
108
22450
108
51002
52002
51890
22450
25202
108
29592
60500

output:

32

result:

ok single line: '32'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

16
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

15
107286470
941389353
874067939
6662747
117798001
16030521
552757157
979431792
529843078
75288560
434084836
612367732
554396938
550271904
838855249

output:

28

result:

ok single line: '28'

Test #17:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

16
900000
800000
500000
200000
100000
200000
300000
350000
400000
450000
500000
600000
700000
800000
900000
950000

output:

32

result:

ok single line: '32'

Test #18:

score: 0
Accepted
time: 2ms
memory: 3340kb

input:

15
900000000
742765535
900000000
900000000
748159432
900000000
794026130
900000000
900000000
900000000
756475608
724054692
702828667
900000000
900000000

output:

14

result:

ok single line: '14'

Test #19:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

16
33349
34423
32907
34508
34793
31811
32189
31304
30367
34379
34880
30611
32485
31519
33826
33864

output:

35

result:

ok single line: '35'

Test #20:

score: 0
Accepted
time: 2ms
memory: 3340kb

input:

16
101164824
178805848
143926182
112256661
107521495
300000000
117240323
195257629
173688529
130829754
176973662
300000000
120285783
179225073
300000000
147582471

output:

25

result:

ok single line: '25'

Subtask #3:

score: 15
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #21:

score: 15
Accepted
time: 1ms
memory: 3428kb

input:

300
905972146
255058605
114356099
848257125
17356212
44031328
549347853
569474479
929925360
240283829
631232520
517625830
60915700
859666464
380617964
817506341
322274449
430302558
923936575
61956725
291315270
968447034
574299484
902436043
127263796
213019116
375236935
84073013
404288886
259876265
9...

output:

11395

result:

ok single line: '11395'

Test #22:

score: 0
Accepted
time: 2ms
memory: 3400kb

input:

1500
79946
79474
79675
79660
79518
79493
79470
79409
79376
79366
79353
79227
79218
79214
79023
79012
78826
78822
78603
78402
78378
78308
78238
78193
78048
78030
78004
77942
77861
77782
77714
77581
77571
77498
77448
77381
77256
77197
77091
77066
76991
76988
76821
76541
76445
76334
76237
76201
76165
7...

output:

454508

result:

ok single line: '454508'

Test #23:

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

input:

2000
2696004
2246305
3270781
2511502
2690816
3485259
2625527
2220653
2166454
3349565
2406002
2337076
1836814
3192506
2359528
3102772
1877092
2600732
3337548
2715324
2769839
3063551
2014320
3336485
2209479
2671488
2158795
3259096
2716045
1926281
2247626
2611692
3487745
3310276
2680516
3371174
2945488...

output:

490787

result:

ok single line: '490787'

Test #24:

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

input:

3500
820954323
816626000
805518484
863139953
812584408
815507238
842079239
839884436
833205942
819771996
826927399
833611737
828203073
897181374
817424064
874054999
802179258
892574766
863865111
875177500
800151880
813633968
832334997
891416716
817697463
874315453
842852532
861541431
897557884
82975...

output:

1522670

result:

ok single line: '1522670'

Test #25:

score: 0
Accepted
time: 3ms
memory: 3516kb

input:

5000
899883354
899866752
899791077
899055543
898862130
898196478
897414043
897156002
896400041
895392879
895295234
894112179
893293478
892863147
892636199
892546061
892435163
892425150
892220215
891927209
890910281
889607840
888212207
888080092
888027513
886381802
886087825
886041891
886015635
88594...

output:

6202441

result:

ok single line: '6202441'

Test #26:

score: 0
Accepted
time: 2ms
memory: 3540kb

input:

5000
2
2
1
2
2
1
2
2
1
2
2
2
2
1
1
1
1
2
1
2
1
2
1
2
1
2
2
2
2
1
2
2
2
2
2
2
1
1
2
1
2
1
2
1
2
1
2
2
2
2
2
2
1
2
2
1
2
1
2
2
1
1
1
1
1
1
2
1
1
2
2
2
2
2
2
2
1
2
1
1
2
2
1
1
1
1
2
2
1
1
2
1
1
2
1
2
2
2
2
2
2
1
1
1
1
1
1
1
2
2
1
1
2
2
1
1
2
2
1
2
2
1
1
1
2
2
2
2
1
2
2
1
2
2
2
2
1
2
1
1
1
2
2
1
1
1
2
1...

output:

1571267

result:

ok single line: '1571267'

Test #27:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

4800
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
100000000
10000...

output:

58685

result:

ok single line: '58685'

Test #28:

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

input:

5000
99990847
99958911
99951484
99884752
99873039
99855557
99749245
99734193
99702560
99683715
99670572
79478775
99653650
99652237
99607302
99331989
99196569
99060089
98948431
98939155
98923440
15980173
98892981
98849699
98842397
98836510
98768293
98693798
98665147
98638832
98616304
98604066
9855948...

output:

4508957

result:

ok single line: '4508957'

Test #29:

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

input:

5000
16272
17913
14296
10863
19061
15249
12924
15376
10828
17939
12123
18129
10486
10238
18400
14442
17154
11854
19058
12259
15454
13778
10032
16199
15615
13367
10681
10898
15069
19832
17077
14272
14474
14274
17163
18446
12758
10527
18915
13616
11497
12425
17612
15455
19302
16494
17472
16536
18583
1...

output:

3176793

result:

ok single line: '3176793'

Test #30:

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

input:

5000
401006871
77690818
400222
599959
913936
1218098
1527689
1531213
1707469
1988567
2065256
2377143
2850070
2911949
3185728
3245754
3287852
3357279
3414479
3446594
3804615
3983716
4198690
4219752
4339147
4465441
4472903
4736039
4803482
4818932
5087757
5303282
5510223
5593786
5612238
5652557
5702317...

output:

41132

result:

ok single line: '41132'

Subtask #4:

score: 55
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #31:

score: 55
Accepted
time: 8ms
memory: 4512kb

input:

60000
327
323
435
484
367
470
440
401
419
316
463
376
373
390
313
456
449
484
491
320
459
400
344
479
368
374
479
500
366
407
453
384
409
418
376
367
483
455
399
432
454
492
417
439
336
467
305
497
395
432
454
440
442
303
380
371
352
451
336
386
329
361
360
428
340
389
360
333
311
470
384
451
434
45...

output:

445274476

result:

ok single line: '445274476'

Test #32:

score: 0
Accepted
time: 20ms
memory: 5356kb

input:

100000
598913959
591914160
544100470
634333723
748957543
551549197
791035144
607626613
681383219
724006282
790999360
498465770
749167407
620657762
827094307
501105011
531706320
379798780
490328278
813813635
638471421
774787849
607804324
515167187
514811629
650078763
562578962
708153601
497099747
486...

output:

1246845047

result:

ok single line: '1246845047'

Test #33:

score: 0
Accepted
time: 24ms
memory: 6940kb

input:

150000
3107566
3841671
4968931
2341054
2718319
4685332
4687412
3673583
1886927
4743550
1270413
3544860
1545930
1894496
4404197
2015505
1234567
2276445
1324101
3402848
1634732
4628874
1030695
4246668
2043338
2195068
3371824
2290313
2282494
4738586
2073865
4270508
4248990
2739682
1234567
3356981
12345...

output:

2768560877

result:

ok single line: '2768560877'

Test #34:

score: 0
Accepted
time: 35ms
memory: 8068kb

input:

200000
305458949
208616035
309366541
909898736
232452203
597576466
102969140
960207698
728809280
374455922
810908010
625334706
377057501
100000000
519630205
350156994
619746942
124424525
353148832
162419588
630420232
100000000
100000000
100000000
107899485
527488355
623165933
790165401
100000000
100...

output:

4908376472

result:

ok single line: '4908376472'

Test #35:

score: 0
Accepted
time: 13ms
memory: 7728kb

input:

200000
999999962
999999900
999999514
999999496
999999443
999999350
999999111
999998987
999998737
999998722
999998542
999998426
999998333
999998280
999998239
999997963
999997798
999997773
999997666
999997582
999997570
999997356
999997334
999997296
999997235
999997219
999997179
999996941
999996809
999...

output:

0

result:

ok single line: '0'

Test #36:

score: 0
Accepted
time: 20ms
memory: 5352kb

input:

100000
999988873
999985523
999971843
999971294
999968067
999967321
999963976
999953616
140127673
999943361
999936863
999934484
999923485
999912201
999907041
999892489
999885889
999869798
999859543
999855308
999838204
999808280
999801949
999781044
999749049
999725527
999712244
999704728
999691316
999...

output:

1512301800

result:

ok single line: '1512301800'

Test #37:

score: 0
Accepted
time: 33ms
memory: 11912kb

input:

300000
8
8
7
7
8
9
9
7
7
9
7
9
9
7
8
7
7
8
8
9
7
9
9
8
8
9
8
7
8
8
7
8
8
9
7
8
8
9
8
7
8
7
7
8
7
8
8
8
9
9
8
9
7
7
8
7
9
9
7
9
9
8
8
7
9
9
9
9
7
8
7
7
8
8
9
8
7
9
9
7
7
9
7
8
9
8
9
9
7
9
7
9
7
8
8
8
8
9
7
8
9
8
8
7
7
9
8
7
9
8
7
9
9
8
7
8
7
8
9
8
8
8
7
7
7
7
9
7
9
7
8
9
7
7
7
8
7
8
9
7
7
7
8
8
8
7
7...

output:

7490078769

result:

ok single line: '7490078769'

Test #38:

score: 0
Accepted
time: 71ms
memory: 10036kb

input:

300000
999986937
999979173
999976948
999976149
999971258
999968909
999968865
999960077
999933763
999930113
999922546
999921628
999914084
999907460
999903496
999898433
999891622
999889880
999888993
999888552
999882379
999881735
999877822
999877756
999866351
999865179
999855076
999852978
999847696
999...

output:

22467143769

result:

ok single line: '22467143769'

Test #39:

score: 0
Accepted
time: 56ms
memory: 12156kb

input:

300000
987654321
946288524
987654321
987654321
275399914
883210294
987654321
987654321
107623361
384074109
536034615
877069300
987654321
987654321
987654321
87485338
987654321
987654321
987654321
982892990
987654321
987654321
987654321
339084407
987654321
987654321
71727362
858369953
987654321
37948...

output:

8425332391

result:

ok single line: '8425332391'

Test #40:

score: 0
Accepted
time: 48ms
memory: 9992kb

input:

300000
499999592
499993548
637424993
499983905
499981815
421439860
331371623
499968691
499963181
499960396
499960291
503510458
656004049
499948299
499948015
427044059
499944663
499943692
499942472
499938448
499933044
499931792
650357387
499927651
499925577
499902390
499899835
631575847
499896435
499...

output:

11562834650

result:

ok single line: '11562834650'

Extra Test:

score: 0
Extra Test Passed