tzoj 1471: Wall 及其题解

        这算是我第一次写题解了

        这次的题目用的是凸包的算法,算是基本的模板题。  

        首先来了解下凸包的概念      (百度百科)

        通俗一点讲,就是给你一些点,让你构造一个多边形,多边形内包含这个点,而且要恰恰好,用橡皮筋套钉子的说法很形象。本题我才用的是凸包的Andrew算法。

tzoj 1471 Wall(从别的那边搞来的题目,非tzoj原创)

        凸包主要是求下凸包和逆序的上凸包。先进行排序,得到1和n的点,很容易想到1和n为多边形的顶点之一。此算法通过栈来存点,通过叉积求是否在有向直线的右侧或共线,如果是则将之前入栈的点出栈,因为栈里出现的点会被新的构成的包围或覆盖,然后反向求一次就行了。

        因为此题目还有保持距离什么的,然后因为多边形的外角和是360度知道多了一个圆的周长,所以再加上就好了。

   

        以下是代码加板子,真的是板子,用数组模拟栈是因为要取两个栈顶点:

#include<bits/stdc++.h>
#define ll long long
#define N 30005
#define M 105
#define debug cout<<"~~debug "
#define Mod 998244353
#define rc p<<1|1
#define lc p<<1
#define PII pair<long long,long long>
#define pb push_back
#define Inf 0x3f3f3f3f
//neuro-sama
using namespace std;
inline int read()
{
	char c=getchar();int f=1,x=0;
	while(c>'9'||c<'0'){ if(c=='-') f=-f;c=getchar();}
	while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return f*x;
}
struct node
{
	double x;
	double y;
}s[N],p[N];
typedef struct node node;
double cross(node a,node b,node c)//叉积
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dis(node a,node b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));//点与点之间的距离和
}
int cmp(node a,node b){
	if(a.x==b.x) 
		return a.y<b.y;
	return a.x<b.x;
}
int top;
int n,sum,m,T;
double k;
void solve()
{
//	cin>>n>>k;
	for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
	sort(p+1,p+1+n,cmp);
	for(int i=1;i<=n;i++){
		while(top>1&&cross(s[top-1],s[top],p[i])<=0) top--;//新点在右边或共线时出栈
		s[++top]=p[i]; 
	}
	int t=top;
	for(int i=n-1;i>=1;i--){
		while(top>t&&cross(s[top-1],s[top],p[i])<=0) top--;
		s[++top]=p[i];
	}
	double ans=0;
	for(int i=1;i<top;i++) ans+=dis(s[i],s[i+1]);
//	cout<<(int)ans<<endl;
	ans+=(2*acos(-1)*k);
	printf("%.lf\n",ans);
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	T=1;
	while(cin>>n>>k)
	{
		solve();
	}
}

    关注neuro-sama喵,关注neuro-sama喵。

    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值