結果
問題 | No.2543 Many Meetings |
ユーザー |
![]() |
提出日時 | 2023-12-30 01:22:54 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,827 bytes |
コンパイル時間 | 2,429 ms |
コンパイル使用メモリ | 207,880 KB |
最終ジャッジ日時 | 2025-02-18 15:22:56 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 WA * 10 |
ソースコード
#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個数選ぶ時の最後に選んだindexvector 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 (i != ci)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 << i << " " << ci << endl;}}/*rep(i, n) {rep(j, 5) {cout << dp[i][j] << " ";}cout << endl;}*/cout << ans << endl;return 0;}