結果

問題 No.2114 01 Matching
ユーザー planesplanes
提出日時 2022-11-03 19:39:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 5,000 ms
コード長 2,388 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 192,752 KB
実行使用メモリ 65,436 KB
最終ジャッジ日時 2023-08-16 02:53:03
合計ジャッジ時間 16,760 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 238 ms
45,328 KB
testcase_03 AC 78 ms
15,668 KB
testcase_04 AC 112 ms
20,984 KB
testcase_05 AC 68 ms
18,376 KB
testcase_06 AC 236 ms
40,408 KB
testcase_07 AC 196 ms
29,496 KB
testcase_08 AC 211 ms
39,388 KB
testcase_09 AC 161 ms
25,040 KB
testcase_10 AC 201 ms
28,872 KB
testcase_11 AC 420 ms
62,400 KB
testcase_12 AC 424 ms
62,516 KB
testcase_13 AC 172 ms
42,116 KB
testcase_14 AC 212 ms
65,156 KB
testcase_15 AC 214 ms
65,436 KB
testcase_16 AC 278 ms
64,484 KB
testcase_17 AC 197 ms
29,568 KB
testcase_18 AC 125 ms
32,644 KB
testcase_19 AC 180 ms
42,352 KB
testcase_20 AC 417 ms
62,384 KB
testcase_21 AC 221 ms
52,160 KB
testcase_22 AC 141 ms
21,056 KB
testcase_23 AC 160 ms
40,796 KB
testcase_24 AC 46 ms
9,928 KB
testcase_25 AC 44 ms
12,284 KB
testcase_26 AC 422 ms
62,388 KB
testcase_27 AC 430 ms
62,252 KB
testcase_28 AC 229 ms
35,852 KB
testcase_29 AC 421 ms
62,568 KB
testcase_30 AC 248 ms
42,036 KB
testcase_31 AC 419 ms
62,256 KB
testcase_32 AC 201 ms
30,916 KB
testcase_33 AC 237 ms
45,440 KB
testcase_34 AC 192 ms
37,808 KB
testcase_35 AC 172 ms
27,684 KB
testcase_36 AC 433 ms
62,304 KB
testcase_37 AC 157 ms
27,968 KB
testcase_38 AC 211 ms
35,956 KB
testcase_39 AC 83 ms
15,004 KB
testcase_40 AC 160 ms
27,308 KB
testcase_41 AC 191 ms
28,260 KB
testcase_42 AC 149 ms
29,092 KB
testcase_43 AC 222 ms
39,652 KB
testcase_44 AC 256 ms
60,900 KB
testcase_45 AC 385 ms
62,136 KB
testcase_46 AC 62 ms
10,788 KB
testcase_47 AC 433 ms
62,248 KB
testcase_48 AC 414 ms
62,296 KB
testcase_49 AC 172 ms
42,952 KB
testcase_50 AC 75 ms
13,136 KB
testcase_51 AC 198 ms
30,352 KB
testcase_52 AC 214 ms
63,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

  #include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

ll INF=2e18;




int main() {

   

  ll N,M,K;cin>>N>>M>>K;
  vector<ll> B(N);
  vector<ll> A(M);
  for(ll i=0;i<N;i++) cin>>B[i];
  for(ll i=0;i<M;i++) cin>>A[i];

sort(all(B));
sort(all(A));

if(N<M) {
  swap(N,M);
vector<ll> C=B;
B=A;
A=C;
}


 vector<vector<ll>> b(N+M,vector<ll> (0));
 vector<vector<ll>> r(N+M,vector<ll> (0));

 ll ind=0;
 map<ll,ll> S;
 for(ll i=0;i<N;i++) {
  if(!S.count(B[i]%K)) {
    S[B[i]%K]=ind;
    ind++;
  }
    b[S[B[i]%K]].push_back(B[i]/K);
  }

ll ans=0;


for(ll i=0;i<M;i++) {
  if(!S.count(A[i]%K)) {
    S[A[i]%K]=ind;
    ind++;
  }
  r[S[A[i]%K]].push_back(A[i]/K);
}





for(ll w=0;w<N+M;w++) {
  if(b[w].size()<r[w].size()) {
    cout<<-1<<endl;
    return 0;
  }
  if(r[w].size()==0) continue;

  ll x=b[w].size();
  ll y=r[w].size();



ll now=y-x;
vector<pair<ll,ll>> vec(0);
for(auto p:b[w]) vec.push_back(make_pair(p,0));
for(auto p:r[w]) vec.push_back(make_pair(p,1));

vector<ll> R(x+y+1,0);
vector<ll> Rind(0);

sort(all(vec));




for(ll i=0;i<x+y;i++) {
  R[i]=now;
  if(vec[i].second==1){
    Rind.push_back(i);
     now--;
  }
  else now++;
}



vector<ll> d(x+y+1,0);

for(ll i=x+y-1;i>=0;i--) {
  d[i]=d[i+1];
  if(vec[i].second==1) d[i]-=vec[i].first;
  else d[i]+=vec[i].first;
}


ll g=max(x,y);
vector<vector<ll>> dp(y,vector<ll> (0));
vector<pair<ll,ll>> note(x+y+g+1,{INF,INF});

ll ri=-1;
now=y-x;




for(ll j=0;j<x+y;j++) {


note[now+g]=make_pair(j,ri);


  if(vec[j].second==1) {
    ll fb=0;
    for(ll h=j+1;h<x+y;h++) {
      if(vec[h].second==1) break;
      fb++;
    }

dp[ri+1]=vector<ll> (fb+1,INF);


ll k=R[j+1];


if(note[k+g].second!=INF) {
  dp[ri+1][0]=d[j+1]-d[note[k+g].first];
  if(note[k+g].second!=-1) {
    dp[ri+1][0]+=dp[note[k+g].second][note[k+g].first-Rind[note[k+g].second]-1];
  }
}



for(ll h=1;h<=fb;h++) {
   k=R[j+h+1];

   if(note[k+g].second!=INF) {
    dp[ri+1][h]=d[note[k+g].first]-d[j+h+1];


    if(note[k+g].second!=-1) {
      dp[ri+1][h]+=dp[note[k+g].second].back();
    }
   }


   dp[ri+1][h]=min(dp[ri+1][h],dp[ri+1][h-1]);
}


}




if(vec[j].second==1) {
  now--;
  ri++;

  }
else now++;
}



ans+=dp[y-1].back();

}







cout<<ans<<endl;
}




  

























    
0