平面上N个点,每两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点

平面上N个点,没两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点(斜率不存在的情况不考虑)。

先把N个点按x排序。
斜率k最大值为max(斜率(point[i],point[i+1])) 1 <=i < n-1。
复杂度Nlog(N)。

下面的例子是求斜率绝对值最大的直线,正常情况下把代码中的绝对值去掉就好了

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define pll pair<ll,ll>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
#define rson rt<<1|1,m+1,r
#define lson rt<<1,l,m
using namespace std;
const int N=1e5+100;
int arr[N],n,pp;
double mx;
struct node
{
  double x,y;
  friend bool operator <(node a,node b)
  {
      return a.x<b.x||a.x==b.x&&a.y<b.y;
  }
}points[N];
int GetMaxSlope()
{
    double fCurSlope = 0;
    double fMaxSlope = 0;
    for (int k=2;k<=n;k++)
    {
        double a=abs(points[k-1].x - points[k].x);
        fCurSlope = abs(points[k-1].y - points[k].y) / a;
        if (fMaxSlope < fCurSlope)
        {
            fMaxSlope = fCurSlope;
        }
    }
    mx=fMaxSlope;
    return 0;
}
int main()
{
    #ifdef LOCAL_DEFINE
        freopen("D:\\rush.txt","r",stdin);
    #endif
    //ios::sync_with_stdio(false),cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        pp=0;
        rep(i,1,n)
        {
            scanf("%lf%lf",&points[i].x,&points[i].y);
        }
        sort(points+1,points+1+n);
        rep(i,1,n-1)
        {
            if(points[i].x==points[i+1].x)
            {
                //cout<<-1<<endl;
                pp=1;
                break;
            }
        }
        if(pp)
            cout<<-1<<endl;
        else
        {
            GetMaxSlope();
            cout<<fixed<<setprecision(8)<<mx<<endl;
        }
    }
}
posted @ 2018-05-19 22:56  ffgcc  阅读(825)  评论(0编辑  收藏  举报