题解:P2924 [USACO08DEC] Largest Fence G
注意到对于一个凸多边形而言,如果将其所有边看作向量后按照顺序写下来,再将起点平移到原点,那么写下这些向量的顺序实际上是顺时针(或者是逆时针)顺序。
所以考虑先把每条边拆成两个有向边,然后把这些有向边按照顺时针排序,那么任意一个凸包都可以表达为有向边序列的一个子序列。
但是并非任意一个子序列都合法,但是如果子序列上相邻的有向边首尾相接,并且子序列最后一个有向边形如
枚举第一条有向边的起点后暴力 dp 即可,时间复杂度
#include<bits/stdc++.h>
using namespace std;
const int maxn = 300;
const double eps = 1e-9;
pair<int,int> P[maxn];
bool cmp(pair<int,int> X,pair<int,int> Y){
//对边 X.first->X.second,Y.first->Y.second 排序
//先分类
int dx1=P[X.second].first-P[X.first].first,dy1=P[X.second].second-P[X.first].second;
int dx2=P[Y.second].first-P[Y.first].first,dy2=P[Y.second].second-P[Y.first].second;
int id1,id2;
if(dx1==0&&dy1>0) id1=1;
else if(dx1>0&&dy1>0) id1=2;
else if(dx1>0&&dy1==0) id1=3;
else if(dx1>0&&dy1<0) id1=4;
else if(dx1==0&&dy1<0) id1=5;
else if(dx1<0&&dy1<0) id1=6;
else if(dx1<0&&dy1==0) id1=7;
else id1=8;
if(dx2==0&&dy2>0) id2=1;
else if(dx2>0&&dy2>0) id2=2;
else if(dx2>0&&dy2==0) id2=3;
else if(dx2>0&&dy2<0) id2=4;
else if(dx2==0&&dy2<0) id2=5;
else if(dx2<0&&dy2<0) id2=6;
else if(dx2<0&&dy2==0) id2=7;
else id2=8;
if(id1!=id2) return id1<id2;
return (P[X.second].second-P[X.first].second)*(P[Y.second].first-P[Y.first].first)>(P[Y.second].second-P[Y.first].second)*(P[X.second].first-P[X.first].first);
}
int n;
vector< pair<int,int> > E;
int mx[maxn];
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>P[i].first>>P[i].second;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j) E.push_back(make_pair(i,j));
}
}
int ans=0;
sort(E.begin(),E.end(),cmp);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) mx[j]=-1e9;
mx[i]=0;
for(pair<int,int> e:E){
int res=mx[e.first]+1;
mx[e.second]=max(mx[e.second],res);
}
ans=max(ans,mx[i]);
}
cout<<ans<<"\n";
return 0;
}