結果
問題 | No.2543 Many Meetings |
ユーザー |
![]() |
提出日時 | 2023-11-24 23:27:41 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 373 ms / 2,000 ms |
コード長 | 2,020 bytes |
コンパイル時間 | 3,414 ms |
コンパイル使用メモリ | 269,620 KB |
実行使用メモリ | 43,924 KB |
最終ジャッジ日時 | 2024-09-26 09:48:59 |
合計ジャッジ時間 | 11,276 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 40 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define FOR(i,m,n) for(int i=(m);i<(n);++i)#define REP(i,n) FOR(i,0,n)using ll = long long;constexpr int INF = 0x3f3f3f3f;constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;constexpr double EPS = 1e-8;constexpr int MOD = 998244353;// constexpr int MOD = 1000000007;constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};template <typename T, typename U>inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }template <typename T, typename U>inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }struct IOSetup {IOSetup() {std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);std::cout << fixed << setprecision(20);}} iosetup;int main() {constexpr int L = 18;int n, k; cin >> n >> k;map<int, int> rl;while (n--) {int l, r; cin >> l >> r;if (const auto it = rl.find(r); it == rl.end()) {rl[r] = l;} else {chmax(it->second, l);}}vector<int> tm;tm.reserve(rl.size() * 2);for (const auto& [r, l] : rl) {tm.emplace_back(l);tm.emplace_back(r);}ranges::sort(tm);tm.erase(unique(tm.begin(), tm.end()), tm.end());const int m = tm.size();vector dp(L, vector(m, m));vector<int> r_min(m);for (const auto& [r, l] : rl) {const int l_ind = distance(tm.begin(), ranges::lower_bound(tm, l));const int r_ind = distance(tm.begin(), ranges::lower_bound(tm, r));chmin(dp[0][l_ind], r_ind);}for (int i = m - 2; i >= 0; --i) chmin(dp[0][i], dp[0][i + 1]);REP(i, L - 1) REP(j, m) {if (dp[i][j] < m) chmin(dp[i + 1][j], dp[i][dp[i][j]]);}int ans = INF;REP(i, m) {int cur = i;for (int bit = 0, j = k; j > 0 && cur < m; ++bit, j /= 2) {if (j & 1) cur = dp[bit][cur];}if (cur >= m) break;chmin(ans, tm[cur] - tm[i]);}cout << (ans == INF ? -1 : ans) << '\n';return 0;}