結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
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 66 ms
6,676 KB
testcase_07 AC 67 ms
6,676 KB
testcase_08 AC 164 ms
31,372 KB
testcase_09 AC 138 ms
31,372 KB
testcase_10 AC 136 ms
31,372 KB
testcase_11 AC 136 ms
31,372 KB
testcase_12 AC 135 ms
31,372 KB
testcase_13 AC 104 ms
25,508 KB
testcase_14 AC 60 ms
15,872 KB
testcase_15 AC 59 ms
15,616 KB
testcase_16 AC 87 ms
21,716 KB
testcase_17 AC 102 ms
24,356 KB
testcase_18 AC 112 ms
27,036 KB
testcase_19 AC 101 ms
24,816 KB
testcase_20 AC 100 ms
25,024 KB
testcase_21 AC 118 ms
28,712 KB
testcase_22 AC 98 ms
24,396 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 80 ms
23,980 KB
testcase_29 AC 27 ms
10,240 KB
testcase_30 AC 27 ms
10,368 KB
testcase_31 AC 22 ms
9,088 KB
testcase_32 AC 73 ms
22,448 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 AC 1 ms
6,676 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 1 ms
6,676 KB
testcase_39 AC 1 ms
6,676 KB
testcase_40 AC 1 ms
6,676 KB
testcase_41 AC 1 ms
6,676 KB
testcase_42 AC 1 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;
}
vector<int> calcNext(const vector<P> &d,int n) {
	vector<int>next(n, -1);
	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;
	}
	return next;
}
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;
	}
	// dp[i][j]:=iから初めて2^j個数選ぶ時の最後に選んだindex
	vector dp(n, vector<int>(20, -1));
	auto next = calcNext(d, n);
	rep(i, n)dp[i][0] = next[i];
	rep2(i, 1, 20) {
		rep(j, n) {
			int x0 = dp[j][i - 1];
			if (-1 == x0)continue;
			int x1 = dp[x0][i - 1];
			dp[j][i] = x1;
		}
	}
	k--;
	int ans = INF;
	rep(i, n) {
		int ck = k;
		int ci = i;
		bool chk = true;
		rep(j, 20) {
			if (1 == ck % 2) {
				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