D. Pair Of Lines

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordinates are integers), and all points are distinct.

You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the number of points you are given.

Then n lines follow, each line containing two integers x**i and y**i (|x**i|, |y**i| ≤ 109)— coordinates of i-th point. All n points are distinct.

Output

If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.

Examples

input

Copy

50 00 11 11 -12 2

output

Copy

YES

input

Copy

50 01 02 11 12 3

output

Copy

NO

Note

In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points.

题意:

给你n个坐标点

问你能不能画两条直线,使所有的点都落在直线上。

(两条直线可以重合)。

思路:

n<=4 直接输出YES

n>=5 时,我们这样考虑:

如果这n个点是满足条件的。

那么两条线上点数中较大的那一个(称为L1)一定大于等于n/2

所以我们可以随机从n个点中选择2个点。

这两点同时落在L1上的概率为\(1/4\)

所以如果这n个点满足条件,我们只需要随机大概4次就可以找到那两条线。

4次只是期望值,N的数据范围是\(1e5\) 所以我们可以随机50次以上,这样更稳。

关于随机到2个点,

我们扫一遍数组,把所有不在这2个确定的直线上的都扔在一个vector里存着。

然后我们再判断vector中所有点是否在同一个直线上。如果有满足条件的就结束随机,输出Yes。

代码:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
// #define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const double eps = 1e-6;
int sgn(double x)
{
if (fabs(x) < eps) { return 0; }
if (x < 0) { return -1; }
else { return 1; }
}
struct Point {
ll x, y;
Point() {}
Point(double _x, double _y)
{
x = _x; y = _y;
}
void input()
{
cin >> x >> y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x, y - b.y);
} // 111 / 179
//叉积
double operator ^(const Point &b)const
{
return x * b.y - y * b.x;
}
//点积
double operator *(const Point &b)const
{
return x * b.x + y * b.y;
}
//绕原点旋转角度 B(弧度值),后 x,y 的变化
void transXY(double B)
{
double tx = x, ty = y;
x = tx * cos(B) - ty * sin(B);
y = tx * sin(B) + ty * cos(B);
}
};
int n;
Point a[maxn];
std::vector<int> p;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin >> n;
repd(i, 1, n) {
a[i].input();
}
if (n <= 4) {
cout << "YES" << endl;
return 0;
}
int id1, id2;
mt19937 rnd(time(0));
int isok = 0;
repd(rp, 1, 100) {
p.clear();
id1 = rnd() % n + 1;
do {
id2 = rnd() % n + 1;
} while (id2 == id1);
repd(i, 1, n) {
if (i == id1 || id2 == i) {
continue;
}
if (((a[id2] - a[id1]) ^ (a[i] - a[id1])) != 0) {
p.push_back(i);
}
}
int flag = 1;
for (int i = 2; i < sz(p); ++i) {
if (((a[p[1]] - a[p[0]]) ^ (a[p[i]] - a[p[0]])) != 0) {
flag = 0;
break;
}
}
if (flag) {
isok = 1;
// chu(id1);
// chu(id2);
break;
}
}
if (isok) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Educational Codeforces Round 41 (Rated for Div. 2) D. Pair Of Lines (几何,随机)的更多相关文章

  1. Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings

    题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...

  2. Educational Codeforces Round 41 (Rated for Div. 2)(A~D)

    由于之前打过了这场比赛的E题,而后面两道题太难,所以就手速半个多小时A了前4题. 就当练手速吧,不过今天除了C题数组开小了以外都是1A A Tetris 题意的抽象解释可以在Luogu里看一下(话说现 ...

  3. Educational Codeforces Round 41 (Rated for Div. 2)

    这场没打又亏疯了!!! A - Tetris : 类似俄罗斯方块,模拟一下就好啦. #include<bits/stdc++.h> #define fi first #define se ...

  4. Educational Codeforces Round 41 (Rated for Div. 2) ABCDEF

    最近打的比较少...就只有这么点题解了. A. Tetris time limit per test 1 second memory limit per test 256 megabytes inpu ...

  5. D. Pair Of Lines( Educational Codeforces Round 41 (Rated for Div. 2))

    #include <vector> #include <iostream> #include <algorithm> using namespace std; ty ...

  6. C. Chessboard( Educational Codeforces Round 41 (Rated for Div. 2))

    //暴力 #include <iostream> #include <algorithm> #include <string> using namespace st ...

  7. B. Lecture Sleep( Educational Codeforces Round 41 (Rated for Div. 2))

    前缀后缀和搞一搞,然后枚举一下区间,找出最大值 #include <iostream> #include <algorithm> using namespace std; ; ...

  8. 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果点的个数<=3 那么直接输出有解. 否则. 假设1,2最后会在一条直线上,则把这条直线上的点都删掉. 看看剩余的点是否在同 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. Swoole练习 UDP

    UDP 服务代码 <?php //创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP $serv = new swoole_server(&quo ...

  2. git下载仓库的部分目录

    有这样的需求,比如某个仓库里包含可执行文件[编译后的文件]或jar包之类的,他们太大我不需要而且我自己可以编译或导入: 或者是某个仓库是自己专门用来放demos的,里面有很多的demo项目,我可能只想 ...

  3. 传输json数据到前台的时候,数据中包含日期数据

    问题描述 当从数据库中查询的数据中包含有日期格式的数据的时候,数据传输到前台会报错. 解决方式 // 逐条将日期进行格式化后再传输 Date date = new SimpleDateFormat(& ...

  4. ubuntu下安装amqp扩展

    目录 环境 下载扩展: 安装amqp: 验证 环境 系统 ubuntu 16.04 php 7.1 下载扩展: sudo apt-get -y install gcc make autoconf li ...

  5. netty-websocket简单的使用方法

    最便捷使用netty-websocket方法 1.pom添加依赖 <dependency> <groupId>org.yeauty</groupId> <ar ...

  6. 人工智能对人类有哪些影响 选择Python入门怎样

    人工智能对人类有哪些影响?选择Python入门怎样?人工智能是科技时代进步的产物,也是目前人们非常关注的一个产业.那么,随着人工智能的发展,对人类生活的有哪些影响呢? 1.人工智能对文化产业影响 据了 ...

  7. Linux shell 中 & && [] [[]] () [] 含义

    | 语法:command 1 | command 2 功能:把第一个命令 command 1 执行的结果作为 command 2 的输入传给 command 2 & & 放在启动参数后 ...

  8. 元组的简单介绍——参考Python编程从入门到实践

    元组 用于存储一系列不可修改的元素 1. 元组的定义 dimensions = (200, 50) # 定义一个元组,即将元素用圆括号括起来 print(dimensions[0]) # 打印元组中的 ...

  9. Redis string操作命令

    字符串类型  string set 从v2.6.12版本开始,Redis增强了set功能, 语法如下: SET key value [EX seconds] [PX milliseconds] [NX ...

  10. vue导入css,js和放置html代码

    使用场景:我是从网上找的html前端页面模板,导入找的模板中的css和js到vue中使用. 1.在main.js中全局导入css和js import '@/assets/css/main.css' i ...