結果

問題 No.2667 Constrained Permutation
ユーザー shobonvipshobonvip
提出日時 2024-03-08 22:28:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,383 bytes
コンパイル時間 4,768 ms
コンパイル使用メモリ 269,484 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-03-08 22:29:04
合計ジャッジ時間 14,309 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 RE -
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 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 3 ms
6,676 KB
testcase_12 AC 113 ms
8,524 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 307 ms
18,764 KB
testcase_15 AC 159 ms
13,876 KB
testcase_16 AC 130 ms
10,748 KB
testcase_17 AC 32 ms
6,676 KB
testcase_18 AC 366 ms
19,328 KB
testcase_19 AC 400 ms
17,084 KB
testcase_20 AC 517 ms
18,272 KB
testcase_21 AC 237 ms
17,120 KB
testcase_22 AC 255 ms
16,992 KB
testcase_23 AC 254 ms
16,992 KB
testcase_24 AC 183 ms
10,316 KB
testcase_25 AC 290 ms
16,740 KB
testcase_26 AC 481 ms
18,912 KB
testcase_27 AC 385 ms
19,040 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 WA -
testcase_34 RE -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 222 ms
13,124 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 151 ms
12,324 KB
testcase_43 AC 272 ms
16,244 KB
testcase_44 AC 120 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; // 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)){
		assert(false);
		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