斜率最大
李陶冶 (命题人)
基准时间限制:1 秒 空间限制:131072 KB 分值: 20
平面上有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<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
int num;
}ch[10005];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&ch[i].x,&ch[i].y);
ch[i].num=i;
}
sort(ch+1,ch+1+n,cmp);
double k,dmax=-0x3f3f3f;
int i1,i2;
for(int i=1;i<n;i++)
{
k=(ch[i+1].y-ch[i].y)*1.0/(ch[i+1].x-ch[i].x);
if(dmax<k)
{
dmax=k;
i1=ch[i].num;
i2=ch[i+1].num;
}
}
printf("%d %d\n",i1,i2);
return 0;
}