結果
| 問題 |
No.2667 Constrained Permutation
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2024-03-09 09:53:45 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 387 ms / 2,000 ms |
| コード長 | 2,130 bytes |
| コンパイル時間 | 1,229 ms |
| コンパイル使用メモリ | 114,116 KB |
| 実行使用メモリ | 13,628 KB |
| 最終ジャッジ日時 | 2024-09-29 21:12:16 |
| 合計ジャッジ時間 | 6,994 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 46 |
ソースコード
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); }
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
int N;
cin >> N;
vector<pair<int, int>> LR(N);
for (auto &[l, r] : LR) cin >> l >> r;
sort(LR.begin(), LR.end());
int all_l_max = -1, all_r_min = 1 << 30;
for (auto [l, r] : LR) {
chmax(all_l_max, l);
chmin(all_r_min, r);
}
auto find_feasible = [&]() -> int {
int now = -1;
for (auto [L, R] : LR) {
chmax(now, (int)L - 1);
++now;
}
return now - (N - 1);
};
auto check = [&](int mid) -> bool {
// [mid, mid + 1, ..., mid + N - 1]
vector<vector<int>> dp(N);
for (auto [l, r] : LR) {
int left = max(l, mid) - mid, right = min(r, mid + N - 1) - mid;
if (left <= right) dp.at(left).push_back(right);
}
priority_queue<int, vector<int>, greater<int>> pq;
REP(i, N) {
for (auto r : dp.at(i)) pq.push(r);
while (pq.size() and pq.top() < i) pq.pop();
if (pq.empty()) return false;
pq.pop();
}
return true;
};
auto binsearch = [&](int ok, int ng) -> int {
while (abs(ok - ng) > 1) {
const int mid = floor_div(ok + ng, 2);
(check(mid) ? ok : ng) = mid;
}
return ok;
};
const int feasible = find_feasible();
if (!check(feasible)) {
puts("0");
} else {
int lo = binsearch(feasible, all_l_max - N);
int hi = binsearch(feasible, all_r_min + 1);
cout << abs(hi - lo) + 1 << '\n';
}
}
hitonanode