Question 215. 2022 Winter Holiday Ladder Competition Training - 7-15 Great Wall (30 points)


Question 215. 2022 Winter Holiday Ladder Competition Training - 7-15 Great Wall (30 points)


1. The topic

insert image description here
insert image description here
insert image description here

2. Problem solving

According to the analysis of the meaning of the question, we will find that if you want to monitor every part of the Great Wall, you must pay attention to the position of the concave point. From south to north, you must be at the first point in front of the concave point (south of the concave point). If you build a beacon tower at a convex point, you can monitor that concave point. Otherwise, if you build a beacon tower at a position after the first one or even at a later position, your line of sight will be limited by the convex point in front and cannot be monitored. to that pit. Since we give priority to the first convex point before the concave point, and this convex point will obviously be the last encountered, this is the last-in-first-out characteristic of the stack, so we can use the stack to achieve the above operation.
We go to the input point, push the point into the stack, when there is one point in the stack, we try to build a beacon tower (into the result set), when there are at least two points in the stack, we go to the new input The point at the top of the stack and the point at the top of the stack are judged to see if the point at the top of the stack is a concave point. The bump on the top of the stack is used to build a beacon tower (into the result set). The size of the final result set is the answer.
Now analyze the judgment of the concave point:
insert image description here

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn=100001;

ll x[maxn],y[maxn];
stack<int> s;
set<int> res;

int isConcave(int A,int B,int C)
{
    
    
    ll xAB=x[B]-x[A],yAB=y[B]-y[A];
    ll xAC=x[C]-x[A],yAC=y[C]-y[A];
    if(yAB*xAC<=yAC*xAB)//kAB<=kAC,则B为A、B、C中的凹点。在原式上做了点变化,避免除法导致浮点数精度问题
    {
    
    
        return 1;
    }
    else
    {
    
    
        return 0;
    }
}

int main()
{
    
    
    int N;
    cin>>N;
    for(int i=0;i<N;i++)
    {
    
    
        scanf("%lld%lld",&x[i],&y[i]);
        if(s.size()>=1)
        {
    
    
            while(s.size()>=2)
            {
    
    
                int B=s.top();
                s.pop();//先假装pop,把B拿去判断是不是当前凹点
                int C=s.top();
                //cout<<B<<" "<<C<<endl;
                if(!isConcave(i,B,C))//如果不是凹点,就把B装回去
                {
    
    
                    s.push(B);
                    break;
                }
            }
            if(s.top()!=0)//不是总部
            {
    
    
                res.insert(s.top());
            }
        }
        s.push(i);
    }
    cout<<res.size();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324347687&siteId=291194637