結果

問題 No.2114 01 Matching
ユーザー planesplanes
提出日時 2022-11-03 17:50:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,377 bytes
コンパイル時間 4,210 ms
コンパイル使用メモリ 191,044 KB
実行使用メモリ 64,904 KB
最終ジャッジ日時 2023-09-25 02:07:15
合計ジャッジ時間 23,944 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 256 ms
45,604 KB
testcase_03 AC 87 ms
15,988 KB
testcase_04 AC 126 ms
21,020 KB
testcase_05 WA -
testcase_06 AC 247 ms
39,604 KB
testcase_07 AC 217 ms
29,660 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 215 ms
29,012 KB
testcase_11 AC 449 ms
62,400 KB
testcase_12 AC 454 ms
62,672 KB
testcase_13 WA -
testcase_14 AC 235 ms
64,180 KB
testcase_15 AC 234 ms
63,984 KB
testcase_16 WA -
testcase_17 AC 213 ms
29,644 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 447 ms
62,588 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 460 ms
62,416 KB
testcase_27 AC 466 ms
62,408 KB
testcase_28 AC 251 ms
35,940 KB
testcase_29 AC 451 ms
62,404 KB
testcase_30 AC 271 ms
40,116 KB
testcase_31 AC 460 ms
62,412 KB
testcase_32 WA -
testcase_33 AC 258 ms
45,416 KB
testcase_34 AC 213 ms
37,672 KB
testcase_35 AC 187 ms
27,732 KB
testcase_36 AC 465 ms
62,544 KB
testcase_37 WA -
testcase_38 AC 232 ms
35,868 KB
testcase_39 WA -
testcase_40 AC 183 ms
27,316 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 250 ms
39,620 KB
testcase_44 WA -
testcase_45 AC 417 ms
62,332 KB
testcase_46 WA -
testcase_47 AC 486 ms
62,200 KB
testcase_48 AC 456 ms
62,308 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 212 ms
30,464 KB
testcase_52 AC 236 ms
62,496 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]];
  }
}



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