結果
問題 | No.2667 Constrained Permutation |
ユーザー |
![]() |
提出日時 | 2024-03-08 22:28:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,383 bytes |
コンパイル時間 | 4,687 ms |
コンパイル使用メモリ | 257,916 KB |
最終ジャッジ日時 | 2025-02-20 02:40:22 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 WA * 4 RE * 9 |
ソースコード
#include<bits/stdc++.h>using namespace std;//* ATCODER#include<atcoder/all>using namespace atcoder;typedef modint998244353 mint;//*//* BOOST MULTIPRECISION#include<boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;//*/typedef long long ll;#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)template <typename T> bool chmin(T &a, const T &b) {if (a <= b) return false;a = b;return true;}template <typename T> bool chmax(T &a, const T &b) {if (a >= b) return false;a = b;return true;}template <typename T> T max(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);return ret;}template <typename T> T min(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);return ret;}template <typename T> T sum(vector<T> &a){T ret = 0;for (int i=0; i<(int)a.size(); i++) ret += a[i];return ret;}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);int n; cin >> n;vector<ll> l(n), r(n);rep(i,0,n){cin >> l[i] >> r[i];}vector<ll> tar = r;sort(tar.begin(), tar.end());auto check_up = [&](ll t) -> bool {vector<ll> g = tar;rep(i,0,n){g[i] -= t;}rep(i,1,n+1){if (i > g[i-1]){return false;}}return true;};auto check_real = [&](ll t) -> bool {vector bucket(n+1, vector<ll>(0));rep(i,0,n){if (max((ll)1, l[i]-t) > n) return false;bucket[max((ll)1, l[i]-t)].push_back(r[i]);}priority_queue<ll,vector<ll>,greater<ll>> pq;rep(i,1,n+1){for (ll j: bucket[i]){pq.push(j);}if(!pq.empty()){ll a = pq.top();if (i > a){return false;}pq.pop();}else{return false;}}return true;};ll ub_upper = 1e12; // NGll lb_upper = -1e12; // OKwhile(ub_upper - lb_upper > 1){ll t = (ub_upper - lb_upper) / 2 + lb_upper;if (check_up(t)) lb_upper = t;else ub_upper = t;}if (!check_real(lb_upper)){assert(false);cout << 0 << '\n';return 0;}ll ub_lower = lb_upper; // OKll lb_lower = -1e12; // NGwhile(ub_lower - lb_lower > 1){ll t = (ub_lower - lb_lower) / 2 + lb_lower;if (check_real(t)) ub_lower = t;else lb_lower = t;}cout << ub_upper - ub_lower << '\n';}