数据过水
查看原帖
数据过水
1052371
jiangjiang12楼主2025/8/4 20:24

下面是我的AC代码。。。复杂度在该题数据下最高可达O(N^2)。。。然而它水过了。。。

#include <bits/stdc++.h>
using namespace std;
struct node{
	long long score[2];
	int book;
}pie[2][100005];
long long D;
int d[2][100005], ans[100005], N;
int bound(node* c, long long val, int obj, bool equ){
	int l=1, r=N, mid;
	while(l<r){
		mid=(l+r)/2;
		if(c[mid].score[obj]>val||(equ&&(c[mid].score[obj]==val))){r=mid;}
		else{
			l=mid+1;
		}
	}
	return (c[l].score[obj]>val||(equ&&(c[l].score[obj]==val)))?l:(N+1);
}
bool cmp0(node scr1, node scr2){
	return scr1.score[0]<scr2.score[0];
}
bool cmp1(node scr1, node scr2){
	return scr1.score[1]<scr2.score[1];
}
void bfs(){
	sort(pie[0]+1, pie[0]+N+1, cmp1);
	sort(pie[1]+1, pie[1]+N+1, cmp0);
	queue<int> xq, kq;
	for(int i=1; i<=N; i++){
		if(!pie[0][i].score[1]){xq.push(i); kq.push(0); d[0][i]=1;}
		if(!pie[1][i].score[0]){xq.push(i); kq.push(1); d[1][i]=1;}
	}
	for(int i=1; i<=N; i++){
		if(!d[0][i]){d[0][i]=-1;}
		if(!d[1][i]){d[1][i]=-1;}
	}
	while(!xq.empty()){
		int xx=xq.front(), kk=kq.front();
		xq.pop(); kq.pop();
		for(int i=bound(pie[1-kk], pie[kk][xx].score[kk]-D, kk, true); i<bound(pie[1-kk], pie[kk][xx].score[kk], kk, false); i++){
			if(d[1-kk][i]<0){
				d[1-kk][i]=d[kk][xx]+1; kq.push(1-kk); xq.push(i);
			}
		}
	}
}
int main(){
	scanf("%d%lld", &N, &D);
	for(int i=1; i<=N; i++){
		scanf("%lld%lld", &pie[0][i].score[0], &pie[0][i].score[1]);
		pie[0][i].book=i;
	}
	for(int i=1; i<=N; i++){
		scanf("%lld%lld", &pie[1][i].score[0], &pie[1][i].score[1]);
	}
	bfs();
	for(int i=1; i<=N; i++){
		ans[pie[0][i].book]=d[0][i];
	}
	for(int i=1; i<=N; i++){
		printf("%d\n", ans[i]);
	}
	return 0;
}
2025/8/4 20:24
加载中...