48pts求助
查看原帖
48pts求助
620253
MvemiY楼主2023/2/27 21:07
#include<bits/stdc++.h>
using namespace std;
int n, ind[5010];
struct Node{
	int x1, y1, x2, y2;
}a[5010], b[5010];
struct EQ{
	double k, b;
}eq[5010];
vector <int> tpx, G[5010];
pair <double,double> JJ(int i, int j){
	double x = (eq[j].b - eq[i].b) * 1.0 / (eq[i].k - eq[j].k);
	double y = eq[i].k * x + eq[i].b;
	return {x, y};
}
pair <double,double> point(int i, int x){
	return {x, eq[i].k * x + eq[i].b};
}
bool out_X(int u, int v){
	// u 与 v X完全不包含 
	return a[u].x2 < a[v].x1 || a[u].x1 > a[v].x2;
}
bool out_Y(int u, int v){
	// u 与 v Y完全不包含
	return b[u].y2 < b[v].y1 || b[u].y1 > b[v].y2;
}
bool under(int u, int v){
	int x;
	if(a[u].x1 >= a[v].x1 && a[u].x1 <= a[v].x2)
		x = a[u].x1;
	else if(a[u].x2 >= a[v].x1 && a[u].x2 <= a[v].x2)
		x = a[u].x2;
	else if(a[v].x1 >= a[u].x1 && a[v].x1 <= a[u].x2)
		x = a[v].x1;
	else x = a[v].x2;
	double yu = eq[u].k * x + eq[u].b, yv = eq[v].k * x + eq[v].b;
//	cout << "EQ: " << u << ' ' << v << "    " << x << ' ' << yu << ' ' << yv << endl;
	return yu < yv ; 
}
bool P(int u, int v){
	if(!under(u, v)){
//		cout << u << ' ' << v << ' ' << "A0" << endl;
		return 0;
	}
	if(!out_X(u, v)){
//		cout << u << ' ' << v << ' ' << "B1" << endl;
		return 1;
	}
//	if(!out_Y(u, v)){
//		cout << u << ' ' << v << ' ' << "C0" << endl;
//		return 0;
//	}
//	cout << u << ' ' << v << ' ' << "D0" << endl;
	return 0;
}
void topo(){
	queue <int> q;
	for(int i = 1; i <= n; i++)
		if(ind[i] == 0)
			q.push(i);
	while(!q.empty()){
		int u = q.front(), vl = G[u].size();
		q.pop();
		cout << u << ' ';
		for(int i = 0; i < vl; i++)
			if(--ind[G[u][i]] == 0)
				q.push( G[u][i] );
	}
}
int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2;
		b[i] = a[i];
		if(a[i].x1 > a[i].x2)
			swap(a[i].x1, a[i].x2),
			swap(a[i].y1, a[i].y2);
		if(b[i].y1 > b[i].y2)
			swap(b[i].x1, b[i].x2),
			swap(b[i].y1, b[i].y2);
	}
	for(int i = 1; i <= n; i++){
		eq[i].k = (a[i].y2 - a[i].y1) * 1.0 / (a[i].x2 - a[i].x1) * 1.0;
		eq[i].b = a[i].y1 - eq[i].k * a[i].x1;
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			if(i != j && P(i, j)){
				G[i].push_back(j);
				ind[j]++;
			}
//	for(int i = 1; i <= n; i++)
//		cout << eq[i].k << ' ' << eq[i].b << endl;
//	for(int i = 1; i <= n; i++){
//		int len = G[i].size();
//		cout << i << ": ";
//		if(len)
//			for(int j = 0; j < len; j++)
//				cout << G[i][j] << ' ';
//		cout << endl;
//	}
	topo();
	return 0;
}

思路是建图跑拓扑,如果线段 aa 在线段 bb 之上,且两者 xx 坐标 有重合地方就连边,用的一次函数来判断,因为给定的线段刚开始一定不重合,所以若 aa 的左端点在 bb 的下方,那么 aa 的右端点也一定在 bb 的下方(即整体都在 bb 下面),却只得了 48pts,有绿有红,求大佬帮助

2023/2/27 21:07
加载中...