結果

問題 No.2667 Constrained Permutation
ユーザー shobonvipshobonvip
提出日時 2024-03-08 22:31:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 2,368 bytes
コンパイル時間 4,458 ms
コンパイル使用メモリ 270,096 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-03-08 22:31:54
合計ジャッジ時間 11,947 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 4 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 123 ms
8,524 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 331 ms
18,764 KB
testcase_15 AC 150 ms
13,876 KB
testcase_16 AC 135 ms
10,748 KB
testcase_17 AC 31 ms
6,676 KB
testcase_18 AC 364 ms
19,328 KB
testcase_19 AC 371 ms
17,084 KB
testcase_20 AC 492 ms
18,272 KB
testcase_21 AC 206 ms
17,120 KB
testcase_22 AC 192 ms
16,992 KB
testcase_23 AC 214 ms
16,992 KB
testcase_24 AC 205 ms
10,316 KB
testcase_25 AC 286 ms
16,740 KB
testcase_26 AC 457 ms
18,912 KB
testcase_27 AC 438 ms
19,040 KB
testcase_28 AC 61 ms
11,404 KB
testcase_29 AC 56 ms
10,936 KB
testcase_30 AC 64 ms
12,040 KB
testcase_31 AC 39 ms
8,528 KB
testcase_32 AC 10 ms
6,676 KB
testcase_33 AC 101 ms
16,348 KB
testcase_34 AC 51 ms
10,500 KB
testcase_35 AC 60 ms
11,556 KB
testcase_36 AC 75 ms
11,112 KB
testcase_37 AC 63 ms
12,140 KB
testcase_38 AC 176 ms
13,124 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 50 ms
10,092 KB
testcase_41 AC 80 ms
14,136 KB
testcase_42 AC 150 ms
12,324 KB
testcase_43 AC 292 ms
16,244 KB
testcase_44 AC 115 ms
8,532 KB
testcase_45 AC 4 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
}

0