51Nod-1100 斜率最大

来源:互联网 发布:c4d r17 mac安装教程 编辑:程序博客网 时间:2024/06/05 00:35

1100 斜率最大`
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注
平面上有N个点,任意2个点确定一条直线,求出所有这些直线中,斜率最大的那条直线所通过的两个点。
(点的编号为1-N,如果有多条直线斜率相等,则输出所有结果,按照点的X轴坐标排序,正序输出。数据中所有点的X轴坐标均不相等,且点坐标为随机。)
Input
第1行,一个数N,N为点的数量。(2 <= N <= 10000)
第2 - N + 1行:具体N个点的坐标,X Y均为整数(-10^9 <= X,Y <= 10^9)
Output
每行2个数,中间用空格分隔。分别是起点编号和终点编号(起点的X轴坐标 < 终点的X轴坐标)
Input示例
5
1 2
6 8
4 4
5 4
2 3
Output示例
4 2

将所有点,按横坐标升序排序,最大的斜率一点存在于连续的两点间

#include<iostream>#include<algorithm>using namespace std;struct Point {int x,y,no;};struct Point P[10010];bool cmp(struct Point a,struct Point b){    return a.x<b.x;}int main(){    int n;    cin>>n;    for (int i=0;i<n;i++)    {        cin>>P[i].x>>P[i].y;        P[i].no=i+1;    }    sort(P,P+n,cmp);    double k=-1;    for (int i=0;i<n-1;i++)    {        int temp=(P[i+1].y-P[i].y)/(P[i+1].x-P[i].x);        if (temp>k)            k=temp;    }     for (int i=0;i<n-1;i++)    {        int temp=(P[i+1].y-P[i].y)/(P[i+1].x-P[i].x);        if (temp==k)           cout<<P[i].no<<" "<<P[i+1].no<<endl;    }    return 0;}
原创粉丝点击