B. Tell Your World(计算几何+思维)

题目链接:http://codeforces.com/contest/849/problem/B

Connect the countless points with lines, till we reach the faraway yonder.

There are n points on a coordinate plane, the i-th of which being (i, yi).

Determine whether it’s possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
Input

The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.

The second line contains n space-separated integers y1, y2, …, yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output

Output “Yes” (without quotes) if it’s possible to fulfill the requirements, and “No” otherwise.

You can print each letter in any case (upper or lower).
Examples
Input
Copy

5
7 5 8 6 9

Output
Copy

Yes

分析:
题意很简单,把给定的点分成两个直线。
判断是否在同一条直线上,求K就好。
我们考虑前三点的情况。
如果是输出yes的话,只可能是一条直线穿过点a[1]和a[2]或者a[1]和a[3]或者a[2]和a[3]。

所以我们考虑这三条直线的k。我们发现k的值一定为整数或者是一个带0.5的小数。所以不用担心浮点问题。
我们得到了k。在判断所以在k上的直线情况就可以了。

#include"stdio.h"
#include"string.h"
#include"map"
#include"algorithm"
using namespace std;
typedef long long ll;

int n;
ll a[1010];

int solve(double k)
{
    int p = -1;
    for(int i = 2; i <= n; i ++)
    {
        if(a[i] - a[1] == (i - 1) * k) continue;
        if(p == -1)
        {
            p = i; continue;
        }
        if(a[i] - a[p] != (i - p) * k) return 0;
    }
    return p != -1;
}

int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n;i ++)
        scanf("%lld",&a[i]);
    int i;

    if(solve((double)a[2] - a[1]) || solve((double)a[3] - a[2]) || solve(((double)a[3] - a[1]) / 2.0))
        printf("Yes\n");
    else
        printf("No\n");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值