Jzoj P6290 傾斜的線___思維

題目大意:

在這裏插入圖片描述
在這裏插入圖片描述

分析:

以斜率爲P/QP/Q的過原點的直線爲新的y軸,垂直於它做x軸
發現答案最優的必定是此時轉換座標以後斜率最大的那一條直線
而斜率最大的直線的兩點必定滿足x座標是相鄰的
那麼就隨便做了

代碼:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>

#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--)

#define inf 0x7fffffff
#define N 200005

using namespace std;

typedef long long ll;
typedef double db;

struct Node {
	double x, y; int id;
}a[N], b[N];
int n;
db P, Q, jio;

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

bool cmp(Node aa, Node bb) {
    return aa.x < bb.x;
}

int main() {
	freopen("slope.in","r",stdin);
	freopen("slope.out","w",stdout);
	scanf("%d %lf %lf", &n, &P, &Q);
	jio = atan(Q / P);
	rep(i, 1, n) {
	    scanf("%lf %lf", &a[i].x, &a[i].y);
        b[i].x = (a[i].x - a[i].y * tan(jio)) * cos(jio); 
        b[i].y = b[i].x * sin(jio) + a[i].y / cos(jio);
	    b[i].id = i;
	}
	sort(b + 1, b + n + 1, cmp);
	ll ans1, ans2;
	db ans = inf, orz = P / Q;
	rep(i, 1, n - 1) {
		db k = (a[b[i].id].y - a[b[i + 1].id].y) / (a[b[i].id].x - a[b[i + 1].id].x);	
		if (ans > fabs(k - orz)) 
		    ans = fabs(k - orz), ans1 = abs(a[b[i].id].y - a[b[i + 1].id].y), ans2 = abs(a[b[i].id].x - a[b[i + 1].id].x);
		    else if (ans == fabs(k - orz) && k > ans)
		    ans = fabs(k - orz), ans1 = abs(a[b[i].id].y - a[b[i + 1].id].y), ans2 = abs(a[b[i].id].x - a[b[i + 1].id].x);		    
	}
	ll ansgcd = Gcd(ans1, ans2);
	printf("%lld/%lld\n", ans1 / ansgcd, ans2 / ansgcd);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章