結果

問題 No.2667 Constrained Permutation
ユーザー inksamuraiinksamurai
提出日時 2024-03-10 18:41:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 213,884 KB
実行使用メモリ 17,264 KB
最終ジャッジ日時 2024-03-10 18:41:21
合計ジャッジ時間 10,909 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
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 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 68 ms
7,620 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 208 ms
17,096 KB
testcase_15 AC 118 ms
12,584 KB
testcase_16 AC 105 ms
9,856 KB
testcase_17 AC 23 ms
6,676 KB
testcase_18 AC 282 ms
17,136 KB
testcase_19 AC 309 ms
15,472 KB
testcase_20 AC 299 ms
16,496 KB
testcase_21 AC 147 ms
15,412 KB
testcase_22 AC 180 ms
15,344 KB
testcase_23 AC 171 ms
15,412 KB
testcase_24 AC 86 ms
9,508 KB
testcase_25 AC 180 ms
15,288 KB
testcase_26 AC 224 ms
17,264 KB
testcase_27 AC 192 ms
17,264 KB
testcase_28 AC 52 ms
9,060 KB
testcase_29 AC 49 ms
8,756 KB
testcase_30 AC 55 ms
9,508 KB
testcase_31 AC 34 ms
7,040 KB
testcase_32 AC 9 ms
6,676 KB
testcase_33 AC 84 ms
14,796 KB
testcase_34 AC 46 ms
9,600 KB
testcase_35 AC 55 ms
10,624 KB
testcase_36 AC 53 ms
10,240 KB
testcase_37 AC 58 ms
11,136 KB
testcase_38 AC 185 ms
11,840 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 45 ms
9,216 KB
testcase_41 AC 70 ms
12,788 KB
testcase_42 AC 106 ms
10,380 KB
testcase_43 AC 172 ms
13,932 KB
testcase_44 AC 175 ms
8,132 KB
testcase_45 AC 5 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int) a.size())
#define vec(...) vector<__VA_ARGS__>
#define _3TpP2FO ios::sync_with_stdio(0),cin.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

const int inf=1e9;

void slv(){
	int n;
	cin>>n;

	vec(pii) a(n);
	rep(i,n){
		cin>>a[i].fi>>a[i].se;
	}
	sort(all(a));

	int lk=-inf;
	rep(i,n){
		auto [l,r]=a[i];
		// l - i - 1 <= k
		lk=max(lk,l-i-1);
	}
	//print(lk);

	auto f=[&](int k)->int{
		vec(vi) adqry(n+1);
		rep(i,n){
			auto [l,r]=a[i];
			l-=k,r-=k;
			l=max(l,1ll);
			r=min(r,n);
			if(l>r) return 0;
			adqry[l].pb(r);
		}
		priority_queue<int,vec(int),greater<int>> pq;
		rng(l,1,n+1){
			for(auto r:adqry[l]) pq.push(r);
			while(sz(pq) and pq.top()<l) pq.pop();
			if(!sz(pq)) return 0;
			pq.pop();
		}
		return 1;
	};
	if(!f(lk)){
		print(0);
		return;
	}

	int l=lk,r=inf;
	int rk=lk;
	while(l<=r){
		int m=(l+r)/2;
		if(f(m)) rk=m,l=m+1;
		else r=m-1;
	}
	print(rk-lk+1);
}

signed main(){
_3TpP2FO;
	slv();
}
0