結果

問題 No.2543 Many Meetings
ユーザー kwm_tkwm_t
提出日時 2023-12-30 01:36:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 216,940 KB
実行使用メモリ 29,036 KB
最終ジャッジ日時 2023-12-30 01:36:20
合計ジャッジ時間 6,711 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,676 KB
testcase_02 WA -
testcase_03 AC 67 ms
6,676 KB
testcase_04 AC 66 ms
6,676 KB
testcase_05 AC 67 ms
6,676 KB
testcase_06 AC 67 ms
6,676 KB
testcase_07 AC 67 ms
6,676 KB
testcase_08 AC 139 ms
29,036 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 137 ms
29,036 KB
testcase_12 AC 138 ms
29,036 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 61 ms
14,564 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 104 ms
22,592 KB
testcase_23 WA -
testcase_24 AC 11 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 WA -
testcase_27 AC 2 ms
6,676 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 28 ms
9,776 KB
testcase_31 AC 23 ms
8,648 KB
testcase_32 AC 75 ms
20,900 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 WA -
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 WA -
testcase_40 AC 1 ms
6,676 KB
testcase_41 AC 1 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
bool chk(const vector<P> &v, int k) {
	int pre = 0;
	int cnt = 0;
	rep2(i, 1, v.size()) {
		if (v[pre].second <= v[i].first) {
			cnt++;
			pre = i;
		}
	}
	return cnt >= k;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k; cin >> n >> k;
	vector<P>d(n);
	rep(i, n)cin >> d[i].first >> d[i].second;
	sort(all(d), [](const P x, const P &y) {return x.second < y.second; });
	if (!chk(d, k)) {
		cout << -1 << endl;
		return 0;
	}
	vector<int>next(n, -1);// todo 高速化
	{
		struct SubD {
			int l, r, idx;
			SubD() {};
			SubD(int l, int r, int idx) : l(l), r(r), idx(idx) {};
		};
		vector<SubD>subD(n);
		rep(i, n) {
			subD[i] = SubD(d[i].first, d[i].second, i);
		}
		int mn = INF;
		int idx = -1;
		sort(all(subD), [](const SubD x, const SubD &y) {return x.l < y.l; });
		rrep(i, n) {
			while (!subD.empty() && d[i].second <= subD.back().l) {
				auto top = subD.back();
				if (chmin(mn, top.r))idx = top.idx;
				subD.pop_back();
			}
			next[i] = idx;
		}
	}
	/*rep(i, n) {
		rep(j, n) {
			if (d[i].second > d[j].first)continue;
			if (-1 == next[i])next[i] = j;
			else if (d[j].second < d[next[i]].second)next[i] = j;
		}
	}*/
	// dp[i][j]:=iから初めて2^j個数選ぶ時の最後に選んだindex
	vector dp(n, vector<int>(20, -1));
	rep(i, n)dp[i][0] = i;
	rep2(i, 1, 20) {
		rep(j, n) {
			int x0 = dp[j][i - 1];
			if (-1 == x0)continue;
			int x1 = next[x0];
			if (-1 == x1)continue;
			int x2 = dp[x1][i - 1];
			dp[j][i] = x2;
		}
	}
	int ans = INF;
	rep(i, n) {
		int ck = k;
		int ci = i;
		bool chk = true;
		rep(j, 20) {
			if (1 == ck % 2) {
				if (j != 0)ci = next[ci];
				if (-1 == ci) {
					chk = false;
					break;
				}
				ci = dp[ci][j];
				if (-1 == ci) {
					chk = false;
					break;

				}
			}
			ck /= 2;
		}
		if (chk) {
			int val = d[ci].second - d[i].first;
			chmin(ans, val);
		}
	}
	cout << ans << endl;
	return 0;
}
0