CodeForces 358A - Dima and Continuous Line(模拟)

A. Dima and Continuous Line
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.

The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn’t have any).

1

Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.

Input
The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, …, xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.

Output
In the single line print “yes” (without the quotes), if the line has self-intersections. Otherwise, print “no” (without the quotes).

Examples
input
4
0 10 5 15
output
yes
input
4
0 15 5 10
output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right.

题意:
给你在数轴上多个点,根据给出的次序将给出的点连接起来,判断是否连线有相交(连线为半圆,直径就是两点间的距离).

解题思路:
模拟,判断情况:
设上一组小的是a,大的是b
这一组小的是c,大的是d
那么 c > a && c < b && d > b || d > a && d < b && c < a 的时候,才会有相交的情况.

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int qdu[1000];
    for(int i = 0;i < n;i++)    scanf("%d",&qdu[i]);
    for(int i = 0;i < n-1;i++)
    {
        for(int j = i+1;j < n-1;j++)
        {
            int a = min(qdu[i],qdu[i+1]);
            int b = max(qdu[i],qdu[i+1]);
            int c = min(qdu[j],qdu[j+1]);
            int d = max(qdu[j],qdu[j+1]);
            if((c>a&&c<b&&d>b)||(d>a&&d<b&&c<a))
            {
                printf("yes\n");
                return 0;
            }
        }
    }
    printf("no\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值