QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183765#6693. Fast and Fatliuzhenhao09#RE 1ms8188kbC++201.5kb2023-09-19 20:11:092023-09-19 20:11:09

Judging History

你现在查看的是最新测评结果

  • [2023-09-19 20:11:09]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:8188kb
  • [2023-09-19 20:11:09]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
struct node{
	int v,w;
	bool operator <(const node& kkk)const{
		return v < kkk.v;
	}
}a[100010],b[100010],c[100010];
int T,n,tot1 = 0,tot2 = 0;
bool cmp1(node A,node B){
	return A.w > B.w;
}
bool cmp2(node A,node B){
	return A.w + A.v < B.w + B.v;
}
bool check(int x){
	tot1 = tot2 = 0;
	for(int i = 1; i <= n; i++){
		if(a[i].v < x) b[++tot1] = a[i];
		else c[++tot2] = a[i];
	}
	sort(b + 1,b + tot1 + 1,cmp1);
	sort(c + 1,c + tot2 + 1,cmp2);
	multiset<pii>s;
	multiset<int>t;
	multiset<pii>::iterator it;
	multiset<int>::iterator ti;
	for(int i = 1; i <= tot2; i++) s.insert(make_pair(c[i].w,c[i].v + c[i].w)),t.insert(c[i].v + c[i].w);
	if(tot1 > tot2) return 0;
	for(int i = 1; i <= tot1; i++){
		if(s.empty()) return 0;
		it = s.lower_bound(make_pair(b[i].w,0));
		if(it != s.end()){
			int v = it->second;
			t.erase(t.find(v));
			s.erase(it);
		} 
		else{
			if(t.empty()) return 0;
			ti = t.end();
			ti--;
			if((*ti) - b[i].w < x) return 0;
			t.erase(ti);
		}
	}
	return 1;
}
signed main(){
	scanf("%lld",&T);
	while(T--){
		scanf("%lld",&n);
		int mx = 0;
		for(int i = 1; i <= n; i++) scanf("%lld %lld",&a[i].v,&a[i].w),mx = max(mx,a[i].v);
		sort(a + 1,a + n + 1);
		int l = 1,r = mx;
		while(l < r){
			int mid = (l + r + 1) >> 1;
			if(check(mid)) l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8188kb

input:

2
5
10 5
1 102
10 100
7 4
9 50
2
1 100
10 1

output:

8
1

result:

ok 2 number(s): "8 1"

Test #2:

score: -100
Runtime Error

input:

10000
4
280251502 664541723
375808746 641141991
95134537 898607509
455259328 944978891
2
798417052 547329847
785434740 991778535
6
623628702 857611223
275667427 453747403
292209526 283132767
330752033 988721243
470297536 608192332
477186035 325224271
3
280572174 994054447
306566740 923535026
3781360...

output:


result: