結果
| 問題 | 
                            No.2667 Constrained Permutation
                             | 
                    
| コンテスト | |
| ユーザー | 
                             shobonvip
                         | 
                    
| 提出日時 | 2024-03-08 22:31:39 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 634 ms / 2,000 ms | 
| コード長 | 2,368 bytes | 
| コンパイル時間 | 4,551 ms | 
| コンパイル使用メモリ | 258,212 KB | 
| 最終ジャッジ日時 | 2025-02-20 02:42:11 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 46 | 
ソースコード
#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]-t);
		}
		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; // NG
	ll lb_upper = -1e12; // OK
	while(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)){
		cout << 0 << '\n';
		return 0;
	}
	ll ub_lower = lb_upper; // OK
	ll lb_lower = -1e12; // NG
	while(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';
}
            
            
            
        
            
shobonvip