結果
問題 |
No.2114 01 Matching
|
ユーザー |
|
提出日時 | 2022-10-28 23:23:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,456 bytes |
コンパイル時間 | 1,239 ms |
コンパイル使用メモリ | 100,728 KB |
実行使用メモリ | 53,504 KB |
最終ジャッジ日時 | 2024-07-06 02:37:52 |
合計ジャッジ時間 | 18,675 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 31 WA * 20 |
コンパイルメッセージ
main.cpp:10:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 10 | main() | ^~~~
ソースコード
#include<iostream> #include<map> #include<vector> #include<algorithm> #include<queue> #include<set> using namespace std; int N,M,K; map<int,pair<vector<int>,vector<int> > >mp; main() { cin>>N>>M>>K; for(int i=0;i<N+M;i++) { int A;cin>>A; if(i<N)mp[A%K].first.push_back(A/K); else mp[A%K].second.push_back(A/K); } long ans=0; for(auto it=mp.begin();it!=mp.end();it++) { vector<int>B,R; B.swap(it->second.first); R.swap(it->second.second); if(N>M)B.swap(R); if(B.size()>R.size()||N==M&&B.size()!=R.size()) { cout<<-1<<endl; return 0; } sort(B.begin(),B.end()); priority_queue<long>LQ; multiset<pair<long,long> >RS,RSS; for(int r:R) { RS.insert(make_pair(r,0)); RSS.insert(make_pair(r+0,0)); } for(int b:B) { while(!RS.empty()&&RS.begin()->first<=b) { pair<long,long>p=*RS.begin(); RS.erase(RS.begin()); RSS.erase(RSS.find(make_pair(p.first+p.second,p.second))); LQ.push(p.first-p.second); } long l=1e15,r=1e15; if(!LQ.empty())l=b-LQ.top(); if(!RSS.empty())r=RSS.begin()->first-b; if(l<=r) { ans+=l; LQ.pop(); } else { ans+=r; pair<long,long>p=*RSS.begin(); RSS.erase(RSS.begin()); RS.erase(RS.find(make_pair(p.first-p.second,p.second))); if(l<(long)1e15) { RS.insert(make_pair(p.first-p.second,l-r+p.second)); RSS.insert(make_pair(p.first+l-r,l-r+p.second)); LQ.pop(); } } } } cout<<ans<<endl; }