結果

問題 No.1209 XOR Into You
ユーザー simasima_71simasima_71
提出日時 2020-07-29 16:27:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 2,458 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 129,312 KB
実行使用メモリ 27,596 KB
最終ジャッジ日時 2023-09-11 11:57:43
合計ジャッジ時間 14,610 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 77 ms
4,736 KB
testcase_05 AC 77 ms
4,684 KB
testcase_06 AC 94 ms
7,796 KB
testcase_07 AC 245 ms
23,420 KB
testcase_08 AC 221 ms
21,392 KB
testcase_09 AC 218 ms
21,428 KB
testcase_10 AC 173 ms
18,200 KB
testcase_11 AC 216 ms
21,236 KB
testcase_12 AC 195 ms
19,988 KB
testcase_13 AC 229 ms
22,160 KB
testcase_14 AC 190 ms
19,332 KB
testcase_15 AC 214 ms
20,244 KB
testcase_16 AC 201 ms
19,936 KB
testcase_17 AC 184 ms
19,328 KB
testcase_18 AC 293 ms
26,380 KB
testcase_19 AC 196 ms
20,048 KB
testcase_20 AC 231 ms
22,252 KB
testcase_21 AC 168 ms
17,492 KB
testcase_22 AC 188 ms
27,308 KB
testcase_23 AC 189 ms
27,340 KB
testcase_24 AC 187 ms
27,596 KB
testcase_25 AC 296 ms
27,256 KB
testcase_26 AC 288 ms
27,456 KB
testcase_27 AC 290 ms
27,344 KB
testcase_28 AC 284 ms
27,408 KB
testcase_29 AC 284 ms
27,312 KB
testcase_30 AC 285 ms
27,392 KB
testcase_31 AC 142 ms
11,472 KB
testcase_32 AC 141 ms
11,428 KB
testcase_33 AC 142 ms
11,416 KB
testcase_34 AC 147 ms
11,488 KB
testcase_35 AC 142 ms
11,604 KB
testcase_36 AC 143 ms
11,448 KB
testcase_37 AC 98 ms
10,932 KB
testcase_38 AC 232 ms
23,148 KB
testcase_39 AC 189 ms
20,880 KB
testcase_40 AC 187 ms
27,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <random>
#include <cassert>
using namespace std;
using ll = long long;

ll merge_cnt(vector<ll>& a) {
    ll n = a.size();
    if (n <= 1) { return 0; }

    ll cnt = 0;
    vector<ll> b(a.begin(), a.begin() + n / 2);
    vector<ll> c(a.begin() + n / 2, a.end());

    cnt += merge_cnt(b);
    cnt += merge_cnt(c);

    ll ai = 0, bi = 0, ci = 0;
    while (ai < n) {
        if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
            a[ai++] = b[bi++];
        }
        else {
            cnt += n / 2 - bi;
            a[ai++] = c[ci++];
        }
    }
    return cnt;
}

int main() {
    ll n;
    cin >> n;
    assert(n >= 3 && n <= 100000);
    vector<ll> a(n);
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        assert(a[i] >= 0 && a[i] < (1ll << 30));
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        assert(b[i] >= 0 && b[i] < (1ll << 30));
    }
    if (a[0] != b[0])cout << -1 << endl;
    else {
        vector<ll> aa(n - 1);
        vector<ll> as(n - 1);
        vector<ll> bb(n - 1);
        vector<ll> bs(n - 1);
        for (int i = 0; i < n-1; i++) {
            aa[i] = a[i]^a[i+1];
            as[i] = aa[i];
        }
        for (int i = 0; i < n - 1; i++) {
            bb[i] = b[i] ^ b[i + 1];
            bs[i] = bb[i];
        }
        sort(as.begin(), as.end());
        sort(bs.begin(), bs.end());
        ll fl = 1;
        for (int i = 0; i < n - 1; i++) {
            if (as[i] != bs[i])fl = 0;
        }
        if (fl == 0)cout << -1 << endl;
        else {
            vector<ll> t(n-1);
            map<ll, vector<ll>> m;
            for (int i = 0; i < n - 1; i++) {
                m[bb[i]].push_back(i);
            }
            map<ll, ll>co;
            for (int i = 0; i < n - 1; i++) {
                t[i] = m[aa[i]][co[aa[i]]];
                co[aa[i]]++;
            }
            cout << merge_cnt(t) << endl;
        }
    }
}
0