結果

問題 No.2114 01 Matching
ユーザー bayashikobayashiko
提出日時 2022-10-19 04:34:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 464 ms / 5,000 ms
コード長 3,034 bytes
コンパイル時間 6,339 ms
コンパイル使用メモリ 300,856 KB
実行使用メモリ 56,372 KB
最終ジャッジ日時 2023-08-16 02:48:58
合計ジャッジ時間 21,275 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 200 ms
25,064 KB
testcase_03 AC 64 ms
14,464 KB
testcase_04 AC 88 ms
19,052 KB
testcase_05 AC 91 ms
12,736 KB
testcase_06 AC 62 ms
11,564 KB
testcase_07 AC 61 ms
11,260 KB
testcase_08 AC 289 ms
23,232 KB
testcase_09 AC 213 ms
11,824 KB
testcase_10 AC 194 ms
17,488 KB
testcase_11 AC 464 ms
56,232 KB
testcase_12 AC 435 ms
56,128 KB
testcase_13 AC 260 ms
32,628 KB
testcase_14 AC 358 ms
41,912 KB
testcase_15 AC 346 ms
40,604 KB
testcase_16 AC 345 ms
41,304 KB
testcase_17 AC 157 ms
22,740 KB
testcase_18 AC 175 ms
21,780 KB
testcase_19 AC 244 ms
32,336 KB
testcase_20 AC 439 ms
56,248 KB
testcase_21 AC 298 ms
37,344 KB
testcase_22 AC 164 ms
10,988 KB
testcase_23 AC 222 ms
25,248 KB
testcase_24 AC 64 ms
6,264 KB
testcase_25 AC 74 ms
10,644 KB
testcase_26 AC 437 ms
56,256 KB
testcase_27 AC 440 ms
56,084 KB
testcase_28 AC 223 ms
32,432 KB
testcase_29 AC 449 ms
56,172 KB
testcase_30 AC 243 ms
23,464 KB
testcase_31 AC 417 ms
56,260 KB
testcase_32 AC 283 ms
15,284 KB
testcase_33 AC 206 ms
24,896 KB
testcase_34 AC 60 ms
11,120 KB
testcase_35 AC 135 ms
24,676 KB
testcase_36 AC 438 ms
56,372 KB
testcase_37 AC 218 ms
14,644 KB
testcase_38 AC 63 ms
11,356 KB
testcase_39 AC 130 ms
9,016 KB
testcase_40 AC 133 ms
24,252 KB
testcase_41 AC 251 ms
12,868 KB
testcase_42 AC 211 ms
16,620 KB
testcase_43 AC 151 ms
23,480 KB
testcase_44 AC 334 ms
39,320 KB
testcase_45 AC 236 ms
56,128 KB
testcase_46 AC 88 ms
6,488 KB
testcase_47 AC 426 ms
56,176 KB
testcase_48 AC 423 ms
56,128 KB
testcase_49 AC 249 ms
33,228 KB
testcase_50 AC 97 ms
7,160 KB
testcase_51 AC 58 ms
13,016 KB
testcase_52 AC 356 ms
41,308 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 op(lint a,lint b){
    return min(a,b);
}
lint e(){
    return 1e18;
}
lint mapping(lint f,lint x){
    return f+x;
}
lint composition(lint f,lint g){
    return f+g;
}
lint id(){
    return 0;
}

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));
    lazy_segtree<lint,op,e,lint,mapping,composition,id> dp(N+M+1);
    int zero=N;
    dp.set(zero,0);
    for(auto e:balls){
        lint num=e.fi;
        bool color=e.se;
        if(color){ //赤が来た場合 捨てるのは不可
            dp.apply(0,zero,num);
            dp.apply(zero,N+M+1,-num);
            zero--;
        }
        else{ //青が来た場合
            lint sv=dp.prod(zero,zero+1);
            dp.apply(0,zero+1,-num);
            dp.apply(zero+1,N+M+1,num);
            dp.set(zero+1,min(sv,dp.prod(zero+1,zero+2)));
            zero++;
        }
    }
    return dp.prod(zero,zero+1)/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