結果

問題 No.2157 崖
ユーザー Kome_soudouKome_soudou
提出日時 2022-12-09 22:37:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,824 ms / 6,000 ms
コード長 1,449 bytes
コンパイル時間 1,549 ms
コンパイル使用メモリ 173,832 KB
実行使用メモリ 19,404 KB
最終ジャッジ日時 2023-08-05 00:18:16
合計ジャッジ時間 31,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1,634 ms
13,840 KB
testcase_14 AC 3,074 ms
17,728 KB
testcase_15 AC 2,298 ms
13,572 KB
testcase_16 AC 2,273 ms
13,500 KB
testcase_17 AC 2,583 ms
15,956 KB
testcase_18 AC 2,631 ms
15,564 KB
testcase_19 AC 2,246 ms
16,064 KB
testcase_20 AC 3,824 ms
19,380 KB
testcase_21 AC 3,696 ms
19,404 KB
testcase_22 AC 2,549 ms
14,832 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int>> d(n, vector<int>(m));
	for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin >> d[i][j];
	for(int i = 0; i < n; i++) sort(d[i].begin(), d[i].end());
	if(n == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	
	int ok = 1000000000;
	int ng = -1;
	int mid;
	while(abs(ok - ng) != 1)
	{
		mid = (ok + ng) / 2;
		vector<vector<int>> dp(n, vector<int>(m));
		for(int i = 0; i < m; i++) dp[0][i] = 1;
		for(int i = 0; i < n - 1; i++)
		{
			for(int j = 0; j < m; j++)
			{
				if(dp[i][j] >= 1)
				{
					int lok = m;
					int lng = -1;
					int rok = -1;
					int rng = m;
					int lmid, rmid;
					while(abs(lok - lng) != 1)
					{
						lmid = (lok + lng) / 2;
						if(d[i][j] <= d[i + 1][lmid]) lok = lmid;
						else lng = lmid;
					}
					while(abs(rok - rng) != 1)
					{
						rmid = (rok + rng) / 2;
						if(d[i + 1][rmid] <= d[i][j] + mid) rok = rmid;
						else rng = rmid;
					}
					
					if(lok != m && rok != -1 && lok <= rok)
					{
						dp[i + 1][lok]++;
						if(rok + 1 < m) dp[i + 1][rok + 1]--;
					}
				}
			}
			for(int j = 1; j < m; j++) dp[i + 1][j] += dp[i + 1][j - 1];
		}
		
		bool pos = false;
		for(int i = 0; i < m; i++)
		{
			if(dp[n - 1][i] >= 1)
			{
				ok = mid;
				pos = true;
				break;
			}
		}
		if(!pos) ng = mid;
	}
	
	if(ok == 1000000000) cout << -1 << endl;
	else cout << ok << endl;
	return 0;
}
0