Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n, x[1001]; cin >> n; if (n <= 3) { cout << "no"; return 0; } for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n - 1; i++) { a = min(x[i], x[i + 1]); b = max(x[i], x[i + 1]); for (int j = 0; j < n - 1; j++) { if (a > min(x[j], x[j + 1]) && b > max(x[j], x[j + 1]) && a < max(x[j], x[j + 1])) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
from sys import stdin,stdout input=stdin.readline import math,bisect n=int(input()) l=list(map(int,input().split())) if n<=2: print("no") else: f=0 for i in range(1,n): x1=min(l[i-1],l[i]) x2=max(l[i-1],l[i]) for j in range(i+1,n): x3=min(l[j-1],l[j]) x4=max(l[j-1],l[j]) if x1<x3<x2<x4: f=1 elif x3<x1<x4<x2: f=1 if f==0: print("no") else: print("yes")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c, d; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { a = min(arr[i], arr[i + 1]); b = max(arr[i], arr[i + 1]); c = min(arr[j], arr[j + 1]); d = max(arr[j], arr[j + 1]); if ((c < a && a < d && b > d) || (a < c && c < b && b < d)) { cout << "yes" << endl; return 0; } } } cout << "no" << endl; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; int x[10000]; int res = 0; int x1, x2, x3, x4; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x[i]); } for (int i = 1; i <= n - 1; i++) { for (int j = i + 2; j < n; j++) { x1 = min(x[i], x[i + 1]); x2 = max(x[i], x[i + 1]); x3 = min(x[j], x[j + 1]); x4 = max(x[j], x[j + 1]); if ((x3 > x1 && x3 < x2 && x4 > x2) || (x1 > x3 && x1 < x4 && x2 > x4)) { res = 1; break; } } if (res == 1) break; } if (res == 1) printf("yes\n"); else printf("no\n"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Codeforces { private static Print writer; private static Scan reader; static class Scan { private byte[] buf = new byte[1024]; // Buffer of Bytes private int index; private InputStream in; private int total; public Scan() throws IOException { in = System.in; } private int next() throws IOException { // Scan method used to scan buf if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } int ni() throws IOException { int integer = 0; int n = next(); while (isWhiteSpace(n)) // Removing startPointing whitespaces n = next(); int neg = 1; if (n == '-') { // If Negative Sign encounters neg = -1; n = next(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = next(); } else throw new InputMismatchException(); } return neg * integer; } long nl() throws IOException { long integer = 0; int n = next(); while (isWhiteSpace(n)) // Removing startPointing whitespaces n = next(); int neg = 1; if (n == '-') { // If Negative Sign encounters neg = -1; n = next(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = next(); } else throw new InputMismatchException(); } return neg * integer; } String line() throws IOException { StringBuilder sb = new StringBuilder(); int n = next(); while (isWhiteSpace(n)) n = next(); while (!isNewLine(n)) { sb.append((char) n); n = next(); } return sb.toString(); } private boolean isNewLine(int n) { return n == '\n' || n == '\r' || n == -1; } private boolean isWhiteSpace(int n) { return n == ' ' || isNewLine(n) || n == '\t'; } int[] nia(int n) throws Exception { if (n < 0) throw new Exception("Array size should be non negative"); int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = ni(); return array; } int[][] n2dia(int r, int c) throws Exception { if (r < 0 || c < 0) throw new Exception("Array size should be non negative"); int[][] array = new int[r][c]; for (int i = 0; i < r; i++) array[i] = nia(c); return array; } long[] nla(int n) throws Exception { if (n < 0) throw new Exception("Array size should be non negative"); long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nl(); return array; } float[] nfa(int n) throws Exception { if (n < 0) throw new Exception("Array size should be non negative"); float[] array = new float[n]; for (int i = 0; i < n; i++) array[i] = nl(); return array; } double[] nda(int n) throws Exception { if (n < 0) throw new Exception("Array size should be non negative"); double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nl(); return array; } } static class Print { private final BufferedWriter bw; Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void print(T str) { try { bw.append(str.toString()); } catch (IOException e) { System.out.println(e.toString()); } } void println() { print("\n"); } <T> void println(T str) { print(str + "\n"); } void flush() throws IOException { bw.flush(); } void close() { try { bw.close(); } catch (IOException e) { System.out.println(e.toString()); } } } public static void main(String[] args) throws Exception { long startPointTime = System.currentTimeMillis(); reader = new Scan(); writer = new Print(); main2(); long endTime = System.currentTimeMillis(); float totalProgramTime = endTime - startPointTime; if (args.length > 0 && args[0].equals("time")) writer.print("Execution time is " + totalProgramTime + " (" + (totalProgramTime / 1000) + "s)"); writer.close(); } static class DisjointSet { int[] arr; int[] size; DisjointSet(int n) { arr = new int[n + 1]; size = new int[n + 1]; makeSet(); } void makeSet() { for (int i = 1; i < arr.length; i++) { arr[i] = i; size[i] = 1; } } void union(int i, int j) { if (i == j) return; if (i > j) { i ^= j; j ^= i; i ^= j; } i = find(i); j = find(j); if (i == j) return; arr[j] = arr[i]; size[i] += size[j]; size[j] = size[i]; } int find(int i) { if (arr[i] != i) { arr[i] = find(arr[i]); size[i] = size[arr[i]]; } return arr[i]; } int getSize(int i) { i = find(i); return size[i]; } public String toString() { return Arrays.toString(arr); } } static long MOD = 1_000_000_007L; static class Line { int from; int to; Line(int from, int to) { if (from > to) { this.from = to; this.to = from; } else { this.from = from; this.to = to; } } public String toString() { return "(" + from + "," + to + ")"; } } static void main2() throws Exception { int n = reader.ni(); Line[] arr = new Line[n - 1]; int from = reader.ni(); for (int i = 0; i < n - 1; i++) { int toto = reader.ni(); arr[i] = new Line(from, toto); from = toto; } //writer.println(Arrays.toString(arr)); for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (intersects(arr, i, j)) { writer.println("yes"); return; } } } writer.println("no"); } // from always <= to static boolean intersects(Line[] arr, int i, int j) { // case 1 check if (arr[i].to <= arr[j].from || arr[j].to <= arr[i].from) { return false; } // case 5 else if ((arr[j].from <= arr[i].from && arr[j].to >= arr[i].to) || (arr[i].from <= arr[j].from && arr[i].to >= arr[j].to)) { return false; } // case 2 else if (arr[i].from == arr[j].from || arr[i].to == arr[j].to) { return false; } return true; } static int min(int... arr) { int min = Integer.MAX_VALUE; for (int var : arr) { min = Math.min(min, var); } return min; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(input()) a=[*map(int,input().split())] a=list(zip(a,a[1:])) for i in range(n-1): for j in range(n-1): if i==j: continue if min(a[i][0],a[i][1])<min(a[j][0],a[j][1])<max(a[i][0],a[i][1])<max(a[j][0],a[j][1]): print('yes') exit(0) print('no')
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
# Link: http://codeforces.com/problemset/problem/358/A # Love you Atreyee my life. I cannot live without you. n = int(input()) li = list(map(int, input().rstrip().split())) flag = 0 for i in range(0, n - 1): x = min(li[i], li[i + 1]) y = max(li[i], li[i + 1]) for j in range(0, n - 1): a = min(li[j], li[j + 1]) b = max(li[j], li[j + 1]) if (a < x and x < b and b < y) or (x < a and a < y and y < b): flag = 1 break if flag == 1: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int n, u, v, r, R, x, m, i, j; pair<int, int> a[1010]; int main() { cin >> n; if (n <= 2) { cout << "no"; return 0; } cin >> u >> v; x = u + v; r = u > v ? 2 * u - x : x - 2 * u; a[++m] = make_pair(x, r); for (n -= 2; n; n--) { cin >> x; u = v; v = x; x = u + v; r = u > v ? 2 * u - x : x - 2 * u; a[++m] = make_pair(x, r); } for (i = 1; i < m; i++) for (j = i + 1; j <= m; j++) { x = a[i].first > a[j].first ? a[i].first - a[j].first : a[j].first - a[i].first; R = max(a[i].second, a[j].second); r = min(a[i].second, a[j].second); if (x == 0) { if (r == R) { cout << "yes"; return 0; } continue; } if (R >= x + r) continue; if (x >= r + R) continue; cout << "yes"; return 0; } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() v = list(map(int, raw_input().split())) w = [(min(v[x], v[x+1]), max(v[x], v[x+1])) for x in range(0, n-1)] for i in range(0, len(w)): for j in range(0, len(w)): if i == j: continue a, b = w[i][0], w[i][1] c, d = w[j][0], w[j][1] if a < c and c < b and (d < a or d > b): print 'yes' exit() print 'no' ####
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) lst = [int(x) for x in input().split()] a = lst[:] a = sorted(a) f = 0 for i in range(n - 1): for j in range(n - 1): if i != j: x1 = [lst[i], lst[i + 1]] x2 = [lst[j], lst[j + 1]] x1 = sorted(x1) x2 = sorted(x2) if x1[0] < x2[0] < x1[1] < x2[1]: f = 1 if f == 1: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265359; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int b, q, li, mi, a[100005], i, j, k = 0, x = 0, l, m, n, f = 0, x1, x2, x3, x4; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n - 1; i++) { x1 = min(a[i], a[i + 1]); x2 = max(a[i + 1], a[i]); for (j = 0; j < n - 1; j++) { x3 = min(a[j], a[j + 1]); x4 = max(a[j], a[j + 1]); if (x1 < x3 && x3 < x2 && x2 < x4) { cout << "yes"; return 0; } else if (x3 < x1 && x1 < x4 && x4 < x2) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Dima{ int x; int y; public Dima(int a, int b){ x=a; y=b; } public static void main(String[] args) throws IOException{ BufferedReader br; br = new BufferedReader(new InputStreamReader(System.in)); int i,j; String lecture = br.readLine(); int n=Integer.parseInt(lecture); lecture= br.readLine(); String[] spliteada=lecture.split(" "); int[] x= new int[n]; for (i=0;i<n;i++){ x[i]=Integer.parseInt(spliteada[i]); } Dima[] puntos= new Dima[n-1]; int max,min; for (i=0;i<n-1;i++){ max=(int)Math.max(x[i],x[i+1]); min=(int)Math.min(x[i],x[i+1]); puntos[i]=new Dima(min,max); } for (i=0;i<n-1;i++){ for (j=0;j<n-1;j++){ if ((puntos[i].y>puntos[j].x ) && (puntos[i].x<puntos[j].x)&&(puntos[i].y<puntos[j].y)){ System.out.println("yes"); System.exit(0); } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int prev1 = 0; int smin, smax, min1, max1; bool b2 = false; int check(int k) { if (k > min1 && k < max1) { smin = min1; min1 = k; smax = max1; prev1 = 1; b2 = true; } else if (k < min1) { if (b2 != true) { max1 = smin; smin = k; min1 = k; } else { max1 = min1; min1 = k; } prev1 = 2; } else if (k > max1) { if (b2 != true) { min1 = smax; smax = k; max1 = k; } else { min1 = max1; max1 = k; } prev1 = 3; } return prev1; } int main() { int n; cin >> n; bool b1 = true; int num1; int num2; cin >> num1 >> num2; min1 = min(num1, num2); max1 = max(num1, num2); int w; cin >> w; if (w > min1 && w < max1) { smin = min1; min1 = w; smax = max1; prev1 = 1; b2 = true; } else if (w < min1) { smin = w; smax = max1; max1 = min1; min1 = w; prev1 = 2; } else if (w > max1) { smin = min1; min1 = max1; smax = w; max1 = w; prev1 = 3; } for (int i = 1; i < n - 2; i++) { int l; cin >> l; if (b1) { if (prev1 == 1) { if (l < smin || l > max1) b1 = false; else prev1 = check(l); } else if (prev1 == 2) { if ((l > max1 && l < smax) || (b2 == true && smin > l)) b1 = false; else prev1 = check(l); } else if (prev1 == 3) { if ((l > smin && l < min1) || (b2 == true && smax < l)) { b1 = false; } else prev1 = check(l); } } } if (b1) cout << "no"; else cout << "yes"; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); int[] v = new int[n]; for (int i = 0; i < n; i++) v[i] = in.nextInt(); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < i; j++) { if (intersect(v[i], v[i + 1], v[j], v[j + 1])) { System.out.println("yes"); return; } } } System.out.println("no"); } private static boolean intersect(int a1, int a2, int b1, int b2) { if (a1 > a2) { int temp = a1; a1 = a2; a2 = temp; } if (b1 > b2) { int temp = b1; b1 = b2; b2 = temp; } if (b1 < a1 && b2 > a1 && b2 < a2) return true; if (b1 > a1 && b1 < a2 && b2 > a2) return true; return false; } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; public class Main { static class num { int small,big; } public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); if(n==1) { System.out.println("no"); return; } num arr[]=new num[n]; int prev=sc.nextInt(); for(int i=1;i<n;i++) { int b=sc.nextInt(); num m=new num(); m.small=Math.min(prev,b); m.big=Math.max(prev,b); arr[i]=m; prev=b; } for(int i=1;i<n;i++) { for(int j=1;j<n;j++) { if(i!=j && arr[i].big>arr[j].small && arr[j].small> arr[i].small && arr[i].big<arr[j].big) { System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int l[] = new int[n]; int r[] = new int[n]; int point[] = new int[n]; for(int i = 0; i < n; i++) { point[i] = in.nextInt(); } for(int i = 0; i < n - 1; i++) { l[i] = point[i]; r[i] = point[i + 1]; if(l[i] > r[i]) { int t = l[i]; l[i] = r[i]; r[i] = t; } } for(int i = 0; i < n - 1; i++) for(int j = i + 1; j < n - 1; j++) if(isIntersect(l[i], r[i], l[j], r[j])) { out.println("yes"); return ; } out.println("no"); } public boolean isIntersect(int l1, int r1, int l2, int r2) { if(l2 > l1 && l2 < r1 && r2 > r1) return true; if(l1 > l2 && l1 < r2 && r1 > r2) return true; return false; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) a = [int(y) for y in input().split()] flag = 0 for i in range(len(a)-2): for j in range(i, 0, -1): r = min(a[j], a[j-1]) s = max(a[j], a[j-1]) if (a[i+2] > r and a[i+2] < s) and (a[i+1] > s or a[i+1] < r): flag += 1 break elif (a[i+1] > r and a[i+1] < s) and (a[i+2] > s or a[i+2] < r): flag += 1 break if(flag == 0): print("no") else: print("yes")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) arr = list(map(int,input().split())) for i in range(n-1): for j in range(i+1,n-1): a = min(arr[i], arr[i+1]) b = max(arr[i], arr[i+1]) c = min(arr[j], arr[j+1]) d = max(arr[j], arr[j+1]) if a < c < b < d or c < a < d < b: print("yes") exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int n, x[1010]; void read() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &x[i]); } bool check() { for (int i = 1; i < n; ++i) for (int j = i + 1; j < n; ++j) { int a = min(x[i], x[i + 1]), b = max(x[i], x[i + 1]), c = min(x[j], x[j + 1]), d = max(x[j], x[j + 1]); if (c < a && a < d && d < b) return true; if (a < c && c < b && b < d) return true; } return false; } int main() { read(); printf("%s\n", check() ? "yes" : "no"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) lis = list(map(int,input().split())) if(n < 3): print("no") else: flag = 0 for i in range(1,n): if(lis[i-1] > lis[i]): r = lis[i-1] l = lis[i] else: l = lis[i-1] r = lis[i] for j in range(i+1,n): if(lis[j-1] > lis[j]): xr = lis[j-1] xl = lis[j] else: xl = lis[j-1] xr = lis[j] if xl > l and xl < r and xr > r: flag = 1 break if xr < r and xr > l and xl < l: flag = 1 break if flag == 1: break if(flag == 0): print("no") else: print("yes")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int z, t, counter = 0; cin >> z; for (int i = 0; i < z; i++) { cin >> t; v.push_back(t); } for (int i = 1; i < z; i++) { int a, b; a = v[i]; b = v[i - 1]; for (int j = 1; j < z; j++) { int x, y; x = v[j]; y = v[j - 1]; if (x < a && a < y) { if (b > y || b < x) { counter++; } } if (x > a && a > y) { if (b > x || b < y) { counter++; } } if (x < b && b < y) { if (a > y || a < x) { counter++; } } if (x > b && b > y) { if (a > x || a < y) { counter++; } } } if (counter > 0) { break; } } if (counter > 0) { cout << "yes" << endl; } else cout << "no" << endl; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; int arr[1009]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); int x1, y1, x2, y2; for (int i = 0; i < n - 1; i++) { x1 = min(arr[i], arr[i + 1]); y1 = max(arr[i], arr[i + 1]); for (int j = i + 1; j < n - 1; j++) { x2 = min(arr[j], arr[j + 1]); y2 = max(arr[j], arr[j + 1]); if ((x1 < x2 && y1 > x2 && y2 > y1) || (x2 < x1 && y2 > x1 && y1 > y2)) { printf("yes\n"); return 0; } } } printf("no\n"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } boolean b = false; for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-1; j++) { if(Math.max(arr[i],arr[i+1])<Math.max(arr[j],arr[j+1]) && Math.min(arr[i],arr[i+1])<Math.min(arr[j],arr[j+1]) && Math.max(arr[i],arr[i+1])>Math.min(arr[j],arr[j+1])) { b = true; break; } } } if (b) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(st.nextToken()); } int[][] line = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { line[i][0] = nums[i]; line[i][1] = nums[i + 1]; Arrays.sort(line[i]); } boolean ok = false; for (int i = 0; i < line.length && !ok; i++) for (int j = 0; j < line.length && !ok; j++) ok |= (line[i][0] < line[j][0] && line[i][1] > line[j][0] && line[i][1] < line[j][1]); if (ok) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; bool bol = false; for (int i = 0; i < n - 1; i++) { for (int j = i + 2; j < n - 1; j++) { int a, b, c, d; a = min(arr[i], arr[i + 1]); b = max(arr[i], arr[i + 1]); if (arr[j] > a && arr[j] < b && !(arr[j + 1] > a && arr[j + 1] < b)) { bol = true; break; } if (arr[j + 1] > a && arr[j + 1] < b && !(arr[j] > a && arr[j] < b)) { bol = true; break; } } if (bol) break; } if (bol) cout << "yes\n"; else cout << "no\n"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.util.*; /** * * @author Altynbek Nurgaziyev */ public class A { public void solution() throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] f = new int[n]; for (int i = 0; i < n; i++) { f[i] = in.nextInt(); } for (int i = 0; i < n - 1; i++) { int r1 = Math.max(f[i], f[i + 1]); int l1 = Math.min(f[i], f[i + 1]); for (int j = 0; j < n - 1; j++) { int r2 = Math.max(f[j], f[j + 1]); int l2 = Math.min(f[j], f[j + 1]); if (l1 < l2 && l2 < r1 && r1 < r2) { System.out.println("yes"); return; } if (l2 < l1 && l1 < r2 && r2 < r1) { System.out.println("yes"); return; } } } System.out.println("no"); } public static void main(String[] args) throws Exception { new A().solution(); } public class Scanner { private BufferedReader br; private StringTokenizer st; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public Scanner(FileReader fr) { br = new BufferedReader(fr); } public String next() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public Integer nextInt() throws Exception { return Integer.parseInt(next()); } public Long NextLong() throws Exception { return Long.parseLong(next()); } public Double nextDouble() throws Exception { return Double.parseDouble(next()); } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; struct ab { int a, b; }; int Shalga(int x1, int y1, int x2, int y2) { if (x1 > y1) swap(x1, y1); if (x2 > y2) swap(x2, y2); if (x1 > x2) { swap(x1, x2); swap(y1, y2); } if (x2 > x1 && x2 < y1 && y1 < y2) return 1; else return -1; } int main() { int k, n, x, y; ab line[10001]; k = 0; cin >> n; cin >> x; for (int i = 1; i < n; i++) { cin >> y; for (int j = 0; j < k; j++) { if (Shalga(x, y, line[j].a, line[j].b) == 1) { cout << "yes" << endl; return 0; } } line[k].a = x; line[k].b = y; k++; x = y; } cout << "no" << endl; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1111; int x[maxn]; int main() { int n, x1, x2, x3, x4, i, j; bool flag = false; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x[i]); } for (i = 0; i < n - 1; i++) { x1 = x[i], x2 = x[i + 1]; if (x1 > x2) swap(x1, x2); for (j = i + 1; j < n - 1; j++) { x3 = x[j], x4 = x[j + 1]; if (x3 > x4) swap(x3, x4); if (x1 < x3 && x3 < x2 && x2 < x4) { flag = true; } if (x3 < x1 && x1 < x4 && x4 < x2) { flag = true; } } } if (flag) cout << "yes" << endl; else cout << "no" << endl; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
def BinarySearch(arr, ele, lft, r): a = arr toSearch = ele left = lft right = r ans = 0 while left <= right: mid = int(left + ((right - left) / 2)) if a[mid] < toSearch: left = mid + 1 else: ans = a[mid] right = mid - 1 return ans n = int(input()) l = list(map(int, input().strip().split(" "))) s = sorted(l) dicti = dict({}) if len(l) == 1: print("no") else: for i in range(len(l) - 1): dicti[l[i]] = l[i + 1] dicti[l[-1]] = l[-2] flag = 0 for i in range(len(l) - 1): if l[i] > l[i + 1]: x = BinarySearch(s, l[i + 1] + 1, 0, len(l)) else: x = BinarySearch(s, l[i] + 1, 0, len(l)) if max(l[i], l[i + 1]) > x > min(l[i + 1], l[i]): if x in dicti.keys(): if min(l[i], l[i + 1]) <= dicti[x] <= max(l[i], l[i + 1]): pass else: flag = 1 break if flag == 1: print("yes") else: print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int N; int A[1002]; bool inters; int main() { cin >> N; for (int i = 1; i <= N; ++i) cin >> A[i]; for (int i = 1; i <= N - 1; ++i) for (int j = 1; j <= N - 1; ++j) { int L1 = min(A[i], A[i + 1]), L2 = min(A[j], A[j + 1]); int R1 = max(A[i], A[i + 1]), R2 = max(A[j], A[j + 1]); if (L1 < L2 && L2 < R1 && R1 < R2) inters = true; } if (inters) cout << "yes\n"; else cout << "no\n"; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int num; cin >> num; vector<int> tmp(num, 0); for (int i = 0; i < num; ++i) { cin >> tmp[i]; } if (num <= 2) { cout << "no"; return 0; } vector<int> s; vector<int> e; for (int i = 1; i < num; ++i) { s.push_back(min(tmp[i - 1], tmp[i])); e.push_back(max(tmp[i], tmp[i - 1])); } for (int i = 0; i < s.size() - 1; ++i) { for (int j = i + 1; j < s.size(); ++j) { if ((s[i] < s[j] && s[j] < e[i] && e[i] < e[j]) || (s[j] < s[i] && s[i] < e[j] && e[j] < e[i]) || (s[i] < e[j] && e[j] < e[i] && e[i] < s[j]) || (s[j] < e[i] && e[i] < e[j] && e[j] < s[i])) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: orhan */ public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] points = new int[n]; for(int i = 0; i<n; i++) { points[i] = sc.nextInt(); } boolean result = true; for(int i = 0; i<n-1 && result; i++) { int min = Math.min(points[i], points[i+1]); int max = Math.max(points[i], points[i+1]); for(int j = 0; j<i && result; j++) { int min2 = Math.min(points[j], points[j+1]); int max2 = Math.max(points[j], points[j+1]); if(min < min2 && max > min2 && max < max2) result = false; else if(min > min2 && min < max2 && max > max2) result = false; } } if(!result) System.out.println("yes"); else System.out.println("no"); sc.close(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) x = list(map(int, input().split())) intervals = [] for i in range(n-1): interval = [x[i], x[i+1]] interval.sort() a, b = interval intervals.append((a,b)) def intersect(t1, t2): combined = [t1, t2] combined.sort() interval1, interval2 = combined if interval2[0] < interval1[1] < interval2[1] and interval1[0] != interval2[0]: return True else: False condition = False for i in range(n-1): for j in range(i+1,n-1,1): if intersect(intervals[i], intervals[j]): condition = True break if condition: break print("yes" if condition else "no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(input()) s=raw_input().split(" ") l=[int(x) for x in s] i=n if(i==1 or i==2 or i==3): print "no" else: left=l[2] i=3 flag=0 # print left while(i<n): # print i left=l[i-1] right=l[i] j=0 k=0 if(left>right): j=right k=left else: j=left k=right i1=0 while(i1<i): if(l[i1]>j and l[i1]<k): leftone=0 if(i1==0): leftone=j else: leftone=l[i1-1] rightone=l[i1+1] # print leftone,rightone if(leftone < j or leftone>k or rightone<j or rightone > k ): # print leftone,rightone print "yes" flag=1 break i1+=1 i+=1 if(flag==1): break if(flag==0): print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(input()) a=list(map(int,input().split())) p=[] for i in range(n-1): p.append([min(a[i],a[i+1]),max(a[i],a[i+1])]) for i in p: for j in p: if i[0]<j[0]<i[1]<j[1] or j[0]<i[0]<j[1]<i[1]: print("yes") exit() print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) a = list(map(int, input().split())) if n <= 3: print('no') else: fg = 0 for i in range(n-3): for j in range(i+2, n-1): if min(a[i], a[i+1]) < min(a[j], a[j+1]) < max(a[i], a[i+1]) < max(a[j], a[j+1]) or min(a[j], a[j+1]) < min(a[i], a[i+1]) < max(a[j], a[j+1]) < max(a[i], a[i+1]): fg = 1 break; if fg: break; if fg: print('yes') else: print('no')
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
def check_conf(p1, p2): if p1[0] > p1[1]: p1[0], p1[1] = p1[1], p1[0] if p2[0] > p2[1]: p2[0], p2[1] = p2[1], p2[0] if (p1 == p2): return True return not((p1[0] <= p2[0] and p1[1] >= p2[1]) or \ (p2[0] <= p1[0] and p2[1] >= p1[1]) or \ p1[1] <= p2[0] or p1[0] >= p2[1]) def solve(n, p): for xid in xrange(n - 1): pair_x = [p[xid], p[xid + 1]] for yid in xrange(xid): pair_y = [p[yid], p[yid + 1]] if check_conf(pair_x, pair_y): return True return False n = input() print 'yes' if solve(n, map(int, raw_input().split())) else 'no'
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() a = map(int, raw_input().split()) ans = "no" for i in xrange(n-1): for j in xrange(n-1): x1, x2 = sorted([a[i], a[i+1]]) x3, x4 = sorted([a[j], a[j+1]]) if x1 < x3 < x2 < x4: ans = "yes" print ans
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(raw_input()) a = map(int, raw_input().split()) for i in xrange(n-1): lo, ro = min(a[i],a[i+1]), max(a[i],a[i+1]) for j in xrange(n-1): ld, rd = min(a[j],a[j+1]), max(a[j], a[j+1]) if lo < ld < ro < rd: print "yes" exit() print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); double a[]=new double[n]; for(int i=0; i<n; i++){ a[i]=sc.nextDouble(); } boolean x = false; boolean result=false; for(int i=0; i<n-3; i++){ for(int j=i+2; j<n-1; j++){ Double p1= (a[i]-a[j])*(a[i]-a[j+1]); Double p2= (a[i+1]-a[j])*(a[i+1]-a[j+1]); Double p3 = p1*p2; if(p3<0){ result=true; x=true; break; } else { continue; } } if(x) break; } if(result==true) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; public class Main { static final int N = 1010; static int n; static int[] arr = new int[N]; static Node[] nodes = new Node[N]; static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static class Node { int l, r; public Node(int l, int r) { this.l = l; this.r = r; } } public static void main(String[] args) throws IOException { // st = new StreamTokenizer( // new BufferedReader(new InputStreamReader(new FileInputStream("/Users/huangweixuan/testdata.txt")))); st.nextToken(); n = (int)st.nval; for (int i = 0; i < n; ++i) { st.nextToken(); arr[i] = (int)st.nval; } int a, b, t; for (int i = 1; i < n; ++i) { a = arr[i - 1]; b = arr[i]; if (a > b) { t = a; a = b; b = t; } nodes[i - 1] = new Node(a, b); } --n; Node t1, t2; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { t1 = nodes[i]; t2 = nodes[j]; if (t1.l < t2.l && t1.r < t2.r && t1.r > t2.l || t1.r > t2.r && t1.l > t2.l && t1.l < t2.r) { bw.write("yes"); bw.close(); return; } } } bw.write("no"); bw.close(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=input() p=map(int, raw_input().split()) flag=0 for i in xrange(len(p)-1): for j in xrange(len(p)-1): x=sorted([p[i],p[i+1]]) y=sorted([p[j],p[j+1]]) if (x[0]<y[0] and y[0]<x[1]<y[1]): flag=1 break if (y[0]<x[0] and x[0]<y[1]<x[1]): flag=1 break if flag: print "yes" else: print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (max(a[i], a[i + 1]) < max(a[j], a[j + 1]) && min(a[i], a[i + 1]) < min(a[j], a[j + 1]) && max(a[i], a[i + 1]) > min(a[j], a[j + 1])) { cout << "yes"; return 0; } } } cout << "no"; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int min(int x, int y) { if (x < y) return x; else return y; } int max(int x, int y) { if (x > y) return x; else return y; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (a[j] > min(a[i], a[i + 1]) && a[j] < max(a[i], a[i + 1]) && (a[j + 1] < min(a[i], a[i + 1]) || a[j + 1] > max(a[i], a[i + 1])) || (a[j + 1] > min(a[i], a[i + 1]) && a[j + 1] < max(a[i], a[i + 1]) && (a[j] < min(a[i], a[i + 1]) || a[j] > max(a[i], a[i + 1])))) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int fx[] = {+1, -1, +0, +0}; const int fy[] = {+0, +0, +1, -1}; void solve() { long long int n, l = 0; cin >> n; vector<pair<long long int, long long int> > v(n); for (long long int i = 0; i < n; i++) { long long int j; cin >> j; if (!i) v[i].first = j; else if (i == n - 1) v[i - 1].second = j; else { v[i].first = j; v[i - 1].second = j; } } n--; for (long long int i = 0; i < n; i++) if (v[i].first > v[i].second) swap(v[i].first, v[i].second); for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < i; j++) { if (((v[i].first - v[j].first) * (v[i].second - v[j].first) < 0) && ((v[i].first - v[j].second) * (v[i].second - v[j].second) > 0)) { l = 1; break; } else if (((v[i].first - v[j].second) * (v[i].second - v[j].second) < 0) && ((v[i].first - v[j].first) * (v[i].second - v[j].first) > 0)) { l = 1; break; } } if (l) break; } if (l) cout << "yes" << '\n'; else cout << "no" << '\n'; return; } signed main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) solve(); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() a = map(int, raw_input().split()) def solve(): for i in xrange(n-1): for j in xrange(n-1): x1, x2 = sorted([a[i], a[i+1]]) x3, x4 = sorted([a[j], a[j+1]]) if x1 < x3 < x2 and x3 < x2 < x4: return "yes" return "no" print solve()
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.math.BigInteger; import java.util.StringTokenizer; import java.util.regex.Pattern; public class BinarySearch { BufferedReader in; StringTokenizer tokenizer; PrintWriter out; public class Intervals { public int min; public int max; public int length; Intervals (int i, int j) { min = Math.min(i, j); max = Math.max(i, j); length = max - min; } } public int signature (int i) { if (i>0) return 1; if (i==0) return 0; return -1; } public static void main(String[] args) throws IOException { new BinarySearch().run(); } //this code is to run public void good() throws IOException { int numbPoints = Integer.parseInt(in.readLine()); tokenizer = new StringTokenizer(in.readLine()); int[] array = new int[numbPoints]; for (int i = 0; i< numbPoints; i++) { array[i] = Integer.parseInt(tokenizer.nextToken()); } Intervals[] massive = new Intervals[numbPoints-1]; for (int i = 0; i< massive.length; i++) { massive[i] = new Intervals(array[i], array[i+1]); } boolean answer = true; for (int i = 0; i < numbPoints-1; i++) { for (int j = 0; j < numbPoints-1; j++) { if (i != j) { int sign = signature(massive[i].max -massive[j].max); sign*=signature(massive[i].min-massive[j].min); sign*=signature(massive[i].min -massive[j].max); sign*=signature(massive[i].max-massive[j].min); if (sign < 0) { answer = false; //System.out.println(i+" "+j); } } } } String answ = new String(""); answ+= answer?"no":"yes"; System.out.println(answ); } public void run() { try { Reader reader = new InputStreamReader(System.in); Writer writer = new OutputStreamWriter(System.out); in = new BufferedReader(reader); out = new PrintWriter(writer); good(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; import static java.lang.System.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class Main{ static PrintStream ps; static boolean inside(int l1, int r1, int l2, int r2){ return (l2>=l1 && r2<=r1) || (l1>=l2 && r1<=r2); } // static boolean touch(int l1, int r1, int l2, int r2){ // return r1==l2 || r2==l1; // } static boolean outside(int l1, int r1, int l2, int r2){ return r1<=l2 || r2<=l1; } public static void main(String[] args)throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(in)); ps = new PrintStream(new BufferedOutputStream(out)); StringTokenizer st; // st = new StringTokenizer(br.readLine()); while(true){ String s = br.readLine(); if(s==null) break; st = new StringTokenizer(s); int n = Integer.valueOf(st.nextToken()); int[] pt = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0; i<n; i++) pt[i] = Integer.valueOf(st.nextToken()); boolean nointersection = true; for(int i=0; i<n-1; i++){ for(int j=i+1; j<n-1; j++){ int l1 = min(pt[i], pt[i+1]); int r1 = max(pt[i], pt[i+1]); int l2 = min(pt[j], pt[j+1]); int r2 = max(pt[j], pt[j+1]); if(!outside(l1, r1, l2, r2) && !inside(l1, r1, l2, r2)){ // ps.printf("[%d,%d] [%d,%d]\n", l1, r1, l2, r2); nointersection = false; } } } if(!nointersection){ ps.println("yes"); }else{ ps.println("no"); } } ps.flush(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.math.*; import java.util.*; import org.omg.CORBA.Environment; //Codeforces public class MainCodeforces1 { private static MyScanner in; private static PrintStream out; private static boolean LOCAL_TEST = false; private static void solve() throws IOException { int n = in.nextInt(); int[] x = new int[n]; for (int i = 0; i < x.length; i++) { x[i] = in.nextInt(); } boolean intersect = false; for (int i = 0; i < n - 1; i++) { int iL = Math.min(x[i], x[i + 1]); int iR = Math.max(x[i], x[i + 1]); for (int j = 0; j < n - 1; j++) { int jL = Math.min(x[j], x[j + 1]); int jR = Math.max(x[j], x[j + 1]); if (i == j) continue; if (jL > iL && jL < iR && jR > iR) intersect = true; if (jL < iL && jR > iL && jR < iR) intersect = true; if (intersect) break; } if (intersect) break; } if (intersect) out.println("yes"); else out.println("no"); } public static void main(String[] args) throws IOException { // helpers for input/output out = System.out; try { String cname = System.getenv("COMPUTERNAME"); if (!cname.equals("")) LOCAL_TEST = true; } catch (Exception e) { } if (LOCAL_TEST) { in = new MyScanner("E:\\zin.txt"); } else { boolean usingFileForIO = false; if (usingFileForIO) { // using input.txt and output.txt as I/O in = new MyScanner("input.txt"); out = new PrintStream("output.txt"); } else { in = new MyScanner(); out = System.out; } } solve(); } // ===================================== static class MyScanner { BufferedReader bufReader; StringTokenizer strTok; public MyScanner() throws IOException { bufReader = new BufferedReader(new InputStreamReader(System.in)); strTok = new StringTokenizer(""); } public MyScanner(String inputFile) throws IOException { bufReader = new BufferedReader(new InputStreamReader( new FileInputStream( inputFile))); strTok = new StringTokenizer(""); } String GetNextToken() throws IOException { if (!strTok.hasMoreTokens()) strTok = new StringTokenizer(bufReader.readLine()); return strTok.nextToken(); } public int nextInt() throws IOException { return Integer.valueOf(GetNextToken()); } public long nextLong() throws IOException { return Long.valueOf(GetNextToken()); } public double nextDouble() throws IOException { return Double.valueOf(GetNextToken()); } public String nextString() throws IOException { return GetNextToken(); } public String nextLine() throws IOException { return bufReader.readLine(); } public int countTokens() { return strTok.countTokens(); } public boolean hasMoreTokens() { return strTok.hasMoreTokens(); } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class CF358A { static class Pair { int x; int y; public Pair (int x, int y) {this.x = x;this.y = y;}} public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out)); int n = sc.nextInt(); int [] p = new int [n]; int [] pS = new int [n]; for (int i = 0; i < p.length; i++) { int in = sc.nextInt(); p[i] = in; pS[i] = in; } boolean intersect = false; ArrayList<Pair> edges = new ArrayList<>(); for (int i = 0; i < pS.length-1; i++) { int bef = p[i]; int next = p[i+1]; if(inRange(bef,next,edges)) { intersect = true; break; } else{ int first = Math.min(p[i],p[i+1]); int second = Math.max(p[i],p[i+1]); edges.add(new Pair(first,second)); } } if(intersect) System.out.println("yes"); else System.out.println("no"); } private static boolean inRange(int bef, int next, ArrayList<Pair> edges) { for (int i = 0; i < edges.size(); i++) { Pair e = edges.get(i); if((next < e.y && next > e.x && (bef < e.x || bef > e.y)) || (bef < e.y && bef > e.x && (next < e.x || next > e.y))) return true; } return false; } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#http://codeforces.com/problemset/problem/358/A #not accepted n = eval(input()) points = [(int)(i) for i in input().split()] poles = [] ''' left = 0 right = 0 prevDir = '-' currentDir = '-' ''' def is_intersect(points): if(n <= 2): return False; global poles poles.append([points[0],points[1]]) for i in range(2,n): prevPoint = points[i-1] currentPoint = points[i] for pole in poles: _min = min(pole[0],pole[1]) _max = max(pole[0],pole[1]) if (prevPoint in range(_min+1,_max) and\ currentPoint not in range(_min,_max+1)) or\ (currentPoint in range(_min+1,_max) and\ prevPoint not in range(_min,_max+1)): return True poles.append([prevPoint,currentPoint]); return False ''' global right global left global prevDir global currentDir left = min(points[0],points[1]) right = max(points[0],points[1]) prevDir = points[1] > points[0] if 'right' else 'left' currentDir = '-' for i in range(2,n): prevPoint = points[i-1] currentPoint = points[i] currentDir = currentPoint > prevPoint if 'right' else 'left' if prevDir == currentDir: if currentDir == 'left': right = prevPoint; left = currentPoint; elif currentDir == 'right': right = currentPoint left = prevPoint else: pass prevDir = currentDir return False ''' if is_intersect(points): print('yes') else: print('no')
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { static final int INF = (int) 1e9; public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); int l1 = -INF, l2 = -INF, l3 = -INF, r1 = INF, r2 = INF, r3 = INF, last = 0; boolean cross = false; for(int i = 1; i < n; i++) { if(a[i] > a[i-1]) { if(a[i] > r1 && a[i] < r2 || a[i] > r3) { cross = true; break; } if(last == 0) { l1 = a[0]; l2 = a[0]; } else if(a[i] < r1 && last == 2) { l3 = a[i-1]; r3 = r1; l2 = l3; l1 = l3; r2 = r3; } else if(a[i] < r1 && last == 1) l1 = a[i-1]; else if(a[i] > r2 && last == 2) { l1 = r2; l2 = a[i-1]; r1 = r3; r2 = r3; } else if(a[i] > r2 && last == 1) l1 = a[i-1]; last = 1; } else { if(a[i] < l1 && a[i] > l2 || a[i] < l3) { cross = true; break; } if(last == 0) { r1 = a[0]; r2 = a[0]; } else if(a[i] > l1 && last == 1) { r3 = a[i-1]; l3 = l1; r2 = r3; r1 = r3; l2 = l3; } else if(a[i] > l1 && last == 2) r1 = a[i-1]; else if(a[i] < l2 && last == 1) { r1 = l2; r2 = a[i-1]; l1 = l3; l2 = l3; } else if(a[i] < l2 && last == 2) r1 = a[i-1]; last = 2; } } out.println(cross? "yes" : "no"); out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System){br = new BufferedReader(new InputStreamReader(System));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException{return br.readLine();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar()throws IOException{return next().charAt(0);} public Long nextLong()throws IOException{return Long.parseLong(next());} public boolean ready() throws IOException{return br.ready();} public void waitForInput(){for(long i = 0; i < 3e9; i++);} } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, a[1000], i, j; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if ((min(a[i], a[i + 1]) < min(a[j], a[j + 1]) && min(a[j], a[j + 1]) < max(a[i], a[i + 1]) && max(a[i], a[i + 1]) < max(a[j], a[j + 1])) || (min(a[j], a[j + 1]) < min(a[i], a[i + 1]) && min(a[i], a[i + 1]) < max(a[j], a[j + 1]) && max(a[j], a[j + 1]) < max(a[i], a[i + 1]))) { cout << "yes"; return 0; } } } cout << "no"; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
from math import * n=int(input()) li=list(map(int,input().split())) li1=[] #print(len(li)) for i in range(0,n-1): li1.append([li[i],li[i+1]]) for i in li1: i.sort() li1.sort(key=lambda x:x[0]) ans="no" for i in range(1,n-1): for j in range(i): if(li1[i][0]>li1[j][0] and li1[i][0]<li1[j][1]) and (li1[i][1]>li1[j][1]): ans="yes" print(ans)
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; const int max_n = 1011, inf = 1e9 + 100; vector<int> v; bool check(pair<int, int> a, pair<int, int> b) { return a.first < b.first && b.first < a.second && a.second < b.second; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { int a; cin >> a; v.push_back(a); } vector<pair<int, int> > s; for (int i = 0; i + 1 < n; ++i) { pair<int, int> p = {v[i], v[i + 1]}; if (p.first > p.second) { swap(p.first, p.second); } s.push_back(p); } for (int i = 0; i < s.size(); ++i) { for (int j = 0; j < s.size(); ++j) { if (check(s[i], s[j])) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
raw_input() xs = map(int, raw_input().split()) outside = None # outside bounding inside = None # inside obstacles last = None prev_x = xs[0] intersect = False for x in xs[1:]: if (inside is not None and inside[0] < x < inside[1]) or (outside is not None and not (outside[0] < x < outside[1])): intersect = True break if last is not None: if last[0] < x < last[1]: # new x inside last inside = None outside = last else: # new x outside last if inside is not None: inside = (min(inside[0], last[0]), max(inside[1], last[1])) else: inside = last last = (min(prev_x, x), max(prev_x, x)) prev_x = x print "yes" if intersect else "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
__author__ = 'MatFuck' import sys #sys.stdin = open("1.txt") n = input() a = [int(x) for x in raw_input().split()] p = [[0, 0] for i in range(n - 1)] for id in range(n - 1): p[id][0] = min(a [id], a [id + 1]) p[id][1] = max(a [id], a [id + 1]) p = sorted(p, key = lambda ps:(ps[0],ps[1]), reverse = False) ans = 0 #print p for id_a, it_a in enumerate(p): for id_b, it_b in enumerate(p[id_a + 1:]): #if (it_a[1] <= it_b[0]): # break #else: if (it_a[1] < it_b[1]) and (it_a[0] < it_b[0]) and (it_a[1] > it_b[0]): print "yes" sys.exit(0) print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.util.*; public class DimaandContinuousLine { public static void main(String args[]) { FastReader sc= new FastReader(); PrintWriter out = new PrintWriter(System.out); int t,n,i,j,min,max; String s; n=sc.nextInt(); int arr[]=sc.readArray(n); int flag=0; for(i=0;i<n-2;i++) { min=Math.min(arr[i], arr[i+1]); max=Math.max(arr[i], arr[i+1]); for(j=i+1;j<n-1;j++) { int mintemp=Math.min(arr[j],arr[j+1]); int maxtemp=Math.max(arr[j],arr[j+1]); if((mintemp<min && maxtemp<max && maxtemp>min) || (mintemp<max && maxtemp>max && mintemp>min)) { flag=1; break; } } if(flag==1) break; } if(flag==1) out.println("yes"); else out.println("no"); out.close(); } /* FASTREADER */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } /*DEFINED BY ME */ int[] readArray(int n){ int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
from collections import defaultdict import sys n = int(raw_input()) data = raw_input().split() connected = defaultdict(set) data = map(int, data) for i in xrange(len(data) - 1): right = data[i + 1] me = data[i] if me < right: connected[me].add(right) else: connected[right].add(me) #sortedArr = sorted(data) def outOfBounds(connected, x, y): for lower, upperL in connected.iteritems(): for upper in upperL: if x < lower < y < upper: return True if lower < x < upper < y: return True return False for lower, upperL in connected.iteritems(): for upper in upperL: if outOfBounds(connected, lower, upper): print "yes" sys.exit(0) print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
N=input() T=map(int,raw_input().split()) for x in xrange(N-1): a=min(T[x],T[x+1]) b=max(T[x],T[x+1]) for j in xrange(N-1): c=min(T[j],T[j+1]) d=max(T[j],T[j+1]) if(a<c<b<d): print "yes" exit() print "no"
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(input()) l=list(map(int,input().split())) l1=[] for i in range(n-1): l1.append([min(l[i],l[i+1]),max(l[i],l[i+1])]) ans='no' for i in l1: for j in l1: a=i[0] b=i[1] if j[0]<b<j[1] and a<j[0]: ans="yes" break if j[0]<a<j[1] and b>j[1]: ans="yes" break print(ans)
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(input()) x = list(map(int, input().split())) for i in range(n - 1): a = (min(x[i], x[i + 1]), max(x[i], x[i + 1])) for j in range(i): b = (min(x[j], x[j + 1]), max(x[j], x[j + 1])) if b[0] < a[0] < b[1] < a[1] or a[0] < b[0] < a[1] < b[1]: print("yes") exit() print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] in = new int[n]; in[0] = s.nextInt(); boolean found = false; for(int i = 1; !found && i < n; i++) { in[i] = s.nextInt(); int start1 = Math.min(in[i], in[i-1]); int end1 = Math.max(in[i], in[i-1]); for(int j = 1; !found && j < i; j++) { int start2 = Math.min(in[i-j], in[i-j-1]); int end2 = Math.max(in[i-j], in[i-j-1]); found = start1 > start2 && start1 < end2 && end1 > end2; if(found) break; found = end1 > start2 && end1 < end2 && start1 < start2; } } if(found) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int n; vector<int> a, b; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n - 1; i++) { a.push_back(v[i]); b.push_back(v[i + 1]); if (a[i] > b[i]) swap(a[i], b[i]); } bool br = false; for (int i = 0; i < b.size(); i++) { for (int j = 0; j < b.size(); j++) { if (i == j) continue; if (a[i] < a[j] && b[i] < b[j] && b[i] > a[j]) br = true; } } if (br) cout << "yes"; else cout << "no"; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#import math #n, m = input().split() #n = int (n) #m = int (m) #n, m, k = input().split() #n = int (n) #m = int (m) #k = int (k) n = int(input()) #m = int(input()) #s = input() #t = input() g = list(map(int, input().split())) if (n == 1): print("no") #print(l) #c = list(map(int, input().split())) #x1, y1, x2, y2 =map(int,input().split()) #n = int(input()) #f = [] #t = [0]*n #f = [(int(s1[0]),s1[1]), (int(s2[0]),s2[1]), (int(s3[0]), s3[1])] else: f = True mi = min(g[0], g[1]) ma = max(g[0], g[1]) inte = (mi, ma) inte2 = (0,0) for i in range(2, n): #print (mi, ma, inte, inte2) if (g[i] < mi and inte2 == (0,0)): inte = (g[i],mi) inte2 = (0,0) mi = g[i] elif(g[i] > ma and inte2 == (0,0)): inte = (ma,g[i]) inte2 = (0,0) ma = g[i] elif (g[i] < inte[1] and g[i] > inte[0]): a = inte[1] inte = (inte[0], g[i]) inte2 = (g[i], a) elif (g[i] < inte2[1] and g[i] > inte2[0]): inte = (inte2[0], g[i]) inte2 = (g[i], inte2[1]) else: f = False break if (not f): print("yes") else: print("no") #h = [""] * n #f1 = sorted(f, key = lambda tup: tup[0]) #f1 = sorted(t, key = lambda tup: tup[0])
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.Scanner; import java.util.TreeSet; public class A { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] p = new int[n]; TreeSet<Integer> set = new TreeSet<Integer>(); for(int i = 0; i < n; i++){ p[i] = in.nextInt(); set.add(p[i]); } int[][] intervals = new int[n][2]; for(int i =0; i < n-1; i++){ int a = p[i]; int b = p[(i+1)%n]; intervals[i][0] = Math.min(a, b); intervals[i][1] = Math.max(a, b); } boolean legal = true; for(int i = 0; i < n; i++){ for(int k = 0; k < n; k++){ if(k == i) continue; int[] int1 = intervals[i]; int[] int2 = intervals[k]; if(int1[0] > int2[0]){ int[] temp = int2; int2 = int1; int1 = temp; } if(int1[0] < int2[0] && int1[1] < int2[1] && int1[1] > int2[0]){ legal = false; //System.out.println(int1[0] + "," + int1[1] + " and " + int2[0] + "," +int2[1]); } } } if(legal)System.out.println("no"); else System.out.println("yes"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.IOException; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.io.BufferedWriter; import java.util.Locale; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Jacob Jiang */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] x = in.nextIntArray(n); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { int a = x[i], b = x[i + 1]; if (a > b) { int t = a; a = b; b = t; } int c = x[j], d = x[j + 1]; if (c > d) { int t = c; c = d; d = t; } if (a < c && c < b && b < d) { out.println("yes"); return; } } } out.println("no"); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int count) { int[] result = new int[count]; for (int i = 0; i < count; i++) { result[i] = nextInt(); } return result; } } class OutputWriter { private PrintWriter writer; public OutputWriter(OutputStream stream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void println(String x) { writer.println(x); } public void close() { writer.close(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; import java.math.*; import java.lang.*; public class main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); long size=cin.nextLong(); long arr[]=new long[(int)size]; for(int i=0;i<size;++i){ arr[i]=cin.nextLong(); } boolean s=false; long a,b,a_1,b_1; for (int i = 0 ; i < size-1 ; ++ i){ for(int j = 0 ; j < size-1 ; ++ j){ a=Math.min(arr[i],arr[i+1]); b=Math.max(arr[i],arr[i+1]); a_1=Math.min(arr[j],arr[j+1]); b_1=Math.max(arr[j],arr[j+1]); if ((a<a_1&&a_1<b&&b<b_1)||(a_1<a&&a<b_1&&b_1<b)){ s=true; } } } if (s)System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> const int maxn = (int)1e6; const int mod = (int)1e9 + 7; using namespace std; int main() { ios_base::sync_with_stdio(false); int n, sum1 = 0, sum2 = 0; bool ok = false; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; ++i) for (int j = 0; j < n - 1; ++j) { int a1 = min(a[i], a[i + 1]), a2 = max(a[i], a[i + 1]); int b1 = min(a[j], a[j + 1]), b2 = max(a[j], a[j + 1]); if (a1 < b1 && a2 > b1 && b2 > a2) ok = true; } if (ok) puts("yes"); else puts("no"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() v = list(map(int, raw_input().split())) w = [(min(v[x], v[x+1]), max(v[x], v[x+1])) for x in range(0, n-1)] for i in range(0, len(w)): for j in range(0, len(w)): if i == j: continue a, b = w[i][0], w[i][1] c, d = w[j][0], w[j][1] if a < c and c < b and (d < a or d > b): print 'yes' exit() print 'no' #
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import com.sun.org.apache.bcel.internal.generic.AALOAD; import com.sun.org.apache.bcel.internal.generic.GOTO; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.stream.IntStream; import javafx.util.Pair; import static jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java; public class Main { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int n = input.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } for (int i = 0; i < n-1; i++) { int min = Math.min(a[i], a[i+1]); int max = Math.max(a[i], a[i+1]); for (int j = 0; j < n-1; j++) { if((min<a[j]&&a[j]<max&&(a[j+1]<min||a[j+1]>max))||(min<a[j+1]&&a[j+1]<max&&(a[j]<min||a[j]>max))) { System.out.println("yes"); return ; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int n; int v[1111]; pair<int, int> s[1111]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &v[i]); if (i) { s[i] = make_pair(v[i - 1], v[i]); if (s[i].first > s[i].second) swap(s[i].first, s[i].second); } } sort(s + 1, s + n); for (int i = 1; i < n; i++) for (int j = i + 1; j < n; j++) if (s[i].first < s[j].first && s[j].first < s[i].second && s[j].second > s[i].second) { printf("yes\n"); return 0; } printf("no\n"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int n; int x[1005]; int sucess; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x[i]); } sucess = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j || i == n - 1 || j == n - 1) continue; if (x[i] < x[i + 1]) { if (x[i] < x[j] && x[i + 1] > x[j] && x[i + 1] < x[j + 1]) { sucess = 0; break; } if (x[i] < x[j] && x[i + 1] > x[j] && x[i] > x[j + 1]) { sucess = 0; break; } if (x[i] < x[j + 1] && x[i + 1] > x[j + 1] && x[i] > x[j]) { sucess = 0; break; } if (x[i] < x[j + 1] && x[i + 1] > x[j + 1] && x[i + 1] < x[j]) { sucess = 0; break; } } if (x[i] > x[i + 1]) { if (x[i] > x[j] && x[i + 1] < x[j] && x[i] < x[j + 1]) { sucess = 0; break; } if (x[i] > x[j] && x[i + 1] < x[j] && x[i + 1] > x[j + 1]) { sucess = 0; break; } if (x[i] > x[j + 1] && x[i + 1] < x[j + 1] && x[i + 1] > x[j]) { sucess = 0; break; } if (x[i] > x[j + 1] && x[i + 1] < x[j + 1] && x[i] < x[j]) { sucess = 0; break; } } } if (!sucess) break; } if (!sucess) { printf("yes\n"); } else printf("no\n"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > inter; int l, r, n, i, vec[10000000]; bool busca(int x, int a, int b) { int j, fi, se; for (j = 0; j < inter.size(); j++) { fi = inter[j].first, se = inter[j].second; if (a <= fi && fi <= b && a <= se && se <= b) { if (fi != a || se != b) { if (fi < x && x < se) return 0; } } } return 1; } int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &vec[i]); bool sw = 1; l = r = vec[0]; for (i = 1; i < n && sw; i++) { if (vec[i - 1] == l || vec[i - 1] == r) { if (vec[i] < l || vec[i] > r) { if (vec[i] - vec[i - 1] > 0) r = vec[i]; else l = vec[i]; } else { if (inter[inter.size() - 1].first < vec[i] && vec[i] < inter[inter.size() - 1].second) { if (vec[i] - vec[i - 1] > 0) l = vec[i - 1], r = vec[i - 2]; else r = vec[i - 1], l = vec[i - 2]; } sw = busca(vec[i], l, r); } } else { if (vec[i] < l || vec[i] > r) sw = 0; else { if (vec[i] - vec[i - 1] > 0) l = vec[i - 1]; else r = vec[i - 1]; sw = busca(vec[i], l, r); } } inter.push_back({min(vec[i], vec[i - 1]), max(vec[i], vec[i - 1])}); } printf("%s\n", sw ? "no" : "yes"); return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 23; const int MOD = 1e9 + 9; const int ARRS = 1e3 + 100; int arr[ARRS]; bool inside(int x, int a, int b) { return min(a, b) < x && x < max(a, b); } bool outside(int x, int a, int b) { return x < min(a, b) || max(a, b) < x; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; bool cut = false; for (int i = 0; i < n; ++i) { cin >> arr[i]; for (int j = 1; j <= i - 1; ++j) { cut |= inside(arr[j - 1], arr[i - 1], arr[i]) && outside(arr[j], arr[i - 1], arr[i]); cut |= outside(arr[j - 1], arr[i - 1], arr[i]) && inside(arr[j], arr[i - 1], arr[i]); } } cout << (cut ? "yes" : "no") << "\n"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF{public static void main(String[]args)throws IOException{S s=new S();s.s();s.output();}}class S{ void s() throws IOException { int n = NI(); if (n < 4) { sout("no"); return; } int t1 = NI(); Segment[] segments = new Segment[n - 1]; for (int i = 0; i < n - 1; i++) { int t2 = NI(); segments[i] = new Segment(min(t1, t2), max(t1, t2)); t1 = t2; } Arrays.sort(segments, (Segment s1, Segment s2) -> s1.b - s2.b); for (int i = 0; i < n - 2; i++) for (int j = i + 1; j < n - 1 && segments[j].b < segments[i].e; j++) if (segments[j].e > segments[i].e && segments[i].b != segments[j].b) { sout("yes"); return; } sout("no"); } class Segment { int b, e; Segment(int q, int w) { b = q; e = w; } } BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));StringTokenizer st=new StringTokenizer("");long LINF=Long.MAX_VALUE; StringBuilder output=new StringBuilder();final int INF=Integer.MAX_VALUE;void sout(Object x){output.append(x.toString()).append('\n');} void inLong(long[]a,int n){for(int i=0;i<n;i++)a[i]=NL();}int min(int i1,int i2){return i1<i2?i1:i2;}double ND(){return Double.parseDouble(NS());} long min(long i1,long i2){return i1<i2?i1:i2;}int max(int i1,int i2){return i1>i2?i1:i2;}long max(long i1,long i2){return i1>i2?i1:i2;} String NS(){while(!st.hasMoreTokens())try{st=new StringTokenizer(stdin.readLine());}catch(IOException ignored){}return st.nextToken();} int NI(){return Integer.parseInt(NS());}long NL(){return Long.parseLong(NS());}String NLn()throws IOException{return stdin.readLine();} int abs(int x){return x<0?-x:x;}long abs(long x){return x<0?-x:x;}void sout(){output.append('\n');}void out(Object x){output.append(x.toString());} int mod(int x,int mod){return(x+mod)%mod;}void output(){System.out.print(output);}void inInt(int[]a,int n){for(int i=0;i<n;i++)a[i]=NI();} }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; int a[1005]; int n; int main() { bool ans = false; cin >> n >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; if (a[i] < a[i - 1]) v.push_back(make_pair(a[i], a[i - 1])); else v.push_back(make_pair(a[i - 1], a[i])); } sort(v.begin(), v.end()); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < i; j++) { if (v[i].first < v[j].second && v[i].first > v[j].first) if (v[i].second > v[j].second) ans = true; } } if (ans) cout << "yes\n"; else cout << "no\n"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.Scanner; import java.util.TreeSet; public class ContinuousLine { public static void main(String[] args) { Scanner in = new Scanner(System.in); int max = in.nextInt(); TreeSet<Integer> tree = new TreeSet<Integer>(); int minNext = 0; int maxNext = 0; int inOut = 0; while (tree.size() < max) { int next = in.nextInt(); if (inOut > 0 && (next < maxNext && next > minNext)) { System.out.println("yes"); in.close(); return; } if (inOut < 0 && (next > maxNext || next < minNext)) { System.out.println("yes"); in.close(); return; } tree.add(next); if (tree.size() < 3) continue; if (tree.first() == next) { minNext = tree.higher(next); maxNext = tree.last(); inOut = 1; } else if (tree.last() == next) { minNext = tree.first(); maxNext = tree.lower(next); inOut = 1; } else { minNext = tree.lower(next); maxNext = tree.higher(next); inOut = -1; } } System.out.println("no"); in.close(); return; } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String line = stdin.readLine(); int n = Integer.parseInt(line); int[] x = new int[n]; line = stdin.readLine(); String[] prms = line.split(" "); for(int i = 0; i < n; i++){ x[i] = Integer.parseInt(prms[i]); } boolean flg = false; for (int i = 1; i < n; i++) { int x1 = x[i-1]; int x2 = x[i]; boolean flg1 = false; boolean flg2 = false; for (int j = 0; j < i-1; j++) { if ((x1 < x[j] && x[j] < x2) || (x2 < x[j] && x[j] < x1)) { flg1 = true; } else { flg2 = true; } } if (flg1 && flg2) { flg = true; break; } } if (flg) { System.out.println("yes"); } else { System.out.println("no"); } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
//package com.ahashem.active; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class DimaAndContinuousLine { public static void main(String[] args) { int t = 1; // sc.nextInt(); StringBuilder builder = new StringBuilder(); while (t-- > 0) { solve(builder); } out.println(builder.toString()); out.close(); } private static void solve(StringBuilder builder) { final int n = sc.nextInt(); int i = 0; int[] points = new int[n]; while (i < n) { points[i] = sc.nextInt(); i++; } i = 0; Pair[] pairs = new Pair[n - 1]; while (i < n - 1) { pairs[i] = new Pair(points[i], points[i + 1]); i++; } i = 0; while (i < n - 1) { int x = Math.min(pairs[i].getstart(), pairs[i].getend()); int y = Math.max(pairs[i].getstart(), pairs[i].getend()); pairs[i].setstart(x); pairs[i].setend(y); i++; } i = 0; int j; while (i < n - 1) { j = 0; while (j < n - 1) { if (j == i) { j++; continue; } if (intersected(pairs[i], pairs[j])) { builder.append("yes"); return; } j++; } i++; } builder.append("no"); } private static boolean intersected(Pair p1, Pair p2 ) { if (p2.getstart() < p1.getstart() && p2.getend() > p1.getstart() && p2.getend() < p1.getend()) { return true; } if (p2.getend() < p1.getstart() && p2.getstart() < p1.getend() && p2.getstart() > p1.getstart()) { return true; } return false; } static class Pair { int start; int end; public Pair(int start, int end) { this.start = start; this.end = end; } public int getstart() { return start; } public void setstart(int start) { this.start = start; } public int getend() { return end; } public void setend(int end) { this.end = end; } } // Helpers // Math private static boolean isEven(int a) { return a % 2 == 0; } private static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } // IO public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static final FastScanner sc = new FastScanner(); public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } // 1 2 2 2 } private static String twoDimArrayToString(Object[][] array) { return twoDimArrayToString(array, " "); } private static String twoDimArrayToString(Object[][] array, String separator) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { stringBuilder.append(array[i][j]); if (array.length != 1) { if (j < array[i].length - 1) { stringBuilder.append(separator); } } } if (i < array.length - 1) { stringBuilder.append("\n"); } } return stringBuilder.toString(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; import java.math.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); solve(in); } public static void solve(Scanner in){ int n = in.nextInt(); int[] a = new int[n]; int[] l = new int[n]; int[] r = new int[n]; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } for(int i = 0; i < n - 1; i++){ l[i] = a[i]; r[i] = a[i + 1]; if(l[i] > r[i]){ int t = r[i]; r[i] = l[i]; l[i] = t; } } for(int i = 0; i < n - 1;i++){ for(int j = 0; j < n - 1; j++){ if(l[i] > l[j] && l[i] < r[j] && r[i] > r[j]){ System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Solution { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] arr= new int[n]; for (int i=0;i<n;i++){ arr[i]= Integer.parseInt(st.nextToken()); } int[] p1 = new int[2]; int[] p2 = new int[2]; for (int i=0;i<n-1;i++){ if (arr[i]<arr[i+1]){ p1[0]=arr[i]; p1[1]=arr[i+1]; }else{ p1[0]=arr[i+1]; p1[1]=arr[i]; } for (int j=i+2;j<n-1;j++){ if (arr[j]<arr[j+1]){ p2[0]=arr[j]; p2[1]=arr[j+1]; }else{ p2[0]=arr[j+1]; p2[1]=arr[j]; } if ((p1[0]<p2[0]&&p1[1]<p2[1]&&p1[1]>p2[0])||(p1[0]>p2[0]&&p1[0]<p2[1]&&p1[1]>p2[1])){ System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(raw_input()) if n > 2: co = list(map(int,raw_input().split())) check = [[min(co[0],co[1]),max(co[0],co[1])]] for i in xrange(1,n-1): flag = -1 # nahi hoga intersect left = min(co[i],co[i+1]) right = max(co[i],co[i+1]) for j in check: if (j[0] >= right) or (j[0] <=left and right <= j[1]) or (j[1] <= left ) or (j[0] >=left and right >= j[1]) : continue else: flag = 0 break check.append([left,right]) if flag != -1: break if flag == 0: print "yes" else: print "no" else: k = list(map(int,raw_input().split())) print 'no'
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; const long long M = 1e7 + 10; long long modpower(long long x, long long y) { long long ans = 1; while (y > 0) { if (y & 1) ans = (ans % M * x % M) % M; y >>= 1; x = (x % M * x % M) % M; } return ans; } long long power(long long x, long long y) { long long ans = 1; while (y > 0) { if (y & 1) ans *= x; y >>= 1; x *= x; } return ans; } long long nextpowerof2(long long n) { long long ans = 0; while (n > 0) { ans++; n >>= 1; } return pow(2, ans); } long long d[3010]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } vector<pair<long long, long long>> v; for (long long i = 0; i < n - 1; i++) { v.push_back(make_pair(a[i], a[i + 1])); } n = v.size(); bool f = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (i == j) continue; long long x = min(v[i].first, v[i].second); long long y = max(v[i].first, v[i].second); long long r = min(v[j].first, v[j].second); long long c = max(v[j].first, v[j].second); if ((r > x && r < y) && c > y) f = 1; if ((c > x && c < y) && r < x) f = 1; } } if (f) cout << "yes" << "\n"; else cout << "no" << "\n"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#pyrival orz import os import sys from io import BytesIO, IOBase input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) def main(): try: n = inp() a = inlt() if n == 1: print("no") return f = False for i in range(1, n): x1, x2 = min(a[i], a[i-1]), max(a[i], a[i-1]) for j in range(1, n): if i != j: x3, x4 = min(a[j], a[j-1]), max(a[j], a[j-1]) if x1 < x3 < x2 < x4 or x3 < x1 < x4 < x2: f = True break if f: break if f: print("yes") else: print("no") except Exception as e: print(e) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(raw_input()) a=[int(i) for i in raw_input().split()] flag=0 def check(i): l,r=min(a[i],a[i+1]),max(a[i],a[i+1]) for x in xrange(i): L,R=min(a[x],a[x+1]),max(a[x],a[x+1]) if not( (L<=l and r<=R) or (l<=L and R<=r) or r<=L or R<=l): return 1 return 0 for x in xrange(n): if x+1<n: if check(x): print 'yes' exit(0) print 'no'
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n=int(input()) arr=list(map(int,input().split())) for x in range(len(arr)-1): x1,x2=(arr[x],arr[x+1]) if x1>x2: x1,x2=x2,x1 for y in range(x+2,len(arr)-1): x3,x4=(arr[y],arr[y+1]) if x3>x4: x3,x4=x4,x3 if x1<x3<x2<x4 or x3<x1<x4<x2: print("yes") exit() print("no")
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() a = map(int, raw_input().split()) for i in range(n-1): lo, ro = min(a[i], a[i+1]), max(a[i], a[i+1]) for j in range(n - 1): ld, rd = min(a[j], a[j+1]), max(a[j], a[j+1]) if lo < ld < ro < rd: print 'yes' exit() print 'no'
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> int power(int x, int y) { int res = 1; x = x % 1000000007; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res; } int ncr(int n, int r) { int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (int i = 1; i <= n; i++) { int t = i; if (r < t) t = r; for (int j = t; j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[r]; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int a, int b) { return (a / gcd(a, b) * b); } using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - 1; ++j) { int x = a[j], y = a[j + 1]; if (a[j] > a[j + 1]) swap(x, y); if (a[i] > x && a[i] < y) { if (a[i + 1] < x || a[i + 1] > y) { cout << "yes"; return 0; } } if (a[i + 1] > x && a[i + 1] < y) { if (a[i] < x || a[i] > y) { cout << "yes"; return 0; } } } } cout << "no"; return 0; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a[n]; int i, j; for (i = 0; i < n; i++) cin >> a[i]; vector<pair<int, int> > l; for (i = 0; i < n - 1; i++) { if (a[i] < a[i + 1]) { l.push_back(make_pair(a[i + 1], a[i])); } else { l.push_back(make_pair(a[i], a[i + 1])); } } for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if (l[i].second < l[j].second && l[j].second < l[i].first && l[i].first < l[j].second) { cout << "yes"; return 0; } else if (l[j].second < l[i].second && l[i].second < l[j].first && l[j].first < l[i].first) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Created by 875k on 1/3/14. */ public class CF208A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; StringTokenizer tok = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(tok.nextToken()); } for(int i = 0; i < n - 1; i++) { int x = Math.min(a[i], a[i + 1]), y = Math.max(a[i], a[i + 1]); for(int j = 0; j < n - 1; j++) { if(j == i) continue; int u = Math.min(a[j], a[j + 1]), v = Math.max(a[j], a[j + 1]); if((u < x && v < y && v > x) || (u > x && u < y && v > y)) { System.out.println("yes"); System.exit(0); } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
def edge_intersects(edge, l): for other_edge in l: if min(other_edge) < min(edge) and max(other_edge) > min(edge) and max(other_edge) < max(edge): return True if min(other_edge) < max(edge) and min(other_edge) > min(edge) and max(other_edge) > max(edge): return True return False def intersects(l): # Form the edges edges = [(l[i], l[i+1]) for i in range(0, len(l)-1)] for i in range(len(edges) - 1): if edge_intersects(edges[i], edges[i+1:]) == True: return True return False n = input() l = [int(item) for item in input().split()] if intersects(l): print("yes") else: print("no") ##l = [int(item) for item in input().split()] ##print(l)
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.util.*; public class a { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] data = new int[n]; for(int i = 0; i<n; i++) data[i] = input.nextInt(); boolean good = true; for(int i = 0; i<n-1; i++) for(int j = i+1; j<n-1; j++) { int a = data[i], b = data[i+1], c = data[j], d = data[j+1]; if(a>b) { int temp = a; a = b; b = temp; } if(c>d) { int temp = c; c = d; d = temp; } //System.out.println(a+" "+b+" "+c+" "+d); if(a < c && b > c && b < d || a > c && a<d && b>d) good = false; } System.out.println(good ? "no" : "yes"); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
function trim(s) { return s.replace(/^\s+|\s+$/gm, ''); } function tokenize(s) { return trim(s).split(/\s+/); } function tokenizeIntegers(s) { var tokens = tokenize(s); for (var i = 0; i < tokens.length; i += 1) { tokens[i] = parseInt(tokens[i]); }; return tokens; } function main() { var pointNum = parseInt(readline()); var points = tokenizeIntegers(readline()); for (var i = 3; i < pointNum; ++i) { var leftA = Math.min(points[i-1], points[i]), rightA = Math.max(points[i-1], points[i]); for (var j = i-2; j > 0; --j) { var leftB = Math.min(points[j-1], points[j]), rightB = Math.max(points[j-1], points[j]); if (Math.min(rightA, rightB) <= Math.max(leftA, leftB)) { continue; } if (leftA >= leftB && rightA <= rightB) { continue; } if (leftB >= leftA && rightB <= rightA) { continue; } print("yes"); return; } } print("no"); } main();
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import javax.print.attribute.standard.Finishings; public class Dima { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int cases=Integer.parseInt(bf.readLine()); if(cases==1|| cases==2) System.out.println("no"); else{ int [] arr=new int[cases]; ArrayList<Integer> start=new ArrayList<Integer>(); ArrayList<Integer> end=new ArrayList<Integer>(); StringTokenizer st=new StringTokenizer(bf.readLine()); boolean intersect=false; int current_start=Integer.parseInt(st.nextToken()), current_finish=Integer.parseInt(st.nextToken()); start.add(current_start);end.add(current_finish); current_start=current_finish; for (int i = 0; i <cases-2; i++) { current_finish=Integer.parseInt(st.nextToken()); for (int j = 0; j <start.size(); j++) { if(current_finish>current_start){ if(current_start>start.get(j) && current_start <end.get(j) ||//my start is inside the range current_start<start.get(j) && current_start >end.get(j)){ ///if my end os not also inside if(!(current_finish>start.get(j) && current_finish <end.get(j) ||//my start is inside the range current_finish<start.get(j) && current_finish >end.get(j))){ intersect=true;break; } } else{//start not inside if the end is inside so bara if(current_start!= end.get(j)&&(current_finish>start.get(j) && current_finish <end.get(j) ||//my start is inside the range current_finish<start.get(j) && current_finish >end.get(j))){ intersect=true;break; } } } else{//finish<start if(current_start>start.get(j) && current_start<end.get(j) || (current_start <start.get(j) && current_start>end.get(j))){ if(!(current_finish >=start.get(j) && current_finish<= end.get(j))) intersect=true;break; } else if((current_finish >start.get(j) && current_finish <end.get(j) || current_finish<start.get(j)&& current_finish>end.get(j) ) && current_start!=end.get(j)){ intersect=true;break; } // else if(current_start==end.get(j)){ // if(current_finish> start.get(j)){ // intersect=true;break; // } // } } } start.add(current_start); end.add(current_finish); current_start=current_finish; } if(intersect) System.out.println("yes"); else System.out.println("no"); } } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @SuppressWarnings("javadoc") public class DimaAndContinuousLine { public DimaAndContinuousLine() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] split = br.readLine().split(" "); if (n < 4) { System.out.println("no"); return; } int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = 1000000 + Integer.parseInt(split[i]); } int xi1, xi2, xj1, xj2; for (int i = 2; i < x.length-1; i++) { xi1 = Math.min(x[i], x[i+1]); xi2 = x[i]+x[i+1]-xi1; for (int j = 0; j < i-1; j++) { xj1 = Math.min(x[j], x[j+1]); xj2 = x[j]+x[j+1]-xj1; if ((xj1 < xi1 && xj2 > xi1 && xj2 < xi2) || (xi1 < xj1 && xi2 > xj1 && xi2 < xj2)) { System.out.println("yes"); return; } } } System.out.println("no"); } public static void main(String[] args) throws IOException { new DimaAndContinuousLine(); } }
JAVA
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = int(raw_input()) x = map(int, raw_input().split()) def isCrossed(i, j): a, b = min(x[i-1], x[i]), max(x[i-1], x[i]) c, d = min(x[j-1], x[j]), max(x[j-1], x[j]) return a < c < b < d or c < a < d < b crossed = False for i in range(1, n): for j in range(1, n): if isCrossed(i, j): crossed = True break print 'yes' if crossed else 'no'
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n = input() ls = map(int,raw_input().split()) if n<=3: print('no') exit(0) c = False for i in range(n-1): for j in range(i+1,n-1): x1 = min(ls[i],ls[i+1]) x2 = max(ls[i],ls[i+1]) x3 = min(ls[j],ls[j+1]) x4 = max(ls[j],ls[j+1]) if x1<x3<x2<x4 or x3<x1<x4<x2: c = True if c: print('yes') else: print('no')
PYTHON
358_A. Dima and Continuous Line
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). <image> 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.
2
7
n, x = int(input()), list(map(int, input().split())) for i in range(n - 2): for j in range(i + 1, n - 1): a = sorted([sorted([x[i], x[i + 1]]), sorted([x[j], x[j + 1]])]) if a[1][0] > a[0][0] and a[1][0] < a[0][1] and a[1][1] > a[0][1]: print('yes'), exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
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). <image> 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.
2
7
# python 3 """ """ from operator import itemgetter def dima_and_continuous_line(segment_list: list) -> str: segment_list.sort(key=itemgetter(0)) # print(segment_list) for i in range(len(segment_list)): for j in range(i+1, len(segment_list)): if segment_list[i][0] < segment_list[j][0] < segment_list[i][1] < segment_list[j][1]: # print(segment_list[i], segment_list[j]) return "yes" return "no" if __name__ == "__main__": """ Inside of this is the test. Outside is the API """ n = int(input()) points = list(map(int, input().split())) segments = [] for i in range(n-1): if points[i] < points[i+1]: segments.append(tuple((points[i], points[i+1]))) else: segments.append(tuple((points[i+1], points[i]))) # print(segments) print(dima_and_continuous_line(segments))
PYTHON3