Gym 101606L - Lounge Lizards - [计算几何+LIS]

题目连接:https://codeforces.com/gym/101606/problem/Lc++

 

题解:spa

在同一条线上的全部蜥蜴,他们的斜率都是相通的,换句话说能够直接经过斜率将蜥蜴分组。code

每一组即表明一条直线上的全部蜥蜴,再将这条直线以TV点为分界分红两条射线,这样每条射线上的蜥蜴,按距离TV从近到远,它们的身高排成一个整数序列,对这个序列求最长上升子序列便可。blog

须要注意的:ci

  DP求LIS的话是 $O(n^2)$ 的时间复杂度,是过不了的应该,要用贪心+二分 $O(n \log n)$ 求LIS长度;get

  不肯定直接用double类型存斜率是否是会有精度问题,能够用pair<int,int>类型当作分数类来表示斜率,注意约分就行了,还能够省去考虑直线平行坐标轴的问题。it

 

AC代码:class

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+10;

int n,X,Y;

int tot;
map<pii,int> mp;

struct Li
{
    ll dist;
    int h;
    bool operator<(const Li& o)const
    {
        return dist<o.dist;
    }
};
vector<Li> H[2][maxn];

inline pii grad(int x,int y)
{
    int dx=x-X, dy=y-Y;
    int g=__gcd(dx,dy);
    return make_pair(dx/g,dy/g);
}
inline ll dist(ll x,ll y)
{
    return (x-X)*(x-X)+(y-Y)*(y-Y);
}
inline bool check(int x,int y)
{
    if(x==X) return y>Y;
    else return x>X;
}

int S[maxn],top;
int solve(const vector<Li>& a)
{
    S[top=0]=a[0].h;
    for(int i=1;i<a.size();i++)
    {
        int pos=lower_bound(S,S+top+1,a[i].h)-S;
        S[pos]=a[i].h;
        top=max(top,pos);
    }
    return top+1;
}

int main()
{
    cin>>X>>Y;
    cin>>n;

    mp.clear(), tot=0;
    for(int i=1,x,y,h;i<=n;i++)
    {
        scanf("%d%d%d",&x,&y,&h);
        pii g=grad(x,y);
        if(mp[g]==0) mp[g]=++tot;
        H[check(x,y)][mp[g]].push_back((Li){dist(x,y),h});
    }

    int ans=0;
    for(int i=1;i<=tot;i++)
    {
        sort(H[0][i].begin(),H[0][i].end());
        sort(H[1][i].begin(),H[1][i].end());
        if(H[0][i].size()) ans+=solve(H[0][i]);
        if(H[1][i].size()) ans+=solve(H[1][i]);
    }
    cout<<ans<<endl;
}
相关文章
相关标签/搜索