[2018.10.17 T1] 斜率

版权声明:大佬您能赏脸,蒟蒻倍感荣幸,还请联系我让我好好膜拜。 https://blog.csdn.net/ShadyPi/article/details/83115882

暂无链接

斜率

【题目描述】

给定平面上 n n 个点的坐标,求所有经过这些点中至少两个点的直线的最大斜率。

【输入格式】

第一行一个整数 n n ,表示点的个数。
接下来 n n 行,每行两个正整数 x , y x,y ,描述每个点的坐标。

【输出格式】

一行一个实数表示答案,保留小数点后 3 3 位。

【样例 1】
slope. in

3
1 2
2 3
3 4

slope.out

1.000

【样例 2】

见选手目录下 slope. in/slope.ans

【数据范围与约定】

对于 20 % 20\% 的数据, n < = 10 n<=10
对于 50 % 50\% 的数据, n < = 1 0 3 n<=10^3
对于 100 % 100\% 的数据, n < = 5 1 0 5 n<=5*10^5 ,坐标 < = 1 0 7 <=10^7 ,没有两点横坐标相同。

题解

发现对于一个角朝下的三角形,上面的那条边斜率一定小于旁边两条边,所以直接将所有点按 x x 轴排序,算相邻两点的斜率取 m a x max 即可。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=5e5+5;
struct sd{double x,y;}pt[M];
double ans=-1e7;
int n,i;
bool operator<(sd a,sd b){return a.x<b.x;}
void in(){scanf("%d",&n);for(i=1;i<=n;++i)scanf("%lf%lf",&pt[i].x,&pt[i].y);}
void ac(){for(sort(pt+1,pt+1+n),i=2;i<=n;++i)ans=max(ans,(pt[i].y-pt[i-1].y)/(pt[i].x-pt[i-1].x));printf("%.3lf",ans);}
int main(){in(),ac();}

猜你喜欢

转载自blog.csdn.net/ShadyPi/article/details/83115882
t1