結果

問題 No.2114 01 Matching
ユーザー bayashikobayashiko
提出日時 2022-10-19 05:09:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,535 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 216,300 KB
実行使用メモリ 56,192 KB
最終ジャッジ日時 2023-09-11 20:26:38
合計ジャッジ時間 15,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 145 ms
18,736 KB
testcase_03 AC 68 ms
14,392 KB
testcase_04 AC 101 ms
18,796 KB
testcase_05 WA -
testcase_06 AC 66 ms
11,472 KB
testcase_07 AC 66 ms
11,156 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 128 ms
14,128 KB
testcase_11 AC 471 ms
56,144 KB
testcase_12 AC 453 ms
56,192 KB
testcase_13 WA -
testcase_14 AC 139 ms
28,088 KB
testcase_15 AC 140 ms
28,228 KB
testcase_16 WA -
testcase_17 AC 111 ms
15,704 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 463 ms
56,104 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 442 ms
56,052 KB
testcase_27 AC 467 ms
56,080 KB
testcase_28 AC 217 ms
32,284 KB
testcase_29 AC 456 ms
56,024 KB
testcase_30 AC 153 ms
15,540 KB
testcase_31 AC 463 ms
56,112 KB
testcase_32 WA -
testcase_33 AC 148 ms
18,876 KB
testcase_34 AC 65 ms
11,088 KB
testcase_35 AC 160 ms
24,744 KB
testcase_36 AC 476 ms
56,100 KB
testcase_37 WA -
testcase_38 AC 67 ms
11,296 KB
testcase_39 WA -
testcase_40 AC 155 ms
24,208 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 113 ms
16,064 KB
testcase_44 WA -
testcase_45 AC 254 ms
56,164 KB
testcase_46 WA -
testcase_47 AC 494 ms
56,100 KB
testcase_48 AC 495 ms
56,104 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 65 ms
13,052 KB
testcase_52 AC 146 ms
28,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if defined(LOCAL)
#include<stdc++.h>
#else
#include<bits/stdc++.h>
#endif
#include<random>
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
//#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define MINF (lint)(2e18)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define flc(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
//#define vec vector<lint>
#define nep(x) next_permutation(all(x))
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=3e5+5;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//vector<int> bucket[MAX_N/1000];
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
/*#include<atcoder/all>
using namespace atcoder;
typedef __int128_t llint;*/

lint solve(vector<lint> R,vector<lint> B,lint K){
    lint ans=0;
    lint N=R.size(),M=B.size();
    multiset<lint> bs;
    rep(i,M) bs.insert(B[i]);
    bs.insert(-1e18);
    bs.insert(1e18);
    rep(i,N){
        auto itr=bs.lower_bound(R[i]);
        if(*itr!=R[i]) itr--;
        auto itr2=bs.upper_bound(R[i]);
        if(abs(R[i]-*itr)<abs(R[i]-*itr2)){
            ans+=abs(R[i]-*itr);
            bs.erase(itr);
        }
        else{
            ans+=abs(R[i]-*itr2);
            bs.erase(itr2);
        }
    }
    return ans/K;
}

int main(void){
    lint N,M,K;
    cin >> N >> M >> K;
    vector<lint> B(N),R(M);
    rep(i,N) cin >> B[i];
    rep(i,M) cin >> R[i];
    if(N<M) swap(N,M),swap(B,R);
    map<int,pair<vector<lint>,vector<lint>>> mp;
    rep(i,N) mp[B[i]%K].fi.push_back(B[i]);
    rep(i,M) mp[R[i]%K].se.push_back(R[i]);
    lint ans=0;
    for(auto e:mp){
        vector<lint> b=e.se.fi;
        vector<lint> r=e.se.se;
        if(b.size()<r.size()){
            cout << -1 << endl;
            return 0;
        }
        if(r.size()) ans+=solve(r,b,K);
    }
    cout << ans << endl;
}
0