一开始的想法出现了错误,以为一旦南方的点遇到扫不过去的点,就无法再对后面产生监视作用了。其实做的过程和凸包的做法如出一辙。
正确代码
#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;
}