倾斜的线

Description
在这里插入图片描述

Input
在这里插入图片描述

Output

在这里插入图片描述
Sample Input
6 15698 17433
112412868 636515040
122123982 526131695
58758943 343718480
447544052 640491230
162809501 315494932
870543506 895723090

Sample Output
193409386/235911335

Data Constraint
在这里插入图片描述

.
.
.
.
.
.
.
分析
把所有点按照P/Q斜率投射到y轴上排序,可以证明相邻的点对应的线段一定有一条是最优解,因为不相邻的点与给定的斜率夹角比相邻的大

.
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int n;
double p,q,x,y,t=2147483647;

struct edge
{
	double x,y,b;
}a[200010];

bool cmp(edge x,edge y)
{
	return x.b<y.b;
}

long long gcd(long long x,long long y)
{
	if (y==0) return x; else return gcd(y,x%y);
}

int main()
{
	freopen("slope.in","r",stdin);
	freopen("slope.out","w",stdout);
	scanf("%d%lf%lf",&n,&p,&q);
	double ans=p/q;
	for (int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&a[i].x,&a[i].y);
		a[i].b=a[i].y-p/q*a[i].x;
	}
	sort(a+1,a+n+1,cmp);
	for (int i=1;i<=n-1;i++)
	{
		double w=(a[i].y-a[i+1].y)/(a[i].x-a[i+1].x);
		if (abs(w-ans)<t)
		{
			t=abs(w-ans);
			x=abs(a[i].x-a[i+1].x);
			y=abs(a[i].y-a[i+1].y);
		}
	}
	long long ansx=(long long)x,ansy=(long long)y;
	long long g=gcd(ansx,ansy);
	ansx/=g;
	ansy/=g;
	printf("%lld/%lld",ansy,ansx);
	fclose(stdin);
	fclose(stdout);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值