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. 代码内存泄露检测(1) MLeaksFinder (Wechat开源) + FBRetainCycleDetector (FaceBook开源)

    每次项目编译完成之后,都被内存搞得头昏脑胀,压力甚大. 利用两周时间,稍微研究了 微信开源的 MLeaksFinder 和 facebook 开源的 FBMemoryProfiler, 这两个开源三方 ...

  2. Django:使用模态框新增数据,成功后提示“提交成功”,并刷新表格bootstrap-table数据

    废话不说先看图:  代码实现: 前台代码: {% load staticfiles %} <!DOCTYPE html> <html lang="en"> ...

  3. SecureCRT配色方案(转)

    1. 设置背景颜色和字体颜色: 选项(Options)==>会话选项(Sessions options)==>终端(Terminal)==>仿真(Emulation)  选项(Opt ...

  4. 继400G后,QSFP-DD800G会是下一个风口吗?

    数据中心市场作为光通信企业的主要战场,近三年400G的热度一直都在持续,虽有Facebook F16继续选用100G架构给市场泼了一些冷水等插曲存在,但近日随着阿里巴巴硅光400G QSFP-DD D ...

  5. Linux-通过源码安装某个版本的内核

    一下操作步骤来自于各种搜索出来的杂七杂八的东西.主要来自:https://blog.csdn.net/happyfreeangel/article/details/85088706 前置环境:Cent ...

  6. HDU 4352:XHXJ's LIS

    题目:(原题是英文而且很迷) 求区间内数的LIS长度==k的个数,比如153948的LIS为1 3 4 8,长度为4.据说这种题叫DP中DP,本来是线性,再套一层状压+数位,简直厉害到不行…… 线性的 ...

  7. Kubernetes(k8s)目录

    Kubernetes(k8s)目录 Kubernetes集群安装(亲测有效)

  8. [转帖]再谈IO的异步,同步,阻塞和非阻塞

    再谈IO的异步,同步,阻塞和非阻塞 https://yq.aliyun.com/articles/53674?spm=a2c4e.11155435.0.0.48bfe8efHUE8wg   krypt ...

  9. [转帖]我最近研究了hive的相关技术,有点心得,这里和大家分享下。

    我最近研究了hive的相关技术,有点心得,这里和大家分享下. https://www.cnblogs.com/sharpxiajun/archive/2013/06/02/3114180.html 首 ...

  10. Struts笔记3

    struts标签 form表单标签 Action:请求地址.直接写动作名称,不用写contextPath <s:form action="/user/register.action&q ...