首页 技术 正文
技术 2022年11月10日
0 收藏 310 点赞 4,802 浏览 1484 个字

平面上n个点,点之间沿直线走,规划一条路线,每次只能往左半平面的点走,走过最多的点。

显然所有的点都能走过。

n^2的暴力显然是每次找左边与其所形成夹角最小的点,但这样过不了(卡常数?)。

或者每轮不断求凸包。有个非常巧妙的地方是将每一轮输出后剩下的最后一个点加到下一轮的点里面一起求凸包,这样只要按逆时针输出每一轮,就能满足。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
struct Point{
ll x,y;
int id;
Point(const ll &x,const ll &y){
this->x=x;
this->y=y;
}
Point(){}
};
typedef Point Vector;
bool cmp(const Point &a,const Point &b){
return a.x!=b.x ? a.x<b.x : a.y<b.y;
}
Vector operator - (const Point &a,const Point &b){
return Vector(a.x-b.x,a.y-b.y);
}
ll Cross(const Vector &a,const Vector &b){
return a.x*b.y-a.y*b.x;
}
Point ps[5010],qs[10010];
int k;
bool vis[5010];
void convex_hull(){
k=0;
for(int i=0;i<n;++i)if(!vis[ps[i].id]){
while(k>1 && Cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<=0){
--k;
}
qs[k++]=ps[i];
}
for(int i=n-2,t=k;i>=0;--i)if(!vis[ps[i].id]){
while(k>t && Cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<=0){
--k;
}
qs[k++]=ps[i];
}
--k;
}
int main(){
//freopen("h.in","r",stdin);
//freopen("h.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lld%lld",&ps[i].x,&ps[i].y);
ps[i].id=i+1;
}
sort(ps,ps+n,cmp);
int e=0;
printf("%d\n",n);
int I=0;
int last;
while(n-e>1){
++I;
convex_hull();
int ID;
if(I==1){
ID=0;
}
else{
for(int i=0;i<k;++i){
if(last==qs[i].id){
ID=i;
break;
}
}
}
for(int i=ID;i<(ID==0 ? k-1 : k);++i){
++e;
vis[qs[i].id]=1;
printf("%d%c",qs[i].id,e==n?'\n':' ');
}
for(int i=0;i<ID-1;++i){
++e;
vis[qs[i].id]=1;
printf("%d%c",qs[i].id,e==n?'\n':' ');
}
if(ID==0){
last=qs[k-1].id;
}
else{
last=qs[ID-1].id;
}
}
for(int i=0;i<n;++i) if(!vis[ps[i].id]){
++e;
printf("%d%c",ps[i].id,e==n?'\n':' ');
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294