平面上N個點,沒兩個點都肯定一條直線,求出斜率最大的那條直線所經過的兩個點(斜率不存在的狀況不考慮)。node
先把N個點按x排序。
斜率k最大值爲max(斜率(point[i],point[i+1])) 1 <=i < n-1。
複雜度Nlog(N)。ios
下面的例子是求斜率絕對值最大的直線,正常狀況下把代碼中的絕對值去掉就行了c++
#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; } } }