Codeforces-961D

题意

给出一些点,判断这些点能否都在小于等于两条直线上

思路

首先找出三个不在一条直线上的点,可以得到三条直线。
依次枚举每条直线可解。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdio>
#include "stack"
#include "list"
#include <cstring>
#include <algorithm>
#include "iostream"
#include <cmath>
#include <vector>
#include "set"
#include "queue"
using namespace std;
#define FOP freopen("input.txt","r",stdin)
#define R index*2+1
#define Met(x,y) memset(x,y,sizeof x)
#define L index*2
typedef long long ll;
const ll MAXN =3e5+100;
const ll INF = 1e18;
const ll MOD = 1e9+7;

struct Point{
ll x,y;
bool exist;
void read()
{
scanf("%lld%lld",&x,&y);
}
Point(double x_=0,double y_=0)
{
x=x_,y=y_;
}
Point operator -(const Point& rhs)
{
return Point(x-rhs.x,y-rhs.y);
}
};
struct Line{
ll A,B,C;
Line(Point a,Point b)
{
A=b.y-a.y;
B=a.x-b.x;
C=b.x*a.y-a.x*b.y;
}
Line(){}
};
Point p[MAXN];
bool PointOnLine(Point a,Line b)
{
return b.A*a.x+b.B*a.y+b.C==0;
}
int main(int argc, char const *argv[]) {
// freopen("input.txt","r",stdin);
ll n;
cin>>n;
for(int i=0;i<n;++i)
p[i].read();
if(n<=4)
{
puts("YES");
return 0;
}
Line l[2];
ll pp[3];
pp[0]=0; pp[1]=1;
Line tem(p[0],p[1]);
bool flag=1;
for(int i=2;i<n;++i)
if (!PointOnLine(p[i],tem)) {
pp[2]=i;
flag=0;
}
if (flag) {
puts("YES");
return 0;
}
for(int i=0;i<3;++i)
{
l[0]=Line(p[pp[i]],p[pp[(i+1)%3]]);
bool yes=1,al=0;
for(int j=0;j<n;++j)
if (al) {
if(!PointOnLine(p[j],l[0]) && !PointOnLine(p[j],l[1]))
{
yes=0;
break;
}
} else {
if(i==2 && j==1)
i=i;
if(PointOnLine(p[j],l[0]) || j==pp[(i+2)%3]) continue;
al=1;
l[1]=Line(p[j],p[pp[(i+2)%3]]);
}
if (yes) {
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}