QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#561579#4425. CakewisniewskijAC ✓7066ms159860kbC++202.6kb2024-09-13 00:09:152024-09-13 00:09:15

Judging History

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

  • [2024-09-13 00:09:15]
  • 评测
  • 测评结果:AC
  • 用时:7066ms
  • 内存:159860kb
  • [2024-09-13 00:09:15]
  • 提交

answer

#include <bits/stdc++.h>

#define ndl '\n'
#define ll long long
#define INF 1000000007
#define st first
#define nd second
#define debug(x) cout << #x << ": " << x << ndl
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()

using namespace std;

ll P{ static_cast<ll>(1e9 + 7) };

void rob() {
    ll n; cin >> n;
    vector<vector<ll>> a(n, vector<ll>(2)), b(n, vector<ll>(2));
    for (ll i = 0;i < n;i++) cin >> a[i][0];
    for (ll i = 0;i < n;i++) cin >> a[i][1];
    for (ll i = 0;i < n;i++) cin >> b[i][0];
    for (ll i = 0;i < n;i++) cin >> b[i][1];
    for (ll i = 1;i < n;i += 2) {
        swap(a[i][0], a[i][1]);
        swap(b[i][0], b[i][1]);
    }
    vector<ll> ah(n), bh(n);
    for (ll i = 0;i < n;i++) {
        ah[i] = a[i][0] + P * a[i][1];
        bh[i] = b[i][0] + P * b[i][1];
    }

    map<ll, vector<ll>> hidb;
    map<ll, ll> idx_b;
    vector<ll> perm(n);

    for (ll i = 0;i < n;i++) {
        if (hidb.find(bh[i]) == hidb.end()) {
            hidb.insert({ bh[i], vector<ll>(1, i) });
            idx_b[bh[i]] = 0;
        }
        else {
            hidb[bh[i]].push_back(i);
        }
    }

    bool ok = true;

    for (ll i = 0;i < n;i++) {
        if (hidb.find(ah[i]) == hidb.end() || hidb[ah[i]].size() <= idx_b[ah[i]]) {
            ok = false;
            break;
        }
        else {
            perm[i] = hidb[ah[i]][idx_b[ah[i]]++];
        }
    }
    if (!ok) {
        cout << -1 << '\n';
        return;
    }
    ll ans = 0;
    vector<ll> tmp(n);
    function<void(ll, ll)> merge_sort = [&](ll a, ll b) {
        if (a >= b) return;
        ll mid_poll = (a + b) / 2;
        merge_sort(a, mid_poll);
        merge_sort(mid_poll + 1, b);
        ll i1 = a, i2 = mid_poll + 1;
        for (ll i = a; i <= b;i++) {
            if (i1 == mid_poll + 1) {
                tmp[i] = perm[i2++];
            }
            else if (i2 == b + 1) {
                tmp[i] = perm[i1++];
            }
            else {
                if (perm[i1] <= perm[i2]) {
                    tmp[i] = perm[i1++];
                }
                else {
                    ans += i2 - i;
                    tmp[i] = perm[i2++];
                }
            }
        }
        for (ll i = a; i <= b;i++)
            perm[i] = tmp[i];
        };
    merge_sort(0, n - 1);
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    for (ll i = 0;i < t;i++)
        rob();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5853ms
memory: 159832kb

input:

4
500000
471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...

output:

0
-1
124999750000
62569045381

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 7066ms
memory: 159860kb

input:

4
500000
413978977 447850293 802546055 289227062 321812178 97254226 380555063 167654142 874843339 349071516 10868808 93093321 996630037 33169052 99312773 335065051 278187197 94810404 643530967 947474510 63047182 573006260 8470509 116477506 251504796 899920047 806158195 452300674 859769748 489596554 ...

output:

62623368052
62486676013
62520737719
62506121292

result:

ok 4 lines

Test #3:

score: 0
Accepted
time: 1179ms
memory: 6356kb

input:

9082
7
714942297 703160828 709363739 23094554 461254804 159017923 666590460
196100940 70571239 755082029 321718821 55663261 378527017 335969190
321718821 335969190 70571239 755082029 378527017 55663261 714942297
23094554 666590460 703160828 709363739 159017923 461254804 196100940
5
2 1 1 2 1
2 1 2 2...

output:

13
5
0
-1
0
2
10
2
0
5
0
10
15
1
2336
-1
1
-1
-1
4
-1
15
0
9
45
15
3
4
0
11
-1
-1
10
-1
18
10
0
3
1
-1
0
1
0
5
-1
16
-1
-1
-1
15
-1
307294
-1
7
0
0
-1
-1
-1
0
2210
3
1
16
-1
-1
0
-1
0
0
5
5
10
3
0
-1
21
0
7
3
0
32308741
-1
20
-1
0
1
-1
9
6
2249
0
-1
0
55236
2265
1
12
-1
-1
29
0
-1
20
0
2
0
0
1
-1
0
...

result:

ok 9082 lines

Extra Test:

score: 0
Extra Test Passed