結果

問題 No.2667 Constrained Permutation
ユーザー shobonvipshobonvip
提出日時 2024-03-08 22:19:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,354 bytes
コンパイル時間 4,530 ms
コンパイル使用メモリ 269,504 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-03-08 22:19:46
合計ジャッジ時間 12,389 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
6,676 KB
testcase_04 AC 3 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 2 ms
6,676 KB
testcase_12 AC 139 ms
8,524 KB
testcase_13 AC 5 ms
6,676 KB
testcase_14 AC 304 ms
18,764 KB
testcase_15 AC 157 ms
13,876 KB
testcase_16 AC 109 ms
10,748 KB
testcase_17 AC 32 ms
6,676 KB
testcase_18 AC 393 ms
19,328 KB
testcase_19 AC 372 ms
17,084 KB
testcase_20 AC 471 ms
18,272 KB
testcase_21 AC 211 ms
17,120 KB
testcase_22 AC 201 ms
16,992 KB
testcase_23 AC 226 ms
16,992 KB
testcase_24 AC 178 ms
10,316 KB
testcase_25 AC 309 ms
16,740 KB
testcase_26 AC 472 ms
18,912 KB
testcase_27 AC 411 ms
19,040 KB
testcase_28 AC 61 ms
11,404 KB
testcase_29 AC 57 ms
10,936 KB
testcase_30 AC 66 ms
12,040 KB
testcase_31 AC 40 ms
8,528 KB
testcase_32 AC 9 ms
6,676 KB
testcase_33 WA -
testcase_34 AC 51 ms
10,500 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 205 ms
13,124 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 51 ms
10,092 KB
testcase_41 AC 82 ms
14,136 KB
testcase_42 AC 150 ms
12,324 KB
testcase_43 AC 282 ms
16,244 KB
testcase_44 AC 114 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]);
		}

		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;
	ll lb_upper = -1e12;
	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