zoukankan      html  css  js  c++  java
  • codeforce 849B

    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
    5
    7 5 8 6 9
    output
    Yes
    input
    5
    -1 -2 0 0 -5
    output
    No
    input
    5
    5 4 3 2 1
    output
    No
    input
    5
    1000000000 0 0 0 0
    output
    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.

    这题看了高手的代码才会。

    题意:给出n个点的坐标,问这n个点能否在都在两条平行的直线上。

    (注意题面第二行给出了横坐标- - 我都是一直以为没有横坐标,读了好久题才发现)

    解题思路:两条平行线的斜率k是一样的,对前三个坐标a[1],a[2],a[3]进行处理,因为要求每个点都在线上,那么这三个点之间的斜率:

    k1=a[2]-a[1];

    k2=a[3]-a[2];

    k3=(a[3]-a[1])/2;

    肯定至少有一个是直线的真正斜率(这点可以画个图仔细想想),再根据直线方程y=kx+b,把这三个斜率对所有点套一下,如果只有

    两个不相等的b值,则输出Yes,否则输出No。

    附代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <set>
     5 using namespace std;
     6 const int M = 1111;
     7 double nu[M];
     8 int n;
     9 bool solve(double k){
    10     set<double>s;
    11     for(int i=1;i<=n;i++){
    12         s.insert(nu[i]-i*k);
    13     }
    14         return s.size()==2;
    15 }
    16 int main(){
    17     scanf("%d",&n);
    18     for(int i=1; i<=n; i++) scanf("%lf",&nu[i]); 
    19     if(solve(nu[2]-nu[1]) || solve(nu[3]-nu[2]) || solve((nu[3]-nu[1])/2))
    20     printf("Yes");
    21     else printf("No"); 
    22     return 0;
    23 }
    View Code
  • 相关阅读:
    MySQL优化查询语句Explain
    Spring Kafka(二)操作Topic以及Kafka Tool 2的使用
    集群作业管理OpenPBS与OpenPBS Interface Library
    OpenPBS 脚本样本 非常值得参考
    mpirun 与 PBS 问题
    MPIRUN 与 PBS 脚本
    什么是IA架构服务器
    集群的管理pbs
    软件包 javax.servlet 不存在
    An introduction to PORTABLE BATCH SYSTEM
  • 原文地址:https://www.cnblogs.com/zmin/p/7465974.html
Copyright © 2011-2022 走看看