PTA团体程序设计天梯赛-练习集 L3-009 长城 (计算几何)

原题链接
思路:
前几天刚做了可见线段的
思考方式是差不多的,考虑什么时候必须加一个烽火台。
在这里插入图片描述
A点看不见C点,所以在B必须要放一个烽火台。
然后再类似凸包,用一个单调栈维护
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
#define I_int ll
#define modl 19260817*19890604-19491001
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
#define read read()
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
const int maxn=1e5+100;

struct node{
    ll x,y;
    int id;
}line[maxn],stk[maxn];
ll n,top;

ll cul(node p1,node p2,node p0)//计算叉积,p1,p2,p0都为点
{
    ll x1,y1,x2,y2;
    x1=p1.x-p0.x;
    y1=p1.y-p0.y;
    x2=p2.x-p0.x;
    y2=p2.y-p0.y;
    return x1*y2-x2*y1;
}
int vis[maxn];
int main(){
    n=read;
    for(int i=1;i<=n;i++){
        cin>>line[i].x>>line[i].y;
        line[i].id=i;
    }
    for(int i=1;i<=n;i++){
    	while(top>=2&&cul(stk[top-1],stk[top],line[i])<=0) top--;
        if(stk[top].id!=1) vis[stk[top].id]=1;
        stk[++top]=line[i];
    }
	int cnt=0;
	for(int i=1;i<=n;i++)
		if(vis[i]) cnt++;
	cout<<cnt<<endl;
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值