結果

問題 No.2114 01 Matching
ユーザー bayashikobayashiko
提出日時 2022-10-19 03:02:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,303 bytes
コンパイル時間 3,059 ms
コンパイル使用メモリ 222,660 KB
実行使用メモリ 8,776 KB
最終ジャッジ日時 2023-09-11 20:16:51
合計ジャッジ時間 10,682 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
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> R,vector<lint> B,lint K){
    lint N=R.size(),M=B.size();
    vector<pair<lint,bool>> balls;
    rep(i,N) balls.push_back({R[i],1});
    rep(i,M) balls.push_back({B[i],0});
    sort(all(balls));
    vector<lint> dp(N+M+1);
    rep(i,N+M+1) dp[i]=1e18;
    int zero=N;
    dp[zero]=0;
    for(auto e:balls){
        lint num=e.fi;
        bool color=e.se;
        vector<lint> ndp(N+M+1);
        rep(i,N+M+1) ndp[i]=1e18;
        if(color){ //赤が来た場合 捨てるのは不可
            rep(i,N+M+1){
                if(dp[i]>=1e18) continue;
                if(i<zero){ //青保留中
                    chmin(ndp[i+1],dp[i]+num);
                }
                else{
                    chmin(ndp[i+1],dp[i]-num);
                }
            }
        }
        else{ //青が来た場合
            rep(i,N+M+1){
                if(dp[i]>=1e18) continue;
                if(i<=zero){
                    chmin(ndp[i-1],dp[i]-num);
                }
                else{ //赤保留中
                    chmin(ndp[i-1],dp[i]+num);
                }
                if(i==zero){ //両方余ってない場合のみ捨てる手がうてる
                    chmin(ndp[i],dp[i]);
                }
            }
        }
        dp=ndp;
    }
    return dp[zero];
}

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