L3-009 长城

一开始的想法出现了错误,以为一旦南方的点遇到扫不过去的点,就无法再对后面产生监视作用了。其实做的过程和凸包的做法如出一辙。

正确代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
struct node
{
	ll x,y;
	node()
	{
	}
	node(ll x,ll y):x(x),y(y){
	}
	node operator-(node a)
	{
		return node(x-a.x,y-a.y);
	}
}s[100009];
int stck[100009];
int cross(node a,node b)
{
	ll t=a.x*b.y-a.y*b.x;
	if(t<=0) return 1;
	else if(t>0) return 0;
}
set<int> q;
int main()
{
	ll n;
	//freopen("t.txt","r",stdin);
	scanf("%lld",&n);
	for(ll i=0;i<n;i++)
	{
		scanf("%lld%lld",&s[i].x,&s[i].y);
	}
	ll st=0;
	ll next=2;
	ll cnt=0;
	node k,r;
	int top=0;
	for(int i=0;i<n;i++)
	{
		while(top>=2&&cross(s[stck[top-1]]-s[stck[top-2]],s[i]-s[stck[top-2]]))
		top--;
		if(top>=1)
		q.insert(stck[top-1]);
		stck[top++]=i; 
	}
	printf("%lld",q.size()-1);
	return 0;
}

错误代码:没考虑之前的烽火台还可能造成影响

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
	ll x,y;
	node()
	{
	}
	node(ll x,ll y):x(x),y(y){
	}
	node operator-(node a)
	{
		return node(x-a.x,y-a.y);
	}
}s[100009];

int cross(node a,node b)
{
	ll t=a.x*b.y-a.y*b.x;
	if(t<=0) return 1;
	else if(t>0) return 0;
}
int main()
{
	ll n;
	//freopen("t.txt","r",stdin);
	scanf("%lld",&n);
	for(ll i=0;i<n;i++)
	{
		scanf("%lld%lld",&s[i].x,&s[i].y);
	}
	ll st=0;
	ll next=1;
	ll cnt=0;
	node k=node(0,-1),r;
	while(1)
	{
		r=s[next]-s[st];
		if(cross(k,r))
		{
			k=r;
			next++;
			if(next==n) break;
		}else
		{
			st=next-1;
			cnt++;
			k=node(0,-1);
		}
	}
	printf("%lld\n",cnt);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值