zoukankan      html  css  js  c++  java
  • CF Round 431 B. Tell Your World

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

    题目大意:给出N个整数y[n],第i个整数代表二维坐标系中一个点(i, y[i]),现在问是否能用两条平行的不同直线穿过他们所有点并且每条线至少一个点。 N <= 1000

    解题思路:一条直线上任意两个点的斜率都是一样的,我们可以认为第一个点在第一条直线上,那么枚举2~n每个点i,计算斜率k,认为第一个点和第i个点在一条直线上,然后将所有在这条直线上的点(与第一个点斜率为k的点)标记,之后判断所有未标记的点是否构成一条斜率为k的直线即可。

    注意两种特殊情况的处理。

    代码:

     1 const int maxn = 1e6 + 5;
     2 int n;
     3 int y[maxn];
     4 bool vis[maxn];
     5 
     6 void solve(){
     7     bool flag = true;
     8     double tmk = (y[2] - y[1]) / 1.0;
     9     for(int i = 2; i <= n; i++){
    10         if((y[i] - y[i - 1]) / 1.0 != tmk){
    11             flag = false;
    12             break;
    13         }
    14     }
    15     if(flag){
    16         puts("No");
    17         return;
    18     }
    19     flag = true;
    20     tmk = (y[3] - y[2]) / 1.0;
    21     if(tmk != (y[2] - y[1]) / 1.0){
    22         for(int i = 3; i <= n; i++){
    23             if((y[i] - y[i - 1]) / 1.0 != tmk){
    24                 flag = false;
    25                 break;
    26             }
    27         }
    28     }
    29     else flag = false; 
    30     if(flag){
    31         puts("Yes");
    32         return;
    33     }
    34     for(int i = 2; i <= n; i++){
    35         vis[1] = 1; vis[i] = true;
    36         double ks = (y[i] - y[1]) / (double)(i - 1);
    37         for(int j = i + 1; j <= n; j++){
    38             if((y[j] - y[1]) / (double)(j - 1) == ks){
    39                 vis[j] = 1;
    40             }
    41         }
    42         int lj = inf;
    43         flag = true;
    44         for(int j = 2; j <= n; j++){
    45             if(vis[j]) continue;
    46             if(lj == inf) lj = j;
    47             else{
    48                 if((y[j] - y[lj]) / (double)(j - lj) != ks){
    49                     flag = false;
    50                     break;
    51                 }
    52             }
    53         }
    54         if(flag){
    55             puts("Yes");
    56             return;
    57         }
    58         memset(vis, 0, sizeof(vis));
    59     }
    60     puts("No");
    61 }
    62 int main(){
    63     scanf("%d", &n);
    64     for(int i = 1; i <= n; i++) scanf("%d", &y[i]);
    65     solve();
    66 }
    67 
    68  

    题目:

    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 points1, 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.

  • 相关阅读:
    Mininet介绍及安装
    第9周学习进度
    团队效绩计划
    第一阶段冲刺个人博客10
    第一阶段冲刺个人博客09
    第一阶段冲刺个人博客08
    win10自带中文输入法的用户体验
    团队项目冲刺第十天
    构建之法阅读笔记02
    团队项目冲刺第九天
  • 原文地址:https://www.cnblogs.com/bolderic/p/7465855.html
Copyright © 2011-2022 走看看