QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394912#1363. Bitonic OrderingZuqa#AC ✓311ms34952kbC++204.4kb2024-04-20 21:39:462024-04-20 21:39:47

Judging History

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

  • [2024-04-20 21:39:47]
  • 评测
  • 测评结果:AC
  • 用时:311ms
  • 内存:34952kb
  • [2024-04-20 21:39:46]
  • 提交

answer

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

#define el '\n'
#define FIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned uint;
typedef __int128 bint;
typedef long double ld;
typedef complex<ld> pt;
typedef unsigned long long ull;

template<typename T, typename X>
using hashTable = gp_hash_table<T, X>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

// mt19937_64 for unsigned long long
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());

ll doWork(vector<int> v)
{
    int n = v.size();


//    int n;
//    cin >> n;
//    vector<int> v(n);
    vector<ll> suff(n + 1);
    ordered_set<int> a, b;

//    for(int i = 0; i < n; i++)
//        cin >> v[i];


    for(int i = n - 1; i >= 0; i--)
    {
        suff[i] = b.size() - b.order_of_key(v[i]), b.insert(v[i]);
        if(i + 1 < n)
            suff[i] += suff[i + 1];
    }

    ll ans = suff[0], invA = 0;
    for(int i = 0; i < n; i++)
    {
        invA += a.size() - a.order_of_key(v[i]);
        cout << invA << ' ';
        a.insert(v[i]);
        if(i + 1 < n)
            ans = min(ans, invA + suff[i + 1]);
        else
            ans = min(ans, invA);
    }
    cout << '\n';
    for(int i = 0; i < n; i++)
        cout << suff[i] - suff[i + 1] << ' ';
    exit(0);
    return ans;
}

ll doWork2(vector<int> &v)
{
    int n = v.size();


//    int n;
//    cin >> n;
//    vector<int> v(n);
    vector<ll> suff(n);
    ordered_set<int> a, b;

//    for(int i = 0; i < n; i++)
//        cin >> v[i];


    for(int i = n - 1; i >= 0; i--)
    {
        suff[i] = b.size() - b.order_of_key(v[i]), b.insert(v[i]);
//        if(i + 1 < n)
//            suff[i] += suff[i + 1];
    }

    ll ans = 0, invA = 0;
    for(int i = 0; i < n; i++)
    {
        invA = a.size() - a.order_of_key(v[i]);
        a.insert(v[i]);
        ans += min(invA, suff[i]);
//        if(i + 1 < n)
//            ans += min(invA, suff[i + 1]);
    }

    return ans;
}

bool valid(vector<int> &v)
{
    bool desc = false;
    for(int i = 1; i < v.size(); i++)
    {
        if(v[i - 1] < v[i])
        {
            if(!desc)
                continue;
            else
                return false;
        }
        else
        {
            if(desc)
                continue;
            else
                desc = 1;
        }
    }
    return true;
}

ll dist(vector<int> a, vector<int> &b)
{
    int ret = 0;
    for(int i = 0; i < a.size(); i++)
    {
        int j = find(a.begin(), a.end(), b[i]) - a.begin();
        if(i == j)
            continue;
        if(i < j)
        {
            for(int k = j - 1; k >= i; k--)
                ret++, swap(a[k], a[k + 1]);
        }
        else
        {
            assert(false);
            for(int k = i; k - 1 >= j; k--)
                ret++, swap(a[k - 1], a[k]);
        }
    }

    return ret;
}

ll brute(vector<int> &v)
{

    vector<int> p(v);
    sort(p.begin(), p.end());

    ll ans = LLONG_MAX;
    do
    {
        if(valid(p))
            ans = min(ans, dist(v, p));

    } while(next_permutation(p.begin(), p.end()));

    return ans;
}

signed main()
{
    FIO
    int T = 1;
//    doWork({2, 3, 4, 1, 5, 6});
//    cin >> T;
    for(int i = 1; i <= T; i++)
    {
        int n;
        cin >> n;
        vector<int> v(n);
        for(auto &it: v)
            cin >> it;
        cout << doWork2(v);

//        int n = 7;
//        vector<int> p(n);
//        iota(p.begin(), p.end(), 1);
//        do
//        {
//
//            if(brute(p) != doWork2(p))
//            {
//                cout << n << '\n';
//                for(auto &it: p)
//                    cout << it << ' ';
//                cout << "\nBrute: " << brute(p) << '\n';
//                cout << "Hazem: " << doWork(p) << '\n';
//                cout << "Hesham: " << doWork2(p) << '\n';
//
//                exit(0);
//            }
//        } while(next_permutation(p.begin(), p.end()));

    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

13
39
40
32
100
13
16
15
28
27
26
25
24
23

output:

14

result:

ok single line: '14'

Test #2:

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

input:

4
3
2
1
10

output:

2

result:

ok single line: '2'

Test #3:

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

input:

13
40
80
90
60
100
20
53
52
51
50
49
48
47

output:

6

result:

ok single line: '6'

Test #4:

score: 0
Accepted
time: 6ms
memory: 4572kb

input:

10000
6048
7145
9278
4540
2505
6384
3715
7910
195
3426
9617
8881
3292
8975
7835
8954
160
5491
760
7767
1502
8277
852
6962
7901
7652
5673
4804
7214
4630
9962
672
6568
5301
179
3767
8589
3256
9022
1215
6827
1809
978
3787
5329
1295
2483
8331
4345
6805
7446
4695
5399
8550
5452
3968
3634
214
1348
6640
91...

output:

12538308

result:

ok single line: '12538308'

Test #5:

score: 0
Accepted
time: 86ms
memory: 13840kb

input:

100000
79611
99198
59215
12212
1068
20968
92768
4976
52551
71627
42534
9227
75293
84258
28203
3971
21644
10476
48090
31870
70655
39717
63939
36338
42901
51351
49298
76460
77186
50374
78750
38146
75785
80387
22773
56976
19956
18105
96559
16337
29650
82378
54109
30126
55031
29411
28918
96418
72024
213...

output:

1253664580

result:

ok single line: '1253664580'

Test #6:

score: 0
Accepted
time: 47ms
memory: 13692kb

input:

100000
100000
99998
99996
99994
99992
99990
99988
99986
99984
99982
99980
99978
99976
99974
99972
99970
99968
99966
99964
99962
99960
99958
99956
99954
99952
99950
99948
99946
99944
99942
99940
99938
99936
99934
99932
99930
99928
99926
99924
99922
99920
99918
99916
99914
99912
99910
99908
99906
9990...

output:

2499950000

result:

ok single line: '2499950000'

Test #7:

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

input:

20
480659024
557251654
627039283
83671517
815607005
812010293
18889126
899462139
815092936
18782502
811978055
62214200
312916198
215867715
922316200
923490093
51888046
945784009
416508880
272018251

output:

43

result:

ok single line: '43'

Test #8:

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

input:

20
488169047
354911583
366715081
140586591
488269292
450116025
352059629
497699006
555548218
705083255
24313401
787684500
43432368
48213034
135520237
950053073
637528936
520213072
137486174
811461181

output:

42

result:

ok single line: '42'

Test #9:

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

input:

19
949023009
661484995
43104597
691253877
453251490
569684983
231700321
423061661
552014871
689973096
243654875
888922996
476078212
981360873
531490171
844546110
461127125
964806680
78133381

output:

42

result:

ok single line: '42'

Test #10:

score: 0
Accepted
time: 82ms
memory: 13840kb

input:

100000
833677886
477656913
806058172
860529353
971557837
71317556
984435297
162644142
514451450
777003448
201821692
429166419
900440106
705421423
533952632
883004145
889321213
634010970
87471274
922658701
641825417
170301294
887883326
598426548
772306703
376457530
209063984
315040475
311489243
28515...

output:

1249363536

result:

ok single line: '1249363536'

Test #11:

score: 0
Accepted
time: 85ms
memory: 13752kb

input:

100000
990906706
933485086
42263363
335688225
420297302
780399610
491053539
835476157
694161548
944299317
249583460
780800860
593679612
940460218
757819780
810638257
419052986
821831825
4460305
544781675
311289984
696339252
743382922
188758108
131535618
558794863
485968343
484274832
501687789
490457...

output:

1254732842

result:

ok single line: '1254732842'

Test #12:

score: 0
Accepted
time: 66ms
memory: 13708kb

input:

100000
3
18
22
26
63
71
82
88
140
142
148
159
191
198
238
255
296
314
318
341
377
399
417
493
527
551
559
572
595
612
614
653
754
782
811
948
960
970
1014
1071
1094
1097
1113
1157
1201
1231
1254
1270
1286
1299
1301
1324
1383
1419
1443
1462
1481
1483
1542
1551
1552
1594
1602
1612
1618
1621
1680
1691
...

output:

0

result:

ok single line: '0'

Test #13:

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

input:

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

output:

0

result:

ok single line: '0'

Test #14:

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

input:

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

output:

0

result:

ok single line: '0'

Test #15:

score: 0
Accepted
time: 311ms
memory: 34828kb

input:

300000
855435593
265463680
986827141
719711703
527695544
996983669
458925829
310193034
950381107
168031936
310016472
53515205
407507886
338050751
897678729
420238223
38727769
465718921
241877215
33256048
165574114
76408720
313316899
110711274
55729537
419238872
296115591
427165392
534214801
21340349...

output:

11244378904

result:

ok single line: '11244378904'

Test #16:

score: 0
Accepted
time: 199ms
memory: 34952kb

input:

300000
299998
299996
299994
299992
299990
299988
299986
299984
299982
299980
299978
299976
299974
299972
299970
299968
299966
299964
299962
299960
299958
299956
299954
299952
299950
299948
299946
299944
299942
299940
299938
299936
299934
299932
299930
299928
299926
299924
299922
299920
299918
299916...

output:

22499700001

result:

ok single line: '22499700001'

Test #17:

score: 0
Accepted
time: 209ms
memory: 34828kb

input:

300000
4436
11291
12457
21839
28465
29593
40014
40245
44296
45084
50070
55903
58856
59638
72149
77652
77949
78873
80379
83670
85039
96575
104656
106648
108352
116210
117045
122580
130154
135157
136736
136844
143481
144636
157354
167486
172257
175989
181253
186979
201542
207646
220033
232574
235595
2...

output:

0

result:

ok single line: '0'