結果

問題 No.1597 Matrix Sort
ユーザー snrnsidysnrnsidy
提出日時 2021-08-25 21:45:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 745 ms / 1,500 ms
コード長 1,002 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 200,468 KB
実行使用メモリ 159,336 KB
最終ジャッジ日時 2024-04-28 13:13:03
合計ジャッジ時間 16,219 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 597 ms
159,104 KB
testcase_04 AC 715 ms
159,232 KB
testcase_05 AC 745 ms
159,336 KB
testcase_06 AC 729 ms
159,208 KB
testcase_07 AC 659 ms
159,104 KB
testcase_08 AC 624 ms
97,280 KB
testcase_09 AC 670 ms
97,280 KB
testcase_10 AC 583 ms
81,664 KB
testcase_11 AC 628 ms
81,536 KB
testcase_12 AC 16 ms
5,376 KB
testcase_13 AC 59 ms
19,072 KB
testcase_14 AC 689 ms
152,064 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 62 ms
11,904 KB
testcase_17 AC 665 ms
85,120 KB
testcase_18 AC 684 ms
159,232 KB
testcase_19 AC 657 ms
121,600 KB
testcase_20 AC 643 ms
85,504 KB
testcase_21 AC 645 ms
81,920 KB
testcase_22 AC 641 ms
81,536 KB
testcase_23 AC 621 ms
81,664 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 13 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 667 ms
81,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;

//Matrix Sort
//parital sum + binary search

long long int A[10000005];
long long int B[10000005];
long long int N,K,P,t;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> K >> P;

	for(int i=0;i<N;i++)
	{
		cin >> t;
		t%=P;
		A[t]++;
	}

	for(int i=0;i<N;i++)
	{
		cin >> t;
		t%=P;
		B[t]++;
	}

	for(int i=1;i<P;i++)
	{
		B[i] += B[i-1];
	}
	long long int res = 1e18;
	long long int lo = 0;
	long long int hi = P-1;
	while(lo<=hi)
	{
		long long int mid = (lo + hi)/2;
		long long int cnt = 0;

		for(int i=0;i<P;i++)
		{
			if(i <= mid)
			{
				long long int mul = A[i];
				mul*=(B[mid - i]);
				cnt += mul;
			}
			long long int L = P - i - 1;
			long long int R = min(P-1,P + mid - i);
			if(i > 0)
			{
				long long int mul = A[i];
				mul*=(B[R] - B[L]);
				cnt += mul;
			}
		}

		if(cnt >= K)
		{
			res = min(res,mid);
			hi = mid - 1;
		}
		else
		{
			lo = mid + 1;
		}
	}

	cout << res << '\n';

	return 0;
}
0