JZOJ 6290. 倾斜的线
题目
分析
- 我们可以对于每个点都做一条斜率为p/q的线
- 然后截距排序 然后答案的直线肯定是相邻两个点
- 求最小就好了
- 那么怎么求截距呢
- 截距=a[i].y-p/q*a[i].x
代码
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cmath> 5 using namespace std; 6 const int N=2e5; 7 const double INF=1e9+7; 8 struct sb 9 { 10 double x,y; 11 double b; 12 }a[N+10]; 13 bool cmp(sb a,sb b){return a.b<b.b;}; 14 double P,Q,ans,nowx,nowy,now=INF; 15 int n; 16 long long gcd(long long a,long long b){return b?gcd(b,a%b):a;} 17 int main () 18 { 19 freopen("slope.in","r",stdin); 20 freopen("slope.out","w",stdout); 21 cin>>n>>P>>Q; 22 ans=P/Q; 23 for (int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,a[i].b=a[i].y-P/Q*a[i].x; 24 sort(a+1,a+1+n,cmp); 25 for (int i=1;i<n;i++) 26 { 27 double k=(a[i].y-a[i+1].y)/(a[i].x-a[i+1].x); 28 if (abs(k-ans)<now) now=abs(k-ans),nowx=abs((a[i].x-a[i+1].x)),nowy=(a[i].y-a[i+1].y); 29 } 30 long long ansx=(long long)nowx,ansy=(long long)nowy; 31 long long g=gcd(ansx,ansy); 32 ansx/=g,ansy/=g; 33 cout<<abs(ansy)<<"/"<<abs(ansx); 34 }
为何要逼自己长大,去闯不该闯的荒唐

浙公网安备 33010602011771号