CF 593B Anton and Lines(水题)

题意是给你n条直线,和x1,x2;问 在x1,x2之间(不包括在x1,x2上)

存不存在任意两条线的交点。

说思路,其实很简单,因为给的直线的条数很多,所以无法暴力求每两条直线的交点,那么就求每条直线与x1,x2的交点,

那么直线1和x1的交点y1与x2的交点y3,直线2与x1的交点y2与x2的交点y4,如果y1>y2,y3>y4,那么画个图就知道肯定不会有交点,反之如果y1>y2,y3<y4,则有交点。

那么求出交点排序,按与x1交点升序排,如果相同的话按与x2交点降序排(此时交点在x1上不合要求)。

那么前一项和后一项比,如果后一项与x2的交点大于前一项说明有交点存在,直接跳出。

这个代码不写注释,(很好理解)

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<math.h>
 7 int f(const void*p,const void*q);
 8 using namespace std;
 9 typedef struct pp
10 {
11     double x1;
12     double x2;
13     double t;
14     double p;
15 } ss;
16 ss a[100005];
17 int main(void)
18 {
19     int n,i,j,k,p,q;
20     double x1,x2,y1,y2;
21     while(scanf("%d",&n)!=EOF)
22     {
23         scanf("%lf %lf",&x1,&x2);
24         for(i=0; i<n; i++)
25         {
26             scanf("%lf %lf",&a[i].x1,&a[i].x2);
27             a[i].t=a[i].x1*x1+a[i].x2;
28             a[i].p=a[i].x1*x2+a[i].x2;
29 
30         }
31         qsort(a,n,sizeof(ss),f);
32         int flag=0;
33         for(i=1; i<n; i++)
34         {
35             if(a[i].p<a[i-1].p)
36             {flag=1;
37              break;
38             }
39         }
40         if(flag)
41         {
42             printf("YES
");
43         }
44         else printf("NO
");
45 
46 
47 
48     }
49 
50 
51 return 0;
52 
53 
54 
55 }
56 
57 int f(const void*p,const void*q)
58 {
59     ss*ww=(ss*)p;
60     ss*ee=(ss*)q;
61     if(ww->t==ee->t)
62     {
63         return ww->p>ee->p?1:-1;
64     }
65     else return ww->t>ee->t?1:-1;
66 }
油!油!you@
原文地址:https://www.cnblogs.com/zzuli2sjy/p/4947634.html