結果

問題 No.2114 01 Matching
ユーザー bayashikobayashiko
提出日時 2022-10-19 04:34:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 429 ms / 5,000 ms
コード長 3,034 bytes
コンパイル時間 5,827 ms
コンパイル使用メモリ 305,776 KB
実行使用メモリ 56,448 KB
最終ジャッジ日時 2024-05-03 12:27:23
合計ジャッジ時間 20,422 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 212 ms
25,200 KB
testcase_03 AC 54 ms
14,592 KB
testcase_04 AC 81 ms
18,944 KB
testcase_05 AC 85 ms
12,904 KB
testcase_06 AC 56 ms
11,848 KB
testcase_07 AC 56 ms
11,572 KB
testcase_08 AC 270 ms
23,176 KB
testcase_09 AC 204 ms
11,852 KB
testcase_10 AC 183 ms
17,528 KB
testcase_11 AC 429 ms
56,320 KB
testcase_12 AC 411 ms
56,448 KB
testcase_13 AC 259 ms
32,768 KB
testcase_14 AC 351 ms
41,868 KB
testcase_15 AC 347 ms
40,412 KB
testcase_16 AC 367 ms
41,876 KB
testcase_17 AC 151 ms
22,404 KB
testcase_18 AC 168 ms
22,592 KB
testcase_19 AC 238 ms
32,776 KB
testcase_20 AC 383 ms
56,448 KB
testcase_21 AC 289 ms
37,180 KB
testcase_22 AC 159 ms
11,204 KB
testcase_23 AC 215 ms
25,340 KB
testcase_24 AC 69 ms
6,944 KB
testcase_25 AC 72 ms
10,628 KB
testcase_26 AC 384 ms
56,448 KB
testcase_27 AC 392 ms
56,448 KB
testcase_28 AC 176 ms
32,640 KB
testcase_29 AC 375 ms
56,320 KB
testcase_30 AC 242 ms
23,220 KB
testcase_31 AC 384 ms
56,448 KB
testcase_32 AC 273 ms
15,220 KB
testcase_33 AC 195 ms
25,080 KB
testcase_34 AC 55 ms
11,348 KB
testcase_35 AC 126 ms
24,960 KB
testcase_36 AC 389 ms
56,448 KB
testcase_37 AC 220 ms
14,700 KB
testcase_38 AC 60 ms
11,452 KB
testcase_39 AC 129 ms
8,956 KB
testcase_40 AC 118 ms
24,576 KB
testcase_41 AC 250 ms
12,920 KB
testcase_42 AC 201 ms
16,704 KB
testcase_43 AC 149 ms
23,380 KB
testcase_44 AC 327 ms
40,180 KB
testcase_45 AC 210 ms
56,320 KB
testcase_46 AC 84 ms
6,940 KB
testcase_47 AC 390 ms
56,320 KB
testcase_48 AC 393 ms
56,448 KB
testcase_49 AC 238 ms
32,664 KB
testcase_50 AC 94 ms
7,120 KB
testcase_51 AC 57 ms
13,380 KB
testcase_52 AC 351 ms
40,676 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