• B. Tell Your World(几何数学 + 思维)


    B. Tell Your World
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    input
    Copy
    5
    -1 -2 0 0 -5
    output
    Copy
    No
    input
    Copy
    5
    5 4 3 2 1
    output
    Copy
    No
    input
    Copy
    5
    1000000000 0 0 0 0
    output
    Copy
    Yes
    Note

    In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.

    In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.

    In the third example, it's impossible to satisfy both requirements at the same time.

    算法:几何数学 + 思维

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    #define INF 0x3f3f3f3f
    const int maxn = 1e5+7;
    
    ll a[maxn];
    int n;
    
    int solve(double k) {
        int pos = -1;
        for(int i = 2; i <= n; i++) {
            if(a[i] - a[1] == (i - 1) * k ) {
                continue;
            }
            if(pos == -1) {
                pos = i;        //确定一个新的基点
            } else if(a[i] - a[pos] != (i - pos) * k){
                return 0;
            }
        }
        return pos != -1;   //判断是否是所有的点都在一条直线上
    }
    
    int main() {
        while(~scanf("%d", &n)) {
            for(int i = 1; i <= n; i++) {
                cin >> a[i];
            }
            //以三点来确定三条直线,有以下三种情况
            double k1 = a[2] - a[1];        
            double k2 = 1.0 * (a[3] - a[1]) / 2;
            double k3 = a[3] - a[2];
            if(solve(k1) || solve(k2) || solve(k3)) {
                printf("Yes
    ");
            } else {
                printf("No
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    各IDE快捷键
    java的GUI之SWT框架 JavaFX框架 配置开发环境(包含但不限于WindowBuilder完整教程,解决Unknown GUI toolkit报错,解决导入SWT包错误)
    20180314 一个浮点数问题
    20180309 算最近新的感悟吧
    20171228 C#值类型和引用类型
    20171129 ASP.NET中使用Skin文件
    20171123初学demo爬去网页资料
    20171018 在小程序页面去获取用户的OpenID
    20171018 微信小程序客户端数据和服务器交互
    20171012 动态爬虫爬取预约挂号有号信息
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/11312083.html
Copyright © 2020-2023  润新知