結果

問題 No.2114 01 Matching
ユーザー kotatsugame
提出日時 2022-10-28 23:44:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,427 bytes
コンパイル時間 1,394 ms
コンパイル使用メモリ 104,304 KB
実行使用メモリ 53,504 KB
最終ジャッジ日時 2024-07-06 03:02:05
合計ジャッジ時間 17,281 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30 WA * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:10:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   10 | main()
      | ^~~~

ソースコード

diff #

#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());
		sort(R.begin(),R.end());
		multiset<long>LQ;
		queue<long>RQ;
		multiset<pair<long,pair<long,long> > >RS;
		for(int r:R)RQ.push(r);
		for(int b:B)
		{
			while(!RQ.empty()&&RQ.front()<=b)
			{
				LQ.insert(RQ.front());
				RQ.pop();
			}
			while(!RS.empty()&&RS.begin()->first<=b)
			{
				pair<long,pair<long,long> >p=*RS.begin();
				RS.erase(RS.begin());
				auto it=LQ.find(p.second.second);
				if(it!=LQ.end())
				{
					LQ.erase(it);
					LQ.insert(p.first-p.second.first);
				}
			}
			long l=1e15,r=1e15;
			if(!LQ.empty())l=b-*--LQ.end();
			if(!RQ.empty())r=RQ.front()-b;
			if(l<=r)
			{
				ans+=l;
				LQ.erase(--LQ.end());
			}
			else
			{
				ans+=r;
				long p=RQ.front();
				RQ.pop();
				if(l<(long)1e15)
				{
					RS.insert(make_pair(p,make_pair(l-r,*--LQ.end())));
				}
			}
		}
	}
	cout<<ans<<endl;
}
0