結果

問題 No.2667 Constrained Permutation
ユーザー 👑 kmjpkmjp
提出日時 2024-03-08 23:37:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 1,656 bytes
コンパイル時間 2,848 ms
コンパイル使用メモリ 212,224 KB
実行使用メモリ 19,732 KB
最終ジャッジ日時 2024-03-08 23:37:49
合計ジャッジ時間 7,641 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,276 KB
testcase_01 AC 4 ms
8,276 KB
testcase_02 AC 4 ms
8,276 KB
testcase_03 AC 4 ms
8,276 KB
testcase_04 AC 5 ms
8,276 KB
testcase_05 AC 4 ms
8,276 KB
testcase_06 AC 5 ms
8,276 KB
testcase_07 AC 4 ms
8,276 KB
testcase_08 AC 5 ms
8,276 KB
testcase_09 AC 5 ms
8,276 KB
testcase_10 AC 4 ms
8,276 KB
testcase_11 AC 5 ms
8,276 KB
testcase_12 AC 67 ms
11,964 KB
testcase_13 AC 5 ms
8,404 KB
testcase_14 AC 205 ms
19,052 KB
testcase_15 AC 99 ms
12,628 KB
testcase_16 AC 66 ms
11,220 KB
testcase_17 AC 23 ms
9,172 KB
testcase_18 AC 222 ms
19,732 KB
testcase_19 AC 218 ms
15,572 KB
testcase_20 AC 257 ms
17,568 KB
testcase_21 AC 114 ms
13,908 KB
testcase_22 AC 122 ms
13,908 KB
testcase_23 AC 126 ms
13,908 KB
testcase_24 AC 74 ms
13,060 KB
testcase_25 AC 145 ms
17,632 KB
testcase_26 AC 185 ms
19,708 KB
testcase_27 AC 195 ms
19,708 KB
testcase_28 AC 58 ms
9,428 KB
testcase_29 AC 56 ms
9,300 KB
testcase_30 AC 60 ms
9,428 KB
testcase_31 AC 39 ms
9,044 KB
testcase_32 AC 11 ms
8,404 KB
testcase_33 AC 103 ms
13,652 KB
testcase_34 AC 51 ms
11,220 KB
testcase_35 AC 60 ms
11,604 KB
testcase_36 AC 59 ms
11,476 KB
testcase_37 AC 61 ms
11,988 KB
testcase_38 AC 104 ms
11,860 KB
testcase_39 AC 6 ms
8,276 KB
testcase_40 AC 50 ms
10,708 KB
testcase_41 AC 79 ms
12,244 KB
testcase_42 AC 109 ms
15,444 KB
testcase_43 AC 161 ms
18,516 KB
testcase_44 AC 67 ms
11,960 KB
testcase_45 AC 6 ms
8,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
pair<int,int> A[202020];

vector<int> add[202020];

int can(int v) {
	int i;
	FOR(i,N) add[i].clear();
	FOR(i,N) {
		int smi;
		if(A[i].first<=v) {
			smi=0;
		}
		else {
			smi=A[i].first-v;
			if(smi>=N) return 0;
		}
		if(A[i].second<v) return 0;
		add[smi].push_back(A[i].second-v);
	}
	multiset<int> A;
	FOR(i,N) {
		FORR(a,add[i]) A.insert(a);
		if(A.size()&&*A.begin()<i) return 0;
		if(A.empty()) return 0;
		A.erase(A.begin());
	}
	return 1;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i].first>>A[i].second;
	}
	sort(A,A+N);
	
	int kmi=0;
	FOR(i,N) {
		kmi=max(kmi,A[i].first-i);
		swap(A[i].first,A[i].second);
	}
	sort(A,A+N);
	int kma=1<<30;
	FOR(i,N) {
		kma=min(kma,A[i].first-i);
		swap(A[i].first,A[i].second);
	}
	
	if(can(kmi)&&can(kma)) cout<<max(0,kma-kmi+1)<<endl;
	else cout<<0<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0