n个点中求任意两点组成斜率的最大值

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100

首先按x坐标排序,然后相邻的三个点A,B,C 组成的三条直线必然有K(AC)<max(K(A,B),K(B,C));(K是斜率) 

所以斜率一定会在相邻的两点中产生,排序O(nlogn),更新最大值O(n)。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d
", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("b.txt", "w", stdout);
36 #define maxn 1010
37 #define maxv 3000
38 #define mod 1000000000
39 using namespace std;
40 
41 struct point
42 {
43     int x,y,id;
44     bool operator < (const point a) const
45     {
46         return x<a.x;
47     }
48 }p[10001];
49 int main()
50 {
51     // freopen("a.txt","r",stdin);
52     int n,j,k;
53     scanf("%d",&n);
54     for(int i=1;i<=n;i++)
55     {
56         scanf("%d%d",&p[i].x,&p[i].y);
57         p[i].id=i;
58     }
59     sort(p+1,p+n+1);
60     double ans=0;
61     for(int i=2;i<=n;i++)
62     {
63         if(p[i].y==p[i-1].y) continue;
64         if((p[i].y-p[i-1].y)*1.0/(p[i].x-p[i-1].x)>ans)
65         {
66             ans=(p[i].y-p[i-1].y)*1.0/(p[i].x-p[i-1].x);
67             j=p[i].id;
68             k=p[i-1].id;
69         }
70     }
71     printf("%d %d
",k,j);
72     return 0;
73 }
原文地址:https://www.cnblogs.com/nowandforever/p/4616937.html