結果

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

ソースコード

diff #

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
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);
		else mp[A%K].second.push_back(A);
	}
	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());
		queue<int>RQ;
		for(int r:R)RQ.push(r);
		priority_queue<int>Q;
		for(int b:B)
		{
			while(!RQ.empty()&&RQ.front()<=b)
			{
				Q.push(RQ.front());
				RQ.pop();
			}
			if(RQ.empty())
			{
				ans+=(b-Q.top())/K;
				Q.pop();
			}
			else if(Q.empty())
			{
				ans+=(RQ.front()-b)/K;
				RQ.pop();
			}
			else
			{
				int l=b-Q.top(),r=RQ.front()-b;
				if(l<=r)
				{
					ans+=l/K;
					Q.pop();
				}
				else
				{
					ans+=r/K;
					Q.push(RQ.front()-(l-r));
					RQ.pop();
				}
			}
		}
	}
	cout<<ans<<endl;
}
0