[计算几何]JZOJ 6290 倾斜的线

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

分析

先考虑正常坐标系下的斜率最大问题,排序x,最大斜率只会在数组中相邻的点对中产生

那么接近p/q也一样,旋转后排序做即可

 

#include <iostream> 
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const int N=2e5+10;
struct Point {
    ll x,y;
}a[N];
int n;
ll P,Q,p,q;
db k;

bool CMP(Point a,Point b) {
    return a.x-1.0*a.y/k<b.x-1.0*b.y/k;
}

ll GCD(ll a,ll b) {return !a?b:GCD(b%a,a);}

int main() {
    freopen("slope.in","r",stdin);
    freopen("slope.out","w",stdout);
    scanf("%d%lld%lld",&n,&P,&Q);k=1.0*P/Q;
    for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
    sort(a+1,a+n+1,CMP);
    p=a[2].y-a[1].y;q=a[2].x-a[1].x;
    for (int i=2;i<n;i++)
        if (abs(k-1.0*p/q)>=abs(k-1.0*(a[i+1].y-a[i].y)/(a[i+1].x-a[i].x))) {
            if (abs(k-1.0*p/q)==abs(k-1.0*(a[i+1].y-a[i].y)/(a[i+1].x-a[i].x))) {
                if (1.0*p/q>1.0*(a[i+1].y-a[i].y)/(a[i+1].x-a[i].x)) p=a[i+1].y-a[i].y,q=a[i+1].x-a[i].x;
            }
            else p=a[i+1].y-a[i].y,q=a[i+1].x-a[i].x;
        }
    ll gcd=GCD(p,q);
    printf("%lld/%lld",p/gcd,q/gcd);
}
View Code

 

posted @ 2019-08-19 07:35  Vagari  阅读(234)  评论(0编辑  收藏  举报