結果

問題 No.2667 Constrained Permutation
ユーザー 沙耶花
提出日時 2024-03-08 21:37:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,288 ms / 2,000 ms
コード長 1,156 bytes
コンパイル時間 4,297 ms
コンパイル使用メモリ 256,160 KB
最終ジャッジ日時 2025-02-20 02:16:11
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
vector<pair<int,int>> lr;
int check(long long k){
	
	int n = lr.size();
	int cp = 0;
	priority_queue<int,vector<int>,greater<int>> Q;
	rep(i,n){
		int cur = k + i;
		while(cp!=n){
			if(lr[cp].first <= cur){
				Q.push(lr[cp].second);
				cp++;
			}
			else break;
		}
			
		if(Q.size()==0)return -1;
		if(Q.top()<cur)return 1;
		Q.pop();
	}
	return 0;
	
	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin>>n;
	lr.resize(n);
	rep(i,n)cin>>lr[i].first>>lr[i].second;
	sort(lr.begin(),lr.end());
	
	long long ans = 0;
	
	{
		long long ok = -1,ng = 2000000000;
		while(ng-ok>1LL){
			
			long long mid = (ok+ng)/2;
			if(check(mid)<=0)ok = mid;
			else ng =mid;
		}
		ans += ok;
	}
	
	{
		long long ok = -1,ng = 2000000000;
		while(ng-ok>1LL){
			
			long long mid = (ok+ng)/2;
			if(check(mid)<=-1)ok = mid;
			else ng =mid;
		}
		ans -= ok;
	}
	cout<<ans<<endl;
	
	
	return 0;
}
0