Submission #1834458


Source Code Expand

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

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream&
operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream&
operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream&
operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const ll MOD = 1000000000+7;

ll factorial(ll x) {
    static array<ll, 100000> memo = {};
    if(memo[x] > 0) return memo[x];
    ll res = 1;
    repeat(i, 1, x + 1) {
        res = (res * i) % MOD;
    }
    return memo[x] = res;
}

int main() {
    ll N; cin >> N;    
    vector<ll> A(N), B(N); cin >> A >> B;
    sort(all(A));
    sort(all(B));

    auto solve = [&] {
        ll ans = 1;
        ll sum = 0;
        rep(i, N) {
            int target = B[i];
            sum += abs(A[i] - B[i]);
            int overlap_num = 1;
            if(target < A[i]) {
                overlap_num = i - (lower_bound(all(A), target) - begin(A));                
            } else if(target > A[i]) {
                overlap_num = i - (lower_bound(all(B), A[i]) - begin(B));                
            } else assert(0);
            // cerr << A[i] << " -> " << B[i] << " : " << overlap_num << endl;
            ans = (ans * (overlap_num + 1)) % MOD;
        }
        return make_pair(ans, sum);
    };

    auto a = solve();
    rep(i, N) {
        A[i] = MOD - A[i];
        B[i] = MOD - B[i];
    }
    sort(all(A));
    sort(all(B));
    auto b = solve();
    debug(a);
    debug(b);
    ll ans = 0;
    ans = a.second < b.second ? a.first : b.first;
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task A - 1D Matching
User cormoran
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2239 Byte
Status AC
Exec Time 105 ms
Memory 1792 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 14
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 63 ms 1152 KB
001.txt AC 23 ms 640 KB
002.txt AC 32 ms 768 KB
003.txt AC 35 ms 768 KB
004.txt AC 91 ms 1664 KB
005.txt AC 102 ms 1792 KB
006.txt AC 103 ms 1792 KB
007.txt AC 102 ms 1792 KB
008.txt AC 103 ms 1792 KB
009.txt AC 102 ms 1792 KB
010.txt AC 97 ms 1792 KB
011.txt AC 105 ms 1792 KB
example0.txt AC 1 ms 256 KB
example1.txt AC 1 ms 256 KB