結果

問題 No.1209 XOR Into You
ユーザー polyomino_24polyomino_24
提出日時 2020-09-06 17:23:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 3,276 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 138,400 KB
実行使用メモリ 16,424 KB
最終ジャッジ日時 2023-08-19 14:08:23
合計ジャッジ時間 12,978 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 65 ms
4,376 KB
testcase_05 AC 65 ms
4,376 KB
testcase_06 AC 81 ms
5,388 KB
testcase_07 AC 154 ms
14,480 KB
testcase_08 AC 133 ms
13,000 KB
testcase_09 AC 130 ms
13,192 KB
testcase_10 AC 105 ms
11,208 KB
testcase_11 AC 132 ms
12,848 KB
testcase_12 AC 118 ms
12,184 KB
testcase_13 AC 142 ms
13,288 KB
testcase_14 AC 115 ms
11,828 KB
testcase_15 AC 123 ms
12,616 KB
testcase_16 AC 119 ms
12,200 KB
testcase_17 AC 116 ms
12,004 KB
testcase_18 AC 176 ms
15,780 KB
testcase_19 AC 120 ms
12,056 KB
testcase_20 AC 140 ms
13,604 KB
testcase_21 AC 109 ms
11,204 KB
testcase_22 AC 125 ms
16,360 KB
testcase_23 AC 124 ms
16,360 KB
testcase_24 AC 123 ms
16,376 KB
testcase_25 AC 182 ms
16,360 KB
testcase_26 AC 180 ms
16,360 KB
testcase_27 AC 186 ms
16,340 KB
testcase_28 AC 180 ms
16,292 KB
testcase_29 AC 181 ms
16,424 KB
testcase_30 AC 180 ms
16,376 KB
testcase_31 AC 102 ms
6,204 KB
testcase_32 AC 103 ms
6,128 KB
testcase_33 AC 111 ms
5,996 KB
testcase_34 AC 109 ms
6,004 KB
testcase_35 AC 102 ms
6,064 KB
testcase_36 AC 102 ms
6,076 KB
testcase_37 AC 75 ms
5,728 KB
testcase_38 AC 150 ms
14,040 KB
testcase_39 AC 133 ms
12,760 KB
testcase_40 AC 128 ms
16,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
template<typename T> class BIT {
private:
    int n;
    vector<T> bit;
public:
    // 0_indexed で i 番目の要素に x を加える
    void add(int i, T x){
        i++;
        while(i < n){
            bit[i] += x, i += i & -i;
        }
    }
    // 0_indexed で [0,i] の要素の和(両閉区間!!)
    T sum(int i){
        i++;
        T s = 0;
        while(i > 0){
            s += bit[i], i -= i & -i;
        }
        return s;
    }
    BIT(){}
    //初期値がすべて0の場合
    BIT(int sz) : n(sz+1), bit(n, 0){}
    BIT(const vector<T>& v) : n((int)v.size()+1), bit(n, 0){
        for(int i = 0; i < n-1; i++){
            add(i,v[i]);
        }
    }
    void print(){
        for(int i = 0; i < n-1; i++){
            cout << sum(i) - sum(i-1) << " ";
        }
        cout << "\n";
    }
    //-1スタート
    void print_sum(){
        for(int i = 0; i < n; i++){
            cout << sum(i-1) << " ";
        }
        cout << "\n";
    }
};
 
// u を昇順にソートするのに必要な交換回数(転倒数) (u は {0,..., n-1} からなる重複を許した長さ n の数列)
long long inv_count(const vector<int>& u)
{
    int n = (int)u.size();
    BIT<int> bt(n);
    long long ans = 0;
    for(int i = 0; i < n; i++){
        ans += i - bt.sum(u[i]);
        bt.add(u[i], 1);
    }
    return ans;
}
 
int main(){
    int n;
    cin >> n;
    vector<int>a(n);
    vector<int>b(n);
    rep(i,n)cin >> a[i];
    rep(i,n)cin >> b[i];
    if(a[0] != b[0] or a[n-1] != b[n-1]){
        cout << -1 << endl;
        return 0;
    }
    vector<int>c(n-1);
    vector<int>d(n-1);
    rep(i,n-1){
        c[i] = a[i] ^ a[i+1];
        d[i] = b[i] ^ b[i+1];
    }
    {
        auto cc = c;
        auto dd = d;
        sort(cc.begin(), cc.end());
        sort(dd.begin(), dd.end());
        if(cc != dd){
            cout << -1 << endl;
            return 0;
        }
    }
    vector<int> e(n-1);
    map<int,vector<int>>mp;
    rep(i,n-1){
        mp[c[i]].push_back(i);
    }
    for(int i = n - 2; i >= 0; i--){
        e[i] = mp[d[i]].back();
        mp[d[i]].pop_back();
    }
    cout << inv_count(e) << endl;
}
0