結果

問題 No.2114 01 Matching
ユーザー kotatsugamekotatsugame
提出日時 2022-10-28 23:23:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,456 bytes
コンパイル時間 1,448 ms
コンパイル使用メモリ 100,108 KB
実行使用メモリ 53,548 KB
最終ジャッジ日時 2023-09-20 06:38:18
合計ジャッジ時間 23,737 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 369 ms
17,448 KB
testcase_03 AC 101 ms
14,076 KB
testcase_04 AC 147 ms
18,160 KB
testcase_05 WA -
testcase_06 AC 156 ms
5,340 KB
testcase_07 AC 156 ms
5,088 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 294 ms
10,948 KB
testcase_11 AC 627 ms
53,336 KB
testcase_12 AC 617 ms
53,344 KB
testcase_13 WA -
testcase_14 AC 313 ms
29,728 KB
testcase_15 AC 315 ms
29,720 KB
testcase_16 WA -
testcase_17 AC 265 ms
13,240 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 611 ms
53,296 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 113 ms
12,624 KB
testcase_26 AC 611 ms
53,472 KB
testcase_27 AC 606 ms
53,360 KB
testcase_28 AC 301 ms
30,996 KB
testcase_29 AC 609 ms
53,316 KB
testcase_30 AC 155 ms
5,400 KB
testcase_31 AC 609 ms
53,548 KB
testcase_32 WA -
testcase_33 AC 321 ms
17,364 KB
testcase_34 AC 156 ms
4,944 KB
testcase_35 AC 220 ms
23,892 KB
testcase_36 AC 620 ms
53,352 KB
testcase_37 WA -
testcase_38 AC 156 ms
5,216 KB
testcase_39 WA -
testcase_40 AC 216 ms
23,572 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 252 ms
13,236 KB
testcase_44 WA -
testcase_45 AC 358 ms
53,360 KB
testcase_46 WA -
testcase_47 AC 624 ms
53,236 KB
testcase_48 AC 632 ms
53,364 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 153 ms
5,220 KB
testcase_52 AC 309 ms
29,732 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());
		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;
}
0