結果

問題 No.2114 01 Matching
ユーザー bayashikobayashiko
提出日時 2022-10-01 19:48:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,295 bytes
コンパイル時間 4,091 ms
コンパイル使用メモリ 212,120 KB
実行使用メモリ 814,004 KB
最終ジャッジ日時 2023-09-11 20:14:33
合計ジャッジ時間 23,696 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 RE -
testcase_03 AC 73 ms
14,320 KB
testcase_04 AC 110 ms
18,880 KB
testcase_05 RE -
testcase_06 AC 67 ms
11,568 KB
testcase_07 AC 67 ms
11,200 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 526 ms
56,120 KB
testcase_12 AC 518 ms
56,124 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 516 ms
56,096 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

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> B,vector<lint> R,lint K){
    lint N=B.size(),M=R.size();
    lint dp[N+1][M+1];
    rep(i,N+1) rep(j,M+1) dp[i][j]=1e18;
    dp[0][0]=0;
    rep(i,N) rep(j,M+1){
        chmin(dp[i+1][j],dp[i][j]);
        if(j!=M) chmin(dp[i+1][j+1],dp[i][j]+abs(B[i]-R[j])/K);
    }
    return dp[N][M];
}

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;
        }
        ans+=solve(b,r,K);
    }
    cout << ans << endl;
}
0