結果

問題 No.2114 01 Matching
ユーザー kotatsugamekotatsugame
提出日時 2022-10-28 23:44:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,427 bytes
コンパイル時間 1,187 ms
コンパイル使用メモリ 103,112 KB
実行使用メモリ 53,472 KB
最終ジャッジ日時 2023-09-20 07:03:32
合計ジャッジ時間 16,899 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 181 ms
5,368 KB
testcase_03 AC 101 ms
13,912 KB
testcase_04 AC 148 ms
18,216 KB
testcase_05 WA -
testcase_06 AC 155 ms
5,476 KB
testcase_07 AC 156 ms
5,052 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 183 ms
5,148 KB
testcase_11 AC 616 ms
53,292 KB
testcase_12 AC 605 ms
53,412 KB
testcase_13 WA -
testcase_14 AC 157 ms
6,260 KB
testcase_15 AC 157 ms
6,108 KB
testcase_16 WA -
testcase_17 AC 174 ms
5,304 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 628 ms
53,308 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 609 ms
53,372 KB
testcase_27 AC 611 ms
53,372 KB
testcase_28 AC 302 ms
30,960 KB
testcase_29 AC 617 ms
53,364 KB
testcase_30 AC 156 ms
5,304 KB
testcase_31 AC 617 ms
53,300 KB
testcase_32 WA -
testcase_33 AC 181 ms
5,568 KB
testcase_34 AC 156 ms
5,268 KB
testcase_35 AC 210 ms
23,788 KB
testcase_36 AC 614 ms
53,364 KB
testcase_37 WA -
testcase_38 AC 156 ms
5,364 KB
testcase_39 WA -
testcase_40 AC 209 ms
23,304 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 174 ms
5,292 KB
testcase_44 WA -
testcase_45 AC 357 ms
53,472 KB
testcase_46 WA -
testcase_47 AC 594 ms
53,428 KB
testcase_48 AC 600 ms
53,296 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 155 ms
5,268 KB
testcase_52 AC 157 ms
6,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:10:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-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