QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#244394#4936. Shopping Changesrgnerdplayer#WA 64ms3792kbC++202.0kb2023-11-09 01:15:572023-11-09 01:15:58

Judging History

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

  • [2023-11-09 01:15:58]
  • 评测
  • 测评结果:WA
  • 用时:64ms
  • 内存:3792kb
  • [2023-11-09 01:15:57]
  • 提交

answer

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

using i64 = long long;

template <typename T>
struct Fenwick {
    int n;
    vector<T> a;
    Fenwick(int n = 0) : n(n), a(n) {}
    void add(int p, T v) {
        while (p < n) {
            a[p] += v;
            p |= p + 1;
        }
    }
    T sum(int p) {
        T res = {};
        while (p >= 0) {
            res += a[p];
            p = (p & p + 1) - 1;
        }
        return res;
    }
};

i64 countInversion(const vector<int> &a) {
    int n = a.size();
    Fenwick<int> f(n);
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](auto i, auto j) {
        return pair(a[i], -i) < pair(a[j], -j);
    });

    i64 res = 0;
    for (auto i : ord) {
        res += f.sum(n - 1) - f.sum(i);
        f.add(i, 1);
    }

    return res;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    auto solve = [&]() {
        int n, q;
        cin >> n >> q;

        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        auto s = a;
        sort(s.begin(), s.end());

        i64 base = countInversion(a);

        while (q--) {
            int m;
            cin >> m;

            vector<int> b(m);
            for (int i = 0; i < m; i++) {
                cin >> b[i];
            }

            i64 res = 0;
            for (int i = 0; i < m; i++) {
                res += n - (upper_bound(s.begin(), s.end(), b[i]) - s.begin());
            }

            i64 cur = res;
            for (int i = 0; i < m; i++) {
                cur -= n - (upper_bound(s.begin(), s.end(), b[i]) - s.begin());
                cur += lower_bound(s.begin(), s.end(), b[i]) - s.begin();
                res = min(res, cur);
            }

            res += base + countInversion(b);
            cout << res << '\n';
        }
    };
    
    solve();
    
    return 0;
}

详细

Test #1:

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

input:

3 3
5 6 7
6 2 3 4 8 9 10
2 100 99
3 5 6 7

output:

0
1
1

result:

ok 3 lines

Test #2:

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

input:

3 2
7 6 5
6 2 3 4 8 9 10
6 10 9 8 4 3 2

output:

3
27

result:

ok 2 lines

Test #3:

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

input:

1 1
589284012
1 767928734

output:

0

result:

ok single line: '0'

Test #4:

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

input:

10000 9999
298772847 712804102 869795012 527998188 804246450 598105371 843966934 639805471 937482040 887551242 254734680 188704975 17408126 626523673 553956319 697723205 231690822 637079761 232393146 377026277 962223856 338922458 912529500 710873344 942955137 51167037 195729799 529674367 990599310 4...

output:

24802338
24830913
24857654
24813132
24846150
24785558
24857315
24805175
24862714
24785339
24804999
24780907
24808009
24798987
24958122
24781372
24772291
24846071
24953540
24778276
24778689
24979527
24770012
24892097
24776909
24865295
24821506
24772586
24800432
24891424
24864488
24820312
24801522
248...

result:

ok 9999 lines

Test #5:

score: -100
Wrong Answer
time: 64ms
memory: 3792kb

input:

42320 25000
977178721 305456426 916831455 324594376 259922325 798438534 906876242 353428436 459214642 504133134 734517252 944888626 929971853 735313273 285979369 866298401 385768124 918185862 811827492 3054135 190006456 852394509 784943097 903969029 4089198 931644108 916374905 942243264 383987411 45...

output:

448869836
448857082
449001852
448984954
448766023
449088371
448753000
448813228
448791233
448952236
448991582
448762189
449564835
448895405
448773617
448827148
448822398
448871141
448785181
448915023
448786816
448758119
448930421
449164517
448765913
448826223
448791835
448895669
448789205
448791198
...

result:

wrong answer 1st lines differ - expected: '448869835', found: '448869836'