其他分享
首页 > 其他分享> > 【UER #9】赶路 题解

【UER #9】赶路 题解

作者:互联网

link

Solution

我们考虑解决从 \(s\to t\) 用 \(S\) 中的点走的这样一个问题,那么对于一个点 \(v_0\) ,我们肯定就可以通过叉积来判断哪些点在它前面,哪些点在它后面,从而继续递归。

复杂度 \(\Theta(n^2)\) 。

Code

#include <bits/stdc++.h>
using namespace std;

#define Int register int
#define int long long
#define MAXN 505

template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}

int T,n;

struct Vector{
	int x,y;
	int operator * (const Vector &p)const{return x * p.y - y * p.x;}
	Vector operator - (const Vector &p)const{return Vector{x - p.x,y - p.y};}
}p[MAXN];

void solve (int s,int t,vector <int> S){
	if (S.size() == 0){
		write (s),putchar (' ');
		return ;
	}
	int v0 = S[0];
	vector <int> st[2];
	for (Int i = 1;i < S.size();++ i) if ((p[S[i]] - p[s]) * (p[v0] - p[s]) < 0) st[0].push_back (S[i]);else st[1].push_back (S[i]);
	if ((p[t] - p[s]) * (p[v0] - p[s]) < 0) swap (st[0],st[1]);
	solve (s,v0,st[0]),solve (v0,t,st[1]);
}

void Work (){
	read (n);
	for (Int i = 1;i <= n;++ i) read (p[i].x,p[i].y);
	vector <int> st;for (Int i = 2;i <= n - 1;++ i) st.push_back (i);
	solve (1,n,st),write (n),putchar ('\n');
}

signed main(){
	read (T);
	while (T --> 0) Work ();
	return 0;
}

标签:int,题解,void,赶路,v0,read,Vector,st,UER
来源: https://www.cnblogs.com/Dark-Romance/p/15840281.html