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
#!usr/bin/env python def do_points_intersect(xs): if len(xs) <= 2: return "no" seen = [(xs[0], xs[1])] for i in range(2, len(xs)): for s in seen: if ((min(xs[i], xs[i - 1]) < s[0] < max(xs[i], xs[i - 1])) and (s[1] < min(xs[i], xs[i-1]) or s[1] > max(xs[i], xs[i - 1]))) or\ ((min(xs[i], xs[i - 1]) < s[1] < max(xs[i], xs[i - 1])) and (s[0] < min(xs[i], xs[i-1]) or s[0] > max(xs[i], xs[i - 1]))): return "yes" seen.append((xs[i - 1], xs[i])) return "no" if __name__ == '__main__': n = int(raw_input()) xs = map(int, raw_input().split()) print do_points_intersect(xs)
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
if int(input())<3: print("no") exit(0) m=list(map(int,input().split())) s=set([(min(m[0],m[1]),max(m[0],m[1]))]) p=m[1] m=m[2:] for i in m: a,b=min(i,p),max(i,p) for c,d in s: if a<c<b<d or c<a<d<b: print("yes") exit(0) s.add((a,b)) p=i 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 C208 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] ar = new int[n]; for(int i = 0 ; i < n;i++) ar[i]=sc.nextInt(); for( int i = 2 ; i < n;i++) { for(int j=i-1; j>0;j--) if( ( in(ar[i],ar[j],ar[j-1]) && notin(ar[i-1],ar[j],ar[j-1])) || ( (in(ar[i-1],ar[j],ar[j-1]) && notin(ar[i],ar[j],ar[j-1]) ) )){ //System.out.println(i+" "+j); System.out.println("yes"); return; } } System.out.println("no"); sc.close(); } private static boolean notin(int i, int j, int k) { if((i>j && i>k) || (i<k && i<j))return true; return false; } public static boolean in(int x,int y, int z){ if((x>y && x<z) || (x<y && x>z))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 Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner in=new Scanner(System.in); int n,i,j,k,l,p,q,temp,flag=0; n=in.nextInt(); int[] a=new int[n]; for(i=0;i<n;i++) a[i]=in.nextInt(); HashSet<Integer> h=new HashSet<>(); for(i=0;i<n-1;i++) { j=a[i]; k=a[i+1]; if(k<j) { temp=j; j=k; k=temp; } for(l=i+1;l<n-1;l++) { p=a[l]; q=a[l+1]; if(q<p) { temp=p; p=q; q=temp; } if((j<p && p<k && k<q) || (p<j && j<q && q<k)) flag=1; } } if(flag==1) 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.util.*; public class Main { private static final long MOD = 1000000007; public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] arr = new int[n]; read(scan, arr); boolean exists = false; outer: for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { int a1 = Math.min(arr[i], arr[i + 1]); int b1 = Math.max(arr[i], arr[i + 1]); int a2 = Math.min(arr[j], arr[j + 1]); int b2 = Math.max(arr[j], arr[j + 1]); if (b1 <= a2 || b2 <= a1) { continue; } else if ((a1 >= a2 && b1 <= b2) || (a2 >= a1 && b2 <= b1)) { continue; } else { exists = true; break outer; } } } System.out.println(exists ? "yes" : "no"); scan.close(); } private static void print(int... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static void print(long... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static <T> void print(T... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static void read(Scanner scan, int[]... arrs) { int len = arrs[0].length; for (int i = 0; i < len; i++) { for (int[] arr : arrs) { arr[i] = scan.nextInt(); } } } private static void read(Scanner scan, long[]... arrs) { int len = arrs[0].length; for (int i = 0; i < len; i++) { for (long[] arr : arrs) { arr[i] = scan.nextLong(); } } } private static void decreaseByOne(int[]... arrs) { for (int[] arr : arrs) { for (int i = 0; i < arr.length; i++) { arr[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; vector<pair<int, int> > forbidden; bool not_forbidden(int a); int main() { int n; scanf("%d", &n); if (n <= 2) { printf("no\n"); return 0; } int a, b, c, aux; scanf("%d", &a); scanf("%d", &b); c = b; b = max(a, b); a = min(a, c); const int INF = 1000010; for (int i = 0; i < n - 2; i++) { scanf("%d", &aux); if (aux < c) { if (c == a) { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(a, b)); a = aux; b = a; } else { printf("yes\n"); return 0; } } else if (aux > a) { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(-INF, a)); forbidden.push_back(pair<int, int>(b, INF)); b = aux; } else { printf("yes\n"); return 0; } } else { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(a, b)); a = aux; } else { printf("yes\n"); return 0; } } } else { if (c == b) { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(a, b)); a = b; b = aux; } else { printf("yes\n"); return 0; } } else if (aux < b) { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(-INF, a)); forbidden.push_back(pair<int, int>(b, INF)); a = aux; } else { printf("yes\n"); return 0; } } else { if (not_forbidden(aux)) { forbidden.push_back(pair<int, int>(a, b)); b = aux; } else { printf("yes\n"); return 0; } } } c = aux; } printf("no\n"); } bool not_forbidden(int a) { for (int i = 0; i < (int)forbidden.size(); i++) if (forbidden[i].first < a && a < forbidden[i].second) { return false; } return true; }
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 sys import itertools import math import collections from collections import Counter ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0] = prime[1] = False r = [p for p in range(n + 1) if prime[p]] return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) n = ii() d = li() t = [] for i in range(1, n): a, b = min(d[i - 1], d[i]), max(d[i - 1], d[i]) t.append([a, b]) t.sort() for i in range(n - 1): for j in range(i + 1, n - 1): if t[i][0] < t[j][0] < t[i][1] < t[j][1]: exit(print('yes')) 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
#!/usr/bin/env python n = int(raw_input()) x = map(int, raw_input().split()) def inter(p1, p2): a = min(p1) b = max(p1) c = min(p2) d = max(p2) if (a < c < b and b < d) or (c < a and a < d < b): return True return False pairs = zip(x, x[1:]) for p1 in pairs: for p2 in pairs: if inter(p1, p2): 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
num=int(raw_input()) a=map(int, raw_input().split(' ')) flag=0 for i in range(num): if i==0 or i==1 :continue for j in range(i-1): d=max(a[j],a[j+1]) x=min(a[j],a[j+1]) dd=max(a[i-1],a[i]) xx=min(a[i-1],a[i]) if ((d>dd and x>xx and dd>x) or (dd>d and xx>x and d>xx)): print "yes" flag=1 break if(flag==1): break 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
import java.util.*; import java.io.*; public class DimaAndContinousLine { PrintWriter out; StringTokenizer st; BufferedReader br; final int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE; final int mod = 1000000007; void solve() throws Exception { int t = 1; // t = ni(); for (int ii = 0; ii < t; ii++) { int n= ni(); int[] a= ni(n); boolean ans= false; for (int i = 0; i < n-1 && !ans; i++) { int s= a[i], l= a[i+1]; if(s> l) { s= s^l; l= s^l; s= s^l; } for (int j = 0; j < i && !ans; j++) { int S= a[j], L= a[j+1]; if(S> L) { S= S^L; L= S^L; S= S^L; } // out.println(s+" "+l+" "+S+ " "+L); if(L< l && L> s) if(S< s) ans= true; if(S> s && S< l) if(L> l) ans= true; } } print(ans); } } public static void main(String[] args) throws Exception { new DimaAndContinousLine().run(); } void run() throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { File file = new File("C:\\college\\CodeForces\\inputf.txt"); br = new BufferedReader(new FileReader(file)); out = new PrintWriter("C:\\college\\CodeForces\\outputf.txt"); } else { out = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } long ss = System.currentTimeMillis(); st = new StringTokenizer(""); while (true) { solve(); String s = br.readLine(); if (s == null) break; else st = new StringTokenizer(s); } //out.println(System.currentTimeMillis()-ss+"ms"); out.flush(); } void read() throws Exception { st = new StringTokenizer(br.readLine()); } int ni() throws Exception { if (!st.hasMoreTokens()) read(); return Integer.parseInt(st.nextToken()); } char nc() throws Exception { if (!st.hasMoreTokens()) read(); return st.nextToken().charAt(0); } String nw() throws Exception { if (!st.hasMoreTokens()) read(); return st.nextToken(); } long nl() throws Exception { if (!st.hasMoreTokens()) read(); return Long.parseLong(st.nextToken()); } int[] ni(int n) throws Exception { int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = ni(); return ret; } long[] nl(int n) throws Exception { long[] ret = new long[n]; for (int i = 0; i < n; i++) ret[i] = nl(); return ret; } double nd() throws Exception { if (!st.hasMoreTokens()) read(); return Double.parseDouble(st.nextToken()); } String ns() throws Exception { String s = br.readLine(); return s.length() == 0 ? br.readLine() : s; } void print(int[] arr) { for (int i : arr) out.print(i + " "); out.println(); } void print(long[] arr) { for (long i : arr) out.print(i + " "); out.println(); } void print(int[][] arr) { for (int[] i : arr) { for (int j : i) out.print(j + " "); out.println(); } } void print(long[][] arr) { for (long[] i : arr) { for (long j : i) out.print(j + " "); out.println(); } } long add(long a, long b) { if (a + b >= mod) return (a + b) - mod; else return a + b >= 0 ? a + b : a + b + mod; } long mul(long a, long b) { return (a * b) % mod; } void print(boolean b) { if (b) out.println("yes"); else out.println("no"); } long binExp(long base, long power) { long res = 1l; while (power != 0) { if ((power & 1) == 1) res = mul(res, base); base = mul(base, base); power >>= 1; } return res; } long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } // strictly smaller on left void stack_l(int[] arr, int[] left) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < arr.length; i++) { while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop(); if (stack.isEmpty()) left[i] = -1; else left[i] = stack.peek(); stack.push(i); } } // strictly smaller on right void stack_r(int[] arr, int[] right) { Stack<Integer> stack = new Stack<>(); for (int i = arr.length - 1; i >= 0; i--) { while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop(); if (stack.isEmpty()) right[i] = arr.length; else right[i] = stack.peek(); stack.push(i); } } private void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) list.add(i); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(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, x[1000][2], x2, x1; cin >> n >> x1; for (int i = 1; i < n; i++) { cin >> x2; x[i - 1][0] = min(x2, x1); x[i - 1][1] = max(x2, x1); x1 = x2; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if ((x[i][0] < x[j][0]) && (x[i][1] > x[j][0]) && x[i][1] > x[j][0] && (x[i][0] < x[j][1]) && x[i][1] < 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
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Stack; import java.util.StringTokenizer; import java.io.*; public class DimaContinuousLine { private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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; } } public static String solution (int[] arr, int n) { int curr1 = 0; int curr2 = 0; int next1 = 0; int next2 = 0; for(int i = 0; i<n-1; i++) { if(arr[i] < arr[i+1]) { curr1 = arr[i]; curr2 = arr[i+1]; } else { curr1 = arr[i+1]; curr2 = arr[i]; } for(int j = i+1; j<n-1; j++) { if(arr[j] < arr[j+1]) { next1 = arr[j]; next2 = arr[j+1]; } else { next1 = arr[j+1]; next2 = arr[j]; } if( (curr2>next1 && curr2<next2 && curr1<next1) || (curr1>next1 && curr1<next2 && curr2>next2)) return "yes"; } } return "no"; } private static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { MyScanner s = new MyScanner(); int n = s.nextInt(); int[] arr = new int[n]; for(int i =0; i<n; i++) arr[i] = s.nextInt(); out.println(solution(arr,n)); out.flush(); out.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.awt.Point; import java.io.*; public class Main { static StreamTokenizer st; static int get() throws IOException { st.nextToken(); return (int)st.nval; } static class Circle { double r, center; Circle(double r, double center) { this.r = r; this.center = center; } public boolean isCollide(Circle c) { double rast = Math.abs(c.center - center); if(rast >= c.r + r) return false; if(center + r <= c.center + c.r && center - r >= c.center -c.r) return false; if(c.center + c.r <= center + r && c.center - c.r >= center - r) return false; return true; } } static public void main(String [] args) throws IOException { st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int n = get(); Circle [] mas = new Circle[n-1]; int prev = get(); boolean c = true; for(int i = 0; i < n-1; i++) { int cur = get(); double r = Math.abs(cur - prev)/2.0; mas[i] = new Circle(r, (double)Math.min(cur, prev) + r); for(int j = i - 1; j >= 0; j--) { if(mas[j].isCollide(mas[i])) { c = false; } } prev = cur; } if(c) 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 sys n = int(raw_input()) nums = [int(x) for x in raw_input().split()] L = [nums[0]] idx = 0 for i in xrange(1, len(nums)): val = nums[i] if val < L[idx]: if idx == len(L) - 1 and val < L[0]: L.insert(0, val) idx = 0 continue if idx > 0 and val < L[idx - 1]: print 'yes' sys.exit(0) L.insert(idx, val) else: if idx == 0 and val > L[-1]: L.insert(len(L), val) idx = len(L) - 1 continue if idx < len(L) - 1 and val > L[idx + 1]: print 'yes' sys.exit(0) L.insert(idx + 1, val) idx += 1 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 sys n = int(input()) inp = input().split() x = [int(p) for p in inp] for i in range(0,n-2): for j in range(i+1,n-1): # (i,i+1) vs. (j,j+1) al = min(x[i],x[i+1]) ar = max(x[i],x[i+1]) bl = min(x[j],x[j+1]) br = max(x[j],x[j+1]) if al<bl<ar and ar<br or al<br<ar and bl<al: print("yes") sys.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()) x = False A = [int(i) for i in input().split()] for i in range(n-2): x1 , x2= min(A[i],A[i+1]),max(A[i],A[i+1]) for j in range(i+1,n-1): X1,X2 = min(A[j],A[j+1]),max(A[j],A[j+1]) if(x1<X1 and X1<x2 and x2<X2): x = True elif(X1<x1 and X2>x1 and X2<x2): x = True if(x): 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 main() { int a, x, y; vector<pair<int, int>> pa; vector<int> num; cin >> a; for (int i = 0; i < a; i++) { cin >> x; num.push_back(x); if (i > 0) { y = num[i - 1]; pa.push_back(make_pair(min(x, y), max(x, y))); } } for (int i = 0; i < pa.size(); i++) for (int j = 0; j < pa.size(); j++) { if (i == j) continue; pair<int, int> pa1 = pa[i]; pair<int, int> pa2 = pa[j]; if (pa1.first < pa2.first && pa1.second < pa2.second && pa1.first > pa2.second) { cout << "yes\n"; return 0; } if (pa1.first > pa2.first && pa1.second > pa2.second && pa1.first < pa2.second) { cout << "yes\n"; return 0; } } 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
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; import java.awt.geom.*; public class A208 { public static boolean intersect(int a, int b, int c, int d) { if ((Math.min(c, d) > Math.min(a, b) && Math.min(c, d) < Math.max(a, b) && Math.max(c, d) > Math.max(a, b)) || (Math.max(c, d) > Math.min(a, b) && Math.max(c, d) < Math.max(a, b) && Math.min(c, d) < Math.min(a, b))) { return true; } return false; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int i, j; boolean flag = true; int arr[] = new int[n]; for (i = 0; i < n; i++) { arr[i] = in.nextInt(); } if (arr.length <= 3) { System.out.println("no"); } else { for (i = 3; i < arr.length && flag; i++) { j = i - 3; while (j >= 0) { if (intersect(arr[j], arr[j + 1], arr[i], arr[i - 1])) { flag = false; break; } j--; } } if (flag) { 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
#!/usr/bin/env python3 n = int(input()) xs = list(map(int,input().split())) found = False for i in range(n-1): al = min(xs[i], xs[i+1]) ar = max(xs[i], xs[i+1]) for j in range(i): bl = min(xs[j], xs[j+1]) br = max(xs[j], xs[j+1]) if al < bl < ar < br or bl < al < br < ar: found = True break if found: break print(found and 'yes' or '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
/* PROG: a LANG: JAVA */ import java.util.*; import java.io.*; public class a { private void solve() throws Exception { //BufferedReader br = new BufferedReader(new FileReader("a.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] pa = br.readLine().split(" "); int[] a = new int[n - 1]; int[] b = new int[n - 1]; for(int i = 0; i < n - 1; ++i) { int x = Integer.parseInt(pa[i]); int y = Integer.parseInt(pa[i + 1]); a[i] = Math.min(x, y); b[i] = Math.max(x, y); } for(int i = 0; i < n - 1; ++i) { for(int j = 0; j < n - 1; ++j) { if(i != j && a[i] > a[j] && a[i] < b[j] && b[i] > b[j]) { System.out.println("yes"); return; } } } System.out.println("no"); } public static void main(String[] args) throws Exception { new a().solve(); } static void debug(Object...o) { System.err.println(Arrays.deepToString(o)); } }
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 DimaLine { 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 < n; i++) arr[i] = sc.nextInt(); sc.close(); boolean flag = false; if (n == 1) System.out.println("no"); else { for (int i = 0; i < n; i++) { if(i+1<n) { int cMin = Math.min(arr[i],arr[i+1]); int cMax = Math.max(arr[i],arr[i+1]); for(int j = i+2;j<n;j++) { if(j+1<n) { int tempMin = Math.min(arr[j],arr[j+1]); int tempMax = Math.max(arr[j], arr[j+1]); if(tempMin<cMin && (tempMax>cMin && tempMax<=cMax)) { flag = true; break; } if((tempMin>cMin && tempMin<cMax) && (tempMax>cMax)) { flag = true; break; } if(tempMin<cMin) { cMin = tempMin; } if(tempMax>cMax) { tempMax = cMax; } } } } if(flag) break; } if(flag) 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.util.*; public class Intersections { static int inside(int x1, int x2, int a){ if(a<Math.min(x1, x2) || a>Math.max(x1, x2)) return 0; else if (a==x1 || a==x2) return -5; else return 1; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); boolean can=true; int n=scan.nextInt(); int[] ar= new int[n]; for(int i=0; i<n; i++){ ar[i]= scan.nextInt(); for(int j=0; j<i-1; j++){ if((inside(ar[j], ar[j+1], ar[i])+inside(ar[j], ar[j+1], ar[i-1])) ==1) { can=false;break; } } if (can==false) break; } if (can) 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
#include <bits/stdc++.h> using namespace std; int a[1010]; int check(int x, int y, int xx, int yy) { if (x > y) swap(x, y); if (xx > yy) swap(xx, yy); if (y <= xx) return 1; if (yy <= x) return 1; if (x >= xx && y <= yy) return 1; if (x <= xx && y >= yy) return 1; return 0; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { for (int j = i + 1; j < n; j++) { if (check(a[i - 1], a[i], a[j - 1], a[j]) == 0) { printf("yes\n"); return 0; } } } printf("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; vector<int> v; struct point { int l; int r; }; point a[100000]; int n; void sort(int l, int r) { if (l >= r) return; int i = l; int j = r; int mid = a[(i + j) / 2].l; while (i <= j) { while (a[i].l < mid) i++; while (a[j].l > mid) j--; if (i <= j) { if (a[i].l > a[j].l) { swap(a[i], a[j]); i++; j--; } else if (a[i].l == a[j].l && a[i].r >= a[j].r) { swap(a[i], a[j]); i++; j--; } } } sort(l, j); sort(i, r); } int main() { cin >> n; int b[10000]; cin >> a[1].l; for (int i = 2; i <= n; i++) { int k; cin >> k; a[i - 1].r = k; if (a[i - 1].r < a[i - 1].l) swap(a[i - 1].r, a[i - 1].l); a[i].l = k; } for (int i = 1; i <= n - 1; i++) for (int j = 1; j <= n - 1; j++) if (j != i && (a[j].l > a[i].l && a[j].l < a[i].r && a[j].r > a[i].r)) { 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 <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int ts, kk = 1; int n, a[1005]; pair<int, int> q[4]; int main() { int t, i, j, k; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); int m1, m2, m3, m4; for (i = 0; i < n - 1; i++) { m1 = a[i]; m2 = a[i + 1]; if (m1 > m2) swap(m1, m2); for (j = 0; j < n - 1; j++) { if (i + 1 < j || j + 1 < i) { m3 = a[j]; m4 = a[j + 1]; if (m3 > m4) swap(m3, m4); if (m2 < m3 || m4 < m1 || (m1 < m3 && m4 < m2) || (m3 < m1 && m2 < m4)) ; else { 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; const int N = 10000; int x[N]; pair<int, int> points[N]; int n; bool intersect(const pair<int, int>& a, const pair<int, int>& b) { return a.first < b.first && b.first < a.second && a.second < b.second; } int main() { while (cin >> n) { for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 1; i < n; ++i) { int a = min(x[i - 1], x[i]); int b = max(x[i - 1], x[i]); points[i - 1] = pair<int, int>(a, b); } bool is_ok = 1; for (int i = 0; i < n - 1 && is_ok; ++i) for (int j = i + 1; j < n - 1 && is_ok; ++j) { if (intersect(points[i], points[j]) || intersect(points[j], points[i])) is_ok = false; } if (!is_ok) cout << "yes" << endl; else 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; inline int min(int x, int y) { if (x < y) return x; return y; } inline int max(int x, int y) { if (x > y) return x; return y; } int x[1010]; int main() { int n; scanf("%d\n", &n); for (int i = 1; i <= n; ++i) scanf("%d ", &x[i]); bool e = true; for (int i = 3; i < n && e; ++i) { int a = min(x[i], x[i + 1]), b = max(x[i], x[i + 1]); for (int j = i - 1; j >= 1 && e; --j) { if ((x[j] > a && x[j] < b) && (x[j + 1] < a || x[j + 1] > b)) e = false; if ((x[j + 1] > a && x[j + 1] < b) && (x[j] < a || x[j] > b)) e = false; } } if (e) printf("no"); else printf("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; vector<int> ve; int n, e; bool g(int i, int j) { int a, b, c, d; a = ve[i - 1]; b = ve[i]; c = ve[j - 1]; d = ve[j]; if (a > b) { swap(a, b); } if (c > d) { swap(c, d); } int p1, p2, d1, d2; if (b < d && b > c && a < c) { return true; } swap(a, c); swap(b, d); if (b < d && b > c && a < c) { return true; } return false; } bool f() { for (int i = 1; i < n; i++) { for (int j = i + 2; j < n; j++) { if (g(i, j)) return true; } } return false; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> e; ve.push_back(2 * e); } if (f()) { 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 checkint(a,b,c,d): check=0 if min(a,b)<min(c,d)<max(a,b)<max(c,d) or \ min(c,d)<min(a,b)<max(c,d)<max(a,b): check=1 return check n=int(input()) lis=input().split() for i in range(n): lis[i]=int(lis[i]) intersect=0 for i in range(n-1): for j in range(n-1): if checkint(lis[i],lis[i+1],lis[j],lis[j+1])==1: intersect=1 if intersect==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
def take_input(s): #for integer inputs if s == 1: return int(input()) return map(int, input().split()) n = take_input(1) if n == 1: print("no"); exit() a = list(take_input(n)) flag = 0 for i in range(1,n-1): for j in range(i): init_1 = min(a[i],a[i+1]); fin_1 = max(a[i],a[i+1]) init_2 = min(a[j],a[j+1]); fin_2 = max(a[j],a[j+1]) if ((init_1 > init_2 and init_1 < fin_2 and fin_1 > fin_2) or (init_1 < init_2 and fin_1 > init_2 and fin_1<fin_2)) and flag == 0: print("yes") flag = 1 break if flag == 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
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] tokens = br.readLine().split("\\s+"); int n = Integer.parseInt(tokens[0]); int[] x = new int[n]; tokens = br.readLine().split("\\s+"); for(int i = 0; i < n; ++i) x[i] = Integer.parseInt(tokens[i]); for(int i = 0; i < n - 2; ++i) for(int j = i + 1; j < n - 1; ++j) { int x1 = Math.min(x[i], x[i + 1]); int x2 = Math.max(x[i], x[i + 1]); int y1 = Math.min(x[j], x[j + 1]); int y2 = Math.max(x[j], x[j + 1]); if(x1 < y1 && x2 < y2 && x2 > y1 || x2 > y2 && x1 > y1 && x1 < y2) { System.out.println("yes"); System.exit(0); } } System.out.println("no"); } } ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// class FunLib { public static long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } public static int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return a / gcd(a, b) * b; } } abstract class AbstractSegTree { public AbstractSegTree(int aSize) { size = aSize; storage = new int[4 * size]; } public int getVal(int index) { return getValHelper(0, 0, size - 1, index); } public void rangeSet(int lower, int upper, int val) { rangeSetHelper(0, 0, size - 1, lower, upper, val); } protected abstract int getValHelper(int node, int beg, int end, int index); protected abstract void rangeSetHelper(int node, int beg, int end, int lower, int upper, int val); protected int[] storage; protected int size; } class Trie { public Trie() { currentNode = root; } public void reset() { currentNode = root; } public void add(String word) { currentNode = root; for(int i = 0; i < word.length(); ++i) { // Only upper case characters int nodeIndex = word.charAt(i) - 'A'; if(currentNode.children[nodeIndex] == null) currentNode.children[nodeIndex] = new Node(); currentNode = currentNode.children[nodeIndex]; } currentNode.isWord = true; currentNode = root; } public boolean feed(char c) { if(currentNode.children[c - 'A'] == null) return false; currentNode = currentNode.children[c - 'A']; return true; } public boolean isCurrentWord() { return currentNode.isWord; } private static class Node { public boolean isWord; // Only upper case characters public Node[] children = new Node[26]; } private Node root = new Node(); private Node currentNode; }
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Abood3A { public static void main(String[] args) throws IOException { 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[] s = new int[n - 1]; int[] e = new int[n - 1]; for (int i = 0; i < n - 1; i++) { s[i] = Math.min(a[i], a[i + 1]); e[i] = Math.max(a[i], a[i + 1]); } boolean can = true; for (int i = 0; i < s.length; i++) { for (int j = 0; j < s.length; j++) { if(i == j || e[j] <= s[i] || s[j] >= e[i] || (s[i] <= s[j] && e[i] >= e[j]) || (s[j] <= s[i] && e[j] >= e[i])) continue; can = false; } } System.out.println(can ? "no" : "yes"); out.println(); out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
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())) c = [] for i in range(n-1): c.append((min(x[i], x[i+1]), max(x[i], x[i+1]))) for i in range(0, len(c)): for j in range(0, len(c)): if c[i][0] < c[j][0] <c[i][1] < c[j][1] or c[j][0] < c[i][0] <c[j][1] < c[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
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws IOException{ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } for(int i=1;i<n-1;i++) { int minx=arr[i]; int miny=arr[i+1]; int maxx=arr[i]; int maxy=arr[i+1]; for(int j=0;j<i;j++) { if(arr[i]>arr[j]) { minx=arr[j]; miny=arr[j+1]; maxx=arr[i]; maxy=arr[i+1]; } else { minx=arr[i]; miny=arr[i+1]; maxx=arr[j]; maxy=arr[j+1]; } /*if(i==n-2) { System.out.println(maxx+" "+maxy); }*/ if(minx==maxx || miny==maxy || maxx==miny || maxy==minx) { continue; } if(isval(minx,miny,maxx) && !isval(minx,miny,maxy)) { System.out.println("yes"); //System.out.println(i+" "+j); //System.out.println(maxx+" "+maxy); return; } if(!isval(minx,miny,maxx) && isval(minx,miny,maxy)) { System.out.println("yes"); //System.out.println(i+" "+j); //System.out.println(maxx+" "+maxy); return; } } } System.out.println("no"); } static boolean isval(int x,int y,int chk) { int min=Math.min(x, y); int max=Math.max(x, y); return chk>min && chk<max; } }
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, i, x = 0, y = 0, j; long a[1000]; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { x = y = 0; for (j = i + 2; j < n; j++) { if ((a[j] > a[i] && a[j] > a[i + 1]) || (a[j] < a[i] && a[j] < a[i + 1])) { x++; } else { y++; } } if (x && y) { 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; struct unit { long long int a[1000000]; }; long long fact(long long int x) { if (x == 1 || x == 0) return 1; else return x * fact(x - 1); } long long power(long long int x, long long int n) { if (n == 0) return 1; else if (n % 2 == 0) return power(x * x, n / 2); else return x * power(x * x, (n - 1) / 2); } int main() { cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0); long long i, j, k, l, m, n, a = 0, cnt = 0, x; cin >> n; vector<long long int> v; for (i = 0; i < n; i++) { cin >> x; v.push_back(x); } for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n - 1; j++) { k = max(v[i], v[i + 1]); l = min(v[i], v[i + 1]); m = max(v[j], v[j + 1]); a = min(v[j], v[j + 1]); if ((k > a && a > l && m > k) || (a < l && m > l && m < k)) { cnt = 1; cout << "yes"; break; } } if (cnt == 1) break; } if (cnt == 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
n=int(input()) r=[int(c)for c in input().split()] r=[sorted(r[i:i+2])for i in range(n-1)] print(['no','yes'][any(a<c<b<d for a,b in r for c,d in r)])
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.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class _Solution implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } //---------------------------------------- void solve() throws IOException{ int n = readInt(); int[] x = new int[n]; ArrayList<Integer> arrayList = new ArrayList<Integer>(); for (int i =0 ; i < n; i++){ x[i] = readInt(); } if (n <= 2){ out.print("no"); return; } int r = max(x[0], x[1]); int l = min(x[0], x[1]); int rr = r; int ll = l; arrayList.add(x[0]); arrayList.add(x[1]); for (int i = 2; i < n; i++){ arrayList.add(x[i]); if (l < x[i] && x[i] < r){ if (x[i] > x[i-1]){ l = x[i-1]; }else { r = x[i-1]; } continue; } if (x[i] == ll){ l = ll; Collections.sort(arrayList); int ind = Collections.binarySearch(arrayList, ll); r = arrayList.get(arrayList.get(ind+1)); continue; } if (x[i] == rr){ r = rr; Collections.sort(arrayList); int ind = Collections.binarySearch(arrayList, ll); l = arrayList.get(ind-1); continue; } if (x[i] < ll && (x[i-1] == ll || x[i-1] == rr)){ r = ll; ll = min(x[i], ll); l = ll; continue; } if (x[i] > rr && (x[i-1] == ll || x[i-1] == rr)){ l = rr; rr = max(rr, x[i]); r=rr; continue; } out.print("yes"); return; } out.print("no"); } //---------------------------------------- public static void main(String[] args){ new Thread(null, new _Solution(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run(){ try{ timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); 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
n = int(input()) pos = [int(k) for k in input().split()] sigue = True if len(pos)>2: for i in range(len(pos)-2): x1 = pos[i] x2 = pos[i+1] for k in range(i+1,len(pos)-1): x3 = pos[k] x4 = pos[k+1] if x1<x3<x2<x4 or x3<x1<x4<x2 or x1<x4<x2<x3 or x4<x1<x3<x2 or x2<x3<x1<x4 or x3<x2<x4<x1 or x2<x4<x1<x3 or x4<x2<x3<x1: print("yes") sigue = False break if not sigue: break if sigue: 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> struct data { int a, b; }; int main() { int n; int in[1000 + 100]; struct data p[1000 + 100]; while (scanf("%d", &n) != EOF) { int i; for (i = 0; i < n; i++) scanf("%d", &in[i]); for (i = 0; i < n - 1; i++) { p[i].a = in[i]; p[i].b = in[i + 1]; } for (i = 0; i < n - 1; i++) { if (p[i].a > p[i].b) { p[i].a += p[i].b; p[i].b = p[i].a - p[i].b; p[i].a = p[i].a - p[i].b; } } int j; int flag = 1; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n - 1; j++) { if (p[i].a < p[j].a && p[i].b > p[j].a && p[i].b < p[j].b) { flag = 0; break; } else if (p[i].a > p[j].a && p[i].a < p[j].b && p[i].b > p[j].b) { flag = 0; break; } } if (flag == 0) break; } if (flag == 0) 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; const int N = 1e6 + 7; void solve() { int n; cin >> n; int a[n]; for (int i = (0); i < (n); i++) { cin >> a[i]; } if (n > 3) { int L = a[0], R = a[1]; if (L > R) { swap(L, R); } for (int i = (3); i < (n); i++) { int x = a[i - 1], y = a[i]; if ((L < x and x < R) != (L < y and y < R)) { cout << "yes" << endl; return; } L = min(L, x), R = max(R, x); } } cout << "no" << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(); { 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
/* package whatever; // don't place package name! */ import java.util.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } public static int lcm(int a, int b) { return a * (b / gcd(a, b)); } public static ArrayList<Long> tprimes(long n){ //O(n*ln(ln(n))) =~ O(n) boolean [] is_prime = new boolean[(int)n]; ArrayList<Long> ret = new ArrayList<>(); Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false; for(long i = 2 ; i < n ; ++i){ if(is_prime[(int)i] == true){ ret.add(i*i); for(long j = i*i ; j < n ; j+=i){ is_prime[(int)j] = false; } } } return ret; } public static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } public static boolean isPrime(long x){ //O(sqrt(n)) for(long i = 2 ; i*i <= x ; ++i) if(x%i == 0) return false; return true; } public static void dfs(int p) { vis[p] = true; for(int n: li.get(p)) { if(!vis[n]) { dfs(n); } } } public static void bfs(int p) { vis[p] = true; for(int n: li.get(p)) { if(!vis[n]) { vis[n] = true; } } } static boolean[] vis = new boolean[100001]; static List<List<Integer>> li = new ArrayList<>(); public static void main(String[] args) throws java.lang.Exception{ MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int[] arr = new int[n]; int[] x = new int[n-1]; int[] y = new int[n-1]; int cnt = 0; for(int i = 0;i<n;i++) { arr[i] = sc.nextInt(); } for(int i = 0;i<n-1;i++) { x[i] = Math.min(arr[i], arr[i+1]); y[i] = Math.max(arr[i],arr[i+1]); } for(int i = 0;i<n-1;i++) { for(int j = i+1;j<n-1;j++) { if(x[j]<x[i]&&y[j]<y[i]&&(y[j]<y[i]&&y[j]>x[i])) { cnt++; } if(x[j]>x[i]&&y[j]>y[i]&&(x[j]>x[i]&&x[j]<y[i])) { cnt++; } } } if(cnt>0) { out.println("yes"); } else { out.println("no"); } out.close(); } static class MyScanner{ StringTokenizer st; BufferedReader br; public MyScanner(){br = new BufferedReader(new InputStreamReader(System.in));} public MyScanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(s);} public String next() throws IOException{ while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
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/contest/358/problem/A """ from operator import __and__ import sys def compare(startx,endx,starty,endy) : startx,endx = sorted([startx,endx]) starty,endy = sorted([starty,endy]) if startx <= endx <= starty : return True elif starty <= startx <= endx <= endy : return True elif endy <= startx <= endx : return True elif startx <=starty <=endy <=endx : return True else : return False N = int(raw_input()) X = map(int,raw_input().split()) #print X if len(X) < 3 : print "no" sys.exit() L = [(X[0],X[1])] #print L for i in xrange(2,len(X)) : #print X[i-1], X[i] if reduce(__and__,(compare(X[i-1], X[i], pair[0], pair[1]) for pair in L)) : L.append((X[i-1],X[i])) #print L else : print "yes" sys.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()) ar=map(int,raw_input().split()) l=[] for _ in xrange(n-1): l.append([]) for i in xrange(n-1): l[i].append((ar[i+1]+ar[i])/2.0) l[i].append(abs(ar[i+1]-ar[i])/2.0) flag=1 for i in xrange(n-1): for j in range(i+1,n-1): c1=l[i][0] c2=l[j][0] r1=l[i][1] r2=l[j][1] val1=[c1-r1,r1+c1] val2=[c2-r2,r2+c2] #print val1,val2 if (val1[0]<val2[0]<val1[1]<val2[1]) or (val2[0]<val1[0]<val2[1]<val1[1]): #print val1,val2 flag=0 break if flag==0: 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
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, len(v)-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()) p=map(int,raw_input().split()) flag=1 for x in range(0,n-1): for y in range(0,n-1): ma1 = max(p[x],p[x+1]) ma2 = max(p[y],p[y+1]) mi2 = min(p[y],p[y+1]) mi1 = min(p[x],p[x+1]) if((mi2<ma1 and ma1<ma2) and (mi2>mi1 and mi2<ma1)): flag = 0 break if(flag==1): print "no" if(flag==0): print "yes"
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() { int n; cin >> n; int a[1100]; for (int i = 1; i <= n; i++) cin >> a[i]; if (n <= 3) { cout << "no" << endl; return 0; } int f1, f2; for (int i = 1; i <= n; i++) { f1 = 0, f2 = 0; for (int j = i + 2; j <= n; j++) { if (a[j] > a[i] && a[j] > a[i + 1]) { f1 = 1; } else if (a[j] < a[i] && a[j] < a[i + 1]) { f1 = 1; } else f2 = 1; } if (f1 && f2) { 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
import java.util.ArrayList; import java.util.Scanner; public class p1_208 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] points = new int[n]; for (int i = 0; i < n; i++) points[i] = in.nextInt(); ArrayList<interval> list = new ArrayList<interval>(); for (int i = 0; i < n - 1; i++) { interval i1 = new interval(points[i], points[i + 1]); list.add(i1); } boolean flag = false; for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { if (list.get(i).intersect(list.get(j)) || list.get(j).intersect(list.get(i))) { flag = true; break; } } if (flag) break; } if (flag) System.out.println("yes"); else System.out.println("no"); } } class interval { int start; int end; public interval(int s, int e) { this.start = Math.min(s, e); this.end = Math.max(s, e); } public boolean intersect(interval i2) { if (i2.start < this.end && i2.start > this.start && this.end < i2.end && this.end > i2.start) 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
from sys import stdin from fractions import Fraction def read_int(): return [int(x) for x in stdin.readline().split()][0] def read_ints(): return [int(x) for x in stdin.readline().split()] def char_list(): return [c for c in stdin.readline()][:-1] def line(): return stdin.readline() def print_yes(arg): if arg: print "YES" else: print "NO" def list_to_dict(lst): dct = {} for elt in lst: if elt not in dct: dct[elt] = 0 dct[elt] += 1 return dct def intersect(pairs): for pair1 in pairs: for pair2 in pairs: if pair1 == pair2: continue a, b = pair1 c, d = pair2 if a == c: continue if a > c: a, b, c, d = c, d, a, b if b > c and b < d: return True return False _ = read_int() nums = read_ints() pairs = set() for i in range(1, len(nums)): a, b = nums[i - 1], nums[i] a, b = min(a, b), max(a, b) pairs.add((a, b)) if intersect(pairs): 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
I=lambda:map(int, raw_input().split()) F=lambda x, y: (x,y) if x < y else (y, x) n = input() a = I() for i in xrange(n-1): x, y = F(a[i],a[i+1]) for j in xrange(i+1, n-1): u, v = F(a[j], a[j+1]) if not(u>=y or x>=v or x<=u<=v<=y or u<=x<=y<=v): 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 k in range(n-1): #print(k, k+1, 1234567890) for kk in range(k+1, n - 1): #print(kk, kk+1) #print(k, k+1) #print(kk, kk+1) a = min(arr[k], arr[k+1]) b = max(arr[k], arr[k+1]) c = min(arr[kk], arr[kk+1]) d = max(arr[kk], arr[kk+1]) '''print(a, b) print(c, d)''' 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
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class T358A { class MyComp implements Comparator<int[]> { public int compare(int[] o1, int[] o2) { if (o1[0] < o2[0]) { return 0; } else if (o1[0] > o2[0]) { return 1; } else { if (o1[1] < o2[1]) return 0; else return 1; } } } public void solve(int n, int[] x) { if (n == 1) { System.out.println("no"); return; } int[][] ans = new int[n - 1][2]; for (int i = 0; i < n - 1; i ++) { ans[i][0] = x[i] > x[i + 1] ? x[i + 1] : x[i]; ans[i][1] = x[i] > x[i + 1] ? x[i] : x[i + 1]; } Arrays.sort(ans, new MyComp()); boolean f = false; for (int i = 0; i < n - 1 && !f; i ++) { for (int j = i + 1; j < n - 1 && !f; j ++) { if (ans[i][1] > ans[j][0] && ans[i][1] < ans[j][1] && ans[i][0] < ans[j][0] || ans[i][0] > ans[j][0] && ans[i][0] < ans[j][1] && ans[i][1] > ans[j][1]) { f = true; } } } if (f) System.out.println("yes"); else System.out.println("no"); } public static void main(String[] args) { FastScanner in = new FastScanner(); int n = in.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i ++) x[i] = in.nextInt(); new T358A().solve(n, x); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
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.PrintStream; import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: ekrylov * Date: 11/7/13 * Time: 4:25 AM * To change this template use File | Settings | File Templates. */ public class A { public static void main(String[] arg) { new A().solve(); } private void solve() { Scanner in = new Scanner(System.in); PrintStream out = System.out; int n = in.nextInt(); if (n == 1) { out.print("no"); return; } int[] a = new int[1000 + 1]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { int x1 = a[i]; int x2 = a[i + 1]; int y1 = a[j]; int y2 = a[j + 1]; if (x1 < y1 && y1 < x2 && x2 < y2) { out.print("yes"); return; } if (x2 < y1 && y1 < x1 && x1 < y2) { out.print("yes"); return; } if (x1 < y2 && y2 < x2 && x2 < y1) { out.print("yes"); return; } if (x2 < y2 && y2 < x1 && x1 < y1) { out.print("yes"); return; } if (y1 < x1 && x1 < y2 && y2 < x2) { out.print("yes"); return; } if (y2 < x1 && x1 < y1 && y1 < x2) { out.print("yes"); return; } if (y1 < x2 && x2 < y2 && y2 < x1) { out.print("yes"); return; } if (y2 < x2 && x2 < y1 && y1 < x1) { out.print("yes"); return; } } } out.print("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 a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { for (int j = i + 1; j + 1 < n; j++) { int x = min(a[i], a[i - 1]); int y = max(a[i], a[i - 1]); if (a[j] > x && a[j] < y && (a[j + 1] < x || a[j + 1] > y)) { cout << "yes"; return 0; } if (a[j + 1] > x && a[j + 1] < y && (a[j] < x || a[j] > y)) { 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
//package main; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Solve solve = new Solve(new ConsoleIO()); solve.getSolve(); } } interface TypeIO { void solve() throws IOException; } class FileIO implements TypeIO { @Override public void solve() throws IOException { Scanner sc = new Scanner(new File("input.txt")); PrintWriter pw = new PrintWriter(new File("output.txt")); sc.close(); pw.close(); } } class ConsoleIOFast implements TypeIO { BufferedReader in; StringTokenizer tok; PrintWriter out; String readToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readToken()); } String readString() throws IOException { return readToken(); } @Override public void solve() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); out.close(); in.close(); } } class ConsoleIO implements TypeIO { class Segment { int begin; int end; public Segment(int begin, int end) { this.begin = begin; this.end = end; } } @Override public void solve() throws IOException { Scanner sc = new Scanner(System.in); boolean ans = false; int n = sc.nextInt(); int a[] = new int[n]; List<Segment> list = new ArrayList(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n - 1; i++) { Segment segment = new Segment(Math.min(a[i], a[i + 1]), Math.max(a[i], a[i + 1])); list.add(segment); } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.size(); j++) { if (list.get(j).begin > list.get(i).begin && list.get(j).begin < list.get(i).end && list.get(j).end > list.get(i).end) { ans = true; } if (list.get(j).begin < list.get(i).begin && list.get(j).end > list.get(i).begin && list.get(j).end < list.get(i).end) { ans = true; } } } if (ans) { System.out.println("yes"); } else { System.out.println("no"); } sc.close(); } } class Solve { private TypeIO typeIO; public Solve(TypeIO typeIO) { this.typeIO = typeIO; } void getSolve() throws IOException { typeIO.solve(); } }
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.*; public class Main implements Runnable { public void solve() throws IOException { int N = nextInt(); long[] x = new long[N]; for (int i = 0; i < N; i++) { x[i] = nextLong(); } boolean yes = false; for (int i = 0; i < N - 1; i++) { long x1 = Math.min(x[i], x[i + 1]); long x2 = Math.max(x[i], x[i + 1]); for (int j = 0; j < i; j++) { long x3 = Math.min(x[j], x[j + 1]); long x4 = Math.max(x[j], x[j + 1]); if (x3 < x1 && x1 < x4 && x4 < x2) { yes = true; } if (x1 < x3 && x3 < x2 && x2 < x4) { yes = true; } } } if (yes) { System.out.println("yes"); } else { System.out.println("no"); } } void print(Object... obj) { for (Object o : obj) { System.out.println(o); } } void print(int[] a) { System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } void print(int[][] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } void print(boolean[][] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } //----------------------------------------------------------- public static void main(String[] args) { new Main().run(); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tok = null; solve(); in.close(); } catch (IOException e) { System.exit(0); } } public String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BufferedReader in; StringTokenizer tok; }
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 sys import math def intersect(xa1, xa2, xb1, xb2): vmin = min(xa2, xb2) vmax = max(xa1, xb1) if((vmin == xa2 and vmax == xa1) or (vmin == xb2 and vmax == xb1)): return False else: return vmax < vmin n = int(input()) xn = [int(x) for x in (sys.stdin.readline()).split()] x1x2 = [] lastx = xn[0] for i in xn[1:]: if(i > lastx): x1x2.append((lastx, i)) else: x1x2.append((i, lastx)) lastx = i for i in range(len(x1x2) - 1): for j in range(i + 1, len(x1x2)): if(intersect(x1x2[i][0], x1x2[i][1], x1x2[j][0], x1x2[j][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
def does_intersect(p1, p2): return (not(p1[1] <= p2[0] or p1[0] >= p2[1])) and ((p1[0] > p2[0] and p1[1] > p2[1]) or (p1[0] < p2[0] and p1[1] < p2[1])) n = int(raw_input()) ls = map(int, raw_input().split()) pairs = [(min(ls[i], ls[i+1]), max(ls[i], ls[i+1])) for i in range(n-1)] intersects = False for i, p1 in enumerate(pairs): for p2 in pairs[:i]: if does_intersect(p1, p2): # print p1, p2 intersects = True print ('yes' if intersects 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
#include <bits/stdc++.h> using namespace std; class Semicircle { public: int startPoint; int endPoint; Semicircle(int _startPoint, int _endPoint) { startPoint = _startPoint; endPoint = _endPoint; } }; int N; int X[1001]; vector<Semicircle> semicircles; int main() { ios::sync_with_stdio(false); cin >> N; for (int i = 0; i < N; i++) cin >> X[i]; for (int i = 0; i < N - 1; i++) { int startPoint = X[i]; int endPoint = X[i + 1]; Semicircle newSemicircle(startPoint, endPoint); semicircles.push_back(newSemicircle); } for (int i = 0; i < semicircles.size(); i++) { Semicircle first = semicircles[i]; for (int j = 0; j < semicircles.size(); j++) { Semicircle second = semicircles[j]; if ((first.startPoint < second.startPoint && first.endPoint > second.startPoint && first.endPoint < second.endPoint) || (first.startPoint < second.endPoint && first.endPoint > second.endPoint && first.endPoint < second.startPoint) || (first.endPoint < second.startPoint && first.startPoint > second.startPoint && first.startPoint < second.endPoint) || (first.endPoint < second.endPoint && first.startPoint > second.endPoint && first.startPoint < second.startPoint)) { 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
n = int(input()) v = list(map(int, input().split())) intersection_happened = False for i in range(0, n-1): for j in range(i+2, n-1): start_1 = min(v[i], v[i+1]) end_1 = max(v[i], v[i+1]) start_2 = min(v[j], v[j+1]) end_2 = max(v[j], v[j+1]) if start_2 < end_1 and end_2 > start_1: if start_1 < start_2 and end_1 < end_2: intersection_happened = True elif start_1 > start_2 and end_1 > end_2: intersection_happened = True if intersection_happened and bool: 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.util.Scanner; public class Prob72 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int [][] array = new int [n][2]; int start = s.nextInt(); int x; int end; int index = -1; for (int i = 0; i < n-1; i++) { x = s.nextInt(); if (x >= start){ end = x; } else { end = start; start = x; } for (int j = index; j >= 0; j--) { if ((start > array[j][0] && start < array [j][1] && end > array[j][1]) || (start<array[j][0] && end > array[j][0] && end < array[j][1])){ System.out.println("yes"); System.exit(0); } } array[i][0] = start; array[i][1] = end; start = x; index++; } s.close(); 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(input()) a=list(map(int, input().split())) def fn(n,a): if n<3:return 'no' for i in range(2,n-1): for j in range(i-1): p1=(a[j]-a[i])*(a[j]-a[i+1]) p2=(a[j+1]-a[i])*(a[j+1]-a[i+1]) if p1*p2<0:return 'yes' return 'no' print(fn(n,a))
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()) x = [int(y) for y in raw_input().split()] def main(): for i in range(len(x)-1): for j in range(len(x)-1): if i == j : continue else: a1 = min(x[i],x[i+1]) a2 = max(x[i],x[i+1]) b1 = min(x[j],x[j+1]) b2 = max(x[j],x[j+1]) if a1 < b1 and b1 < a2 and a2 < b2 : print "yes" return if b1 < a1 and a1 < b2 and b2 < a2 : print "yes" return print "no" return main()
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 = [int(i) for i in input().split()] res = "no" semi_circles = [] for i in range(1, len(A)): if A[i-1] < A[i]: semi_circles.append([A[i-1], A[i]]) else: semi_circles.append([A[i], A[i-1]]) res = 'no' for i in range(len(semi_circles)): for j in range(i+1, len(semi_circles)): x1, x2 = semi_circles[i] x3, x4 = semi_circles[j] if x1<x3<x2<x4 or x3<x1<x4<x2: res = "yes" break if res=='yes': break print(res)
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[1005]; struct node { int l, r; } s[1005]; int ind = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (n <= 2) puts("no"); else { int flag = 0; int ff = 1; int l = a[0], r = a[1]; if (l > r) { ff = 2; } s[ind].l = min(l, r); s[ind++].r = max(l, r); for (int i = 2; i < n && flag == 0; i++) { if (a[i] < r) ff = 2; else ff = 1; l = r, r = a[i]; for (int j = 0; j < ind; j++) { if (ff == 1) { if (r > s[j].r && l < s[j].r && l > s[j].l) flag = 1; else if (l < s[j].l && r > s[j].l && r < s[j].r) flag = 1; } else { if (l > s[j].r && r < s[j].r && r > s[j].l) flag = 1; else if (r < s[j].l && l > s[j].l && l < s[j].r) flag = 1; } if (flag) break; } s[ind].l = min(l, r); s[ind++].r = max(l, r); } if (flag) 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
#include <bits/stdc++.h> using namespace std; int main() { int n, check = 0, c = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { check = 0, c = 0; for (int j = i + 2; j < n; j++) { if (a[j] > a[i] && a[j] > a[i + 1] || a[j] < a[i] && a[j] < a[i + 1]) check++; else c++; } if (check && c) { 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; public class A358 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int []values = new int[n]; for (int i = 0; i < n; i++) { values[i] = scan.nextInt(); } boolean flag = false; for (int i = 0; i < n; i++) { if(i>1) { for(int j=0; j<i; j++) { if(Math.min(values[j],values[j+1])<Math.min(values[i],values[i-1])&&Math.max(values[j],values[j+1])>Math.min(values[i],values[i-1])&&Math.max(values[j],values[j+1])<Math.max(values[i],values[i-1])) flag=true; if(Math.min(values[i],values[i-1])<Math.min(values[j],values[j+1])&&Math.max(values[i],values[i-1])>Math.min(values[j],values[j+1])&&Math.max(values[i],values[i-1])<Math.max(values[j],values[j+1])) flag=true; } } } String answer = flag ? "yes" : "no"; System.out.println(answer); } }
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 a[3001], n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } if (n <= 2) { puts("no"); return 0; } for (int i = 0; i < n - 1; ++i) { for (int 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]))) { puts("yes"); return 0; } } } 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=int(input()) inp=list(map(int,input().split())) lst=[] ans="no" for i in range(n-1): lst.append([min(inp[i],inp[i+1]),max(inp[i],inp[i+1])]) for i in lst: for j in lst: if i[0]<j[0]<i[1]<j[1] or j[0]<i[0]<j[1]<i[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
import java.io.*; import java.util.*; public class A { public static void solution(BufferedReader reader, PrintWriter out) throws IOException { In in = new In(reader); int n = in.nextInt(); int pre = in.nextInt(); ArrayList<int[]> segs = new ArrayList<int[]>(); for (int i = 1; i < n; i++) { int cur = in.nextInt(); int[] s = new int[] { Math.min(pre, cur), Math.max(pre, cur) }; for (int[] seg : segs) if (s[1] <= seg[0] || s[0] >= seg[1] || (s[0] >= seg[0] && s[1] <= seg[1]) || (s[0] <= seg[0] && s[1] >= seg[1])) continue; else { out.println("yes"); return; } segs.add(s); pre = cur; } out.println("no"); } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(System.out))); solution(reader, out); out.close(); } protected static class In { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public In(BufferedReader reader) { this.reader = reader; } public String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(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> int a[1005], n, x, y, z, t, k; using namespace std; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n - 2; i++) { for (int j = i + 1; j <= n - 1; j++) { if (a[i] <= a[i + 1]) { x = a[i]; y = a[i + 1]; } else { x = a[i + 1]; y = a[i]; } if (a[j] <= a[j + 1]) { z = a[j]; t = a[j + 1]; } else { z = a[j + 1]; t = a[j]; } if (x < z && z < y) { if (t > y) { cout << "yes"; return 0; } } if (z < x) { if (x < t && t < y) { 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; import java.io.File; public class A { private Scanner sc; int n; int[] arr = new int [1001]; static int tmp = 0; public static void main(String[] args) throws Exception { A cl = new A(); cl.input(); cl.solve(); cl.output(); } public void input() throws Exception { sc = new Scanner(System.in); // sc = new Scanner(new File("input.txt")); n = sc.nextInt(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } } public void solve() throws Exception { for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { if (i != j && intersect(arr[i - 1], arr[i], arr[j - 1], arr[j])) { System.out.println("yes"); System.exit(0); } } } } public static boolean intersect(int a, int b, int c, int d) { if (a > b) { tmp = a; a = b; b = tmp; } if (c > d) { tmp = c; c = d; d = tmp; } if ((a <= c && b >= d) || (a >= c && b <= d) || (a >= d) || (b <= c)) return false; return true; } public void output() throws Exception { 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() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; if (n == 1) cout << "no" << endl; else if (n > 1) { vector<pair<long long int, long long int>> vp; for (int i = 0; i < n - 1; i++) vp.push_back({min(arr[i], arr[i + 1]), max(arr[i], arr[i + 1])}); bool exist = true; for (int i = 0; i < vp.size() - 1; i++) { for (int j = i + 1; j < vp.size(); j++) { long long int one1 = vp[i].first, two1 = vp[i].second; long long int one2 = vp[j].first, two2 = vp[j].second; if (one1 < one2 && one2 < two1 && two1 < two2) exist = false; if (one2 < one1 && one1 < two2 && two2 < two1) exist = false; } } cout << (!exist ? "yes" : "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
# -*- coding: utf-8 -*- """ Created on Thu Aug 27 17:23:46 2020 @author: DELL ## # # # # # # ## # --- --- # # 0 0 # # --- --- # # * . # # ______ # # # ## # # # # # # ## """ n=int(input()) x=list(map(int,input().split())) m=[] t=False for i in range(n-1): m+=[[x[i],x[i+1]]] for i in m: for j in m: i.sort() j.sort() if (i[0]<j[0]<i[1]<j[1]) or (j[0]<i[0]<j[1]<i[1]): t=True break if t: 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
""" // Author : snape_here - Susanta Mukherjee """ from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) def gcd(x, y): while y: x, y = y, x % y return x def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') import math mod=100000007 def main(): n=ii() x=li() for i in range(n-1): a=x[i] b=x[i+1] l=[a,b] l.sort() a=l[0] b=l[1] for j in range(i): c=x[j] d=x[j+1] s=[c,d] s.sort() c=s[0] d=s[1] if a<c<b<d or c<a<d<b: print("yes") exit() print("no") # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #Comment read()
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::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n], b[n]; int c[n + 1]; for (int i = 1; i <= n; i++) { cin >> c[i]; } for (int i = 1; i < n; i++) a[i] = c[i]; for (int i = 1; i < n; i++) b[i] = c[i + 1]; for (int i = 2; i < n; i++) { if (a[i] > b[i]) { for (int j = 1; j < i; j++) { if ((a[j] > b[i] && a[j] < a[i]) && ((b[j] > a[i] && b[j] > b[i]) || (b[j] < b[i] && b[j] < a[i]))) { cout << "yes\n"; return 0; } else if ((b[j] > b[i] && b[j] < a[i]) && ((a[j] > a[i] && a[j] > b[i]) || (a[j] < b[i] && a[j] < a[i]))) { cout << "yes\n"; return 0; } } } else { if (a[i] < b[i]) { for (int j = 1; j < i; j++) { if ((a[j] > a[i] && a[j] < b[i]) && ((b[j] > a[i] && b[j] > b[i]) || (b[j] < b[i] && b[j] < a[i]))) { cout << "yes\n"; return 0; } else if ((b[j] > a[i] && b[j] < b[i]) && ((a[j] > a[i] && a[j] > b[i]) || (a[j] < b[i] && a[j] < a[i]))) { cout << "yes\n"; return 0; } } } } } 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; public class C358 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } int x1,x2,x3,x4; for (int i = 0; i < n-1; i++) { x2 = Math.max(a[i],a[i+1]); x1 = Math.min(a[i],a[i+1]); for (int j = 0; j < n-1; j++) { x4 = Math.max(a[j],a[j+1]); x3 = Math.min(a[j],a[j+1]); if ((x1<x3&&x3<x2&&x2<x4)||(x3<x1&&x1<x4&&x4<x2)){ System.out.print("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; const double PI = acos(-1.0); const double EPS = 1e-9; const int INF = 0x5ffffff; const int N = 100 * 1000 + 10; int n; int a[N]; bool okay(int x, int y) { int la = a[x - 1], ra = a[x]; if (la > ra) { swap(la, ra); } int lb = a[y - 1], rb = a[y]; if (lb > rb) { swap(lb, rb); } return (la < lb && ra > lb && ra < rb) || (lb < la && rb > la && rb < ra); } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } bool yes = false; for (int i = 1; i < n && !yes; i++) { for (int j = i + 1; j < n && !yes; j++) { if (okay(i, j)) { yes = true; break; } } } yes ? cout << "yes" << endl : 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() { long long n, x[1000 + 10]; cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (max(x[i], x[i + 1]) < max(x[j], x[j + 1]) && min(x[i], x[i + 1]) < min(x[j], x[j + 1]) && max(x[i], x[i + 1]) > min(x[j], x[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
import java.util.*; public class semicircle { public static boolean dir(int x,int y){ if(x<y)return true; else return false; } public static boolean change(int []arr,int[]brr,int x,int n){ boolean f=true; for(int i=1;i<brr.length-1;i++){ if(arr[brr[i+1]-n]==0){ f=false; //System.out.println(i); break; } else{ if(dir(brr[i-1],brr[i])==dir(brr[i],brr[i+1])){ if(brr[i]>brr[i+1]){ for(int j=brr[i]-n;j<brr[i-1]-n;j++){ arr[j]=0; } } else{ for(int j=brr[i-1]-n;j<brr[i]-n;j++){ arr[j]=0; } } } else{ if(dir(brr[i-1],brr[i])==true&&dir(brr[i],brr[i+1])==false){ if(brr[i-1]>brr[i+1]){ for(int j=brr[i-1]-n;j<=brr[i]-n;j++){ arr[j]=0; } } else{ for(int j=brr[i]-n+1;j<arr.length;j++){ arr[j]=0; } for(int j=0;j<brr[i-1]-n;j++){ arr[j]=0; } } } if(dir(brr[i-1],brr[i])==false&&dir(brr[i],brr[i+1])==true){ if(brr[i-1]>brr[i+1]){ for(int j=0;j<brr[i]-n;j++){ arr[j]=0; } } else{ for(int j=brr[i]-n;j<=brr[i-1]-n;j++){ arr[j]=0; } } } } } }return f; } public static void main(String []args){ Scanner in = new Scanner(System.in); int a= in.nextInt(); int b[] = new int[a]; int c[]; int max,min; max=min=b[0]=in.nextInt(); for(int i =1;i<a;i++){ b[i]= in.nextInt(); if(max<b[i])max=b[i]; if(min>b[i])min=b[i]; } //System.out.println(max); //System.out.println(min); c=new int[max-min+1]; for(int i=0;i<c.length;i++){ c[i]=1; } if(change(c,b,max,min))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
#include <bits/stdc++.h> using namespace std; int main() { int n, x[1000], f = 1; int mx, mn; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x[i]); if (i >= 2 && f) { for (int j = 1; j < i; j++) { if (x[j] > x[j - 1]) { mx = j; mn = j - 1; } else { mx = j - 1; mn = j; } if ((x[i] > x[mn] && x[i] < x[mx]) && (x[i - 1] < x[mn] || x[i - 1] > x[mx])) { f = 0; } else if ((x[i - 1] > x[mn] && x[i - 1] < x[mx]) && (x[i] < x[mn] || x[i] > x[mx])) { f = 0; } } } } if (f == 1) { printf("no"); } else { printf("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 long long N = 2e6 + 5; const double E = 1e-9; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a[n], maxx = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { int minn1 = min(a[i], a[i - 1]); int maxx1 = max(a[i], a[i - 1]); int minn2 = min(a[j], a[j - 1]); int maxx2 = max(a[j], a[j - 1]); if (minn1 < minn2 && minn2 < maxx1 && maxx1 < maxx2 || minn2 < minn1 && minn1 < maxx2 && maxx2 < maxx1) { 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; int n, a[1002]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i < n; i++) for (int j = 1; j < n; j++) if (i != j) { int l = min(a[i], a[i + 1]), r = max(a[i], a[i + 1]), x = min(a[j], a[j + 1]), y = max(a[j], a[j + 1]); if (l < x && x < r && r < y) cout << "yes", exit(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; int n, x, i; pair<int, int> p[1001]; int main() { cin >> n; cin >> x; p[i].first = x; for (; i < n - 2; i++) { cin >> x; p[i].second = x; p[i + 1].first = x; } cin >> x; p[i].second = x; for (int j = 0; j < n; j++) { if (p[j].first > p[j].second) swap(p[j].first, p[j].second); for (int k = j; k < n; k++) { if (p[k].first > p[k].second) swap(p[k].first, p[k].second); if (((p[k].first > p[j].first && p[k].first < p[j].second) && (p[k].second < p[j].first || p[k].second > p[j].second)) || ((p[j].first > p[k].first && p[j].first < p[k].second) && (p[j].second < p[k].first || p[j].second > p[k].second))) { cout << "yes\n"; 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 num, flag = 0; cin >> num; int array[num]; for (int i = 0; i < num; i++) { cin >> array[i]; } for (int i = 1; i < num; i++) { int x, y; if (array[i] > array[i - 1]) { x = array[i - 1], y = array[i]; } else { x = array[i], y = array[i - 1]; } for (int j = 1; j < num; j++) { int a, b; if (array[j] < array[j - 1]) { a = array[j], b = array[j - 1]; } else { a = array[j - 1], b = array[j]; } if (a > x and a < y and b > y) { cout << "yes"; flag = 1; break; } else if (x > a and b > x and y > b) { cout << "yes"; flag = 1; break; } } if (flag == 1) { break; } } if (flag == 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 n; int x[1005]; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n - 1; ++i) { int i1 = (i + 1) % n; int x1 = min(x[i], x[i1]), x2 = max(x[i], x[i1]); for (int j = 0; j < n - 1; ++j) { if (i == j) continue; int y1 = min(x[j], x[(j + 1) % n]), y2 = max(x[j], x[(j + 1) % n]); if (x1 >= y1 && x2 <= y2 || x1 <= y1 && x2 >= y2) continue; if (min(x2, y2) > max(x1, y1)) { cout << "yes\n"; return 0; } } } 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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x[n]; for (int& e : x) cin >> e; bool intersect = false; for (int i = 0; i < n - 1; i++) { int a = x[i], b = x[i + 1]; if (a > b) swap(a, b); for (int j = 0; j < n - 1; j++) { int c = x[j], d = x[j + 1]; if (c > d) swap(c, d); if (a < c and c < b and b < d) intersect = true; } } cout << (intersect ? "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
n=int(input()) l=list(map(int,input().split())) f=0 for i in range(n-1): for j in range(i+2,n-1): x1,x2=(l[i],l[i+1]) if(l[i]<l[i+1]) else (l[i+1],l[i]) x3,x4=(l[j],l[j+1]) if(l[j]<l[j+1]) else (l[j+1],l[j]) if((x1<x3<x2<x4) or (x3<x1<x4<x2)): f=1 break if(f==1): break print("yes") if(f==1) 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 main(): n=int(raw_input()) x=map(int,raw_input().split()) p=[[0,0]]*(n-1) for i in range(n-1): p[i] = [min(x[i],x[i+1]),max(x[i],x[i+1])] p.sort() for i in range(n-2): for j in range(i+1,n-1): if (p[i][0]<p[j][0] and p[j][0]<p[i][1]) and p[i][1]<p[j][1]: print 'yes' return print 'no' main()
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> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; int main() { int n, i, x = 0, y = 0, j; int a[1000]; scanf("%d", &n); for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) { x = y = 0; for (j = i + 2; j < n; j++) { if ((a[j] > a[i] && a[j] > a[i + 1]) || (a[j] < a[i] && a[j] < a[i + 1])) x++; else y++; } if (x && y) { 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 sys #sys.stdin = open('in') n = int(raw_input()) nums = map(int,raw_input().split()) ok = True lst = [] for i in range(n - 1): lst.append((min(nums[i], nums[i + 1]), max(nums[i], nums[i + 1]))) for i in range(n - 1): if not ok: break for j in range(i + 1, n - 1): if lst[i][0] < lst[j][0]: if lst[i][1] > lst[j][0] and lst[i][1] < lst[j][1]: ok = False break if lst[j][0] < lst[i][0]: if lst[j][1] > lst[i][0] and lst[j][1] < lst[i][1]: ok = False break print 'no' if ok else 'yes'
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
def f(): n = int(input()) - 1 x = list(map(int, input().split())) p = [(x[i], x[i + 1]) if x[i] < x[i + 1] else (x[i + 1], x[i]) for i in range(n)] p.sort() for j, (x, y) in enumerate(p, 1): while j < n and p[j][0] == x: j += 1 while j < n and p[j][0] < y: if p[j][1] > y: return 'yes' j += 1 return 'no' print(f())
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())) for i in range(n-1): for j in range(n-1): x,y=(sorted([a[i],a[i+1]])) m,t=sorted([a[j],a[j+1]]) if x<m<y<t or m<x<t<y: 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
#include <bits/stdc++.h> using namespace std; int n; int x[1005]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { int a = x[i]; int b = x[i + 1]; int c = x[j]; int d = x[j + 1]; if (a > b) swap(a, b); if (c > d) swap(c, d); if (a < c && c < b && b < d) { 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 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] palabras= in.readLine().split(" "); long[] datos =new long[n]; long ver=1; for (int i =0;i<n;i++){ datos[i]=Long.parseLong(palabras[i]); } if (n<=3){ System.out.println("no"); } else{ for(int i=3;i<n;i++){ long nuevo=datos[i]; long exnuevo=datos[i-1]; for (int j=0;j<i-2;j++){ long izq=Math.min(datos[j], datos[j+1]); long der=Math.max(datos[j], datos[j+1]); if (nuevo>der && exnuevo>der){ ver=ver*1; } else if(nuevo<izq && exnuevo<izq){ ver=ver*1; } else if((nuevo>der && exnuevo<izq)||(nuevo<izq && exnuevo>der)){ ver=ver*1; } else if(nuevo<der && nuevo>izq && exnuevo<der && exnuevo>izq){ ver=ver*1; } else{ 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
/** * Created with IntelliJ IDEA. * User: flevix * Date: 25.10.13 * Time: 19:26 */ import java.io.*; import java.util.*; import static java.lang.Math.min; import static java.lang.Math.max; public class A { FastScanner in; PrintWriter out; boolean checkIntersect(int x1, int x2, int y1, int y2) { int xmin = min(x1, x2); int xmax = max(x1, x2); int ymin = min(y1, y2); int ymax = max(y1, y2); if (xmin < ymin && ymin < xmax && xmax < ymax) return true; if (ymin < xmin && xmin < ymax && ymax < xmax) return true; return false; } public void solve() throws IOException { int n = in.nextInt(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); } if (n < 3) { out.print("no"); return; } boolean intersect = false; l1: for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { intersect = checkIntersect(p[i], p[i + 1], p[j], p[j + 1]); if (intersect) break l1; } } out.print(intersect ? "yes" : "no"); } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new A().run(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { 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()); } } }
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()) v = list(map(int, input().split())) intersection_happened = False for i in range(0, n-1): for j in range(i+2, n-1): start_1 = min(v[i], v[i+1]) end_1 = max(v[i], v[i+1]) start_2 = min(v[j], v[j+1]) end_2 = max(v[j], v[j+1]) if start_1 < start_2 < end_1 < end_2: intersection_happened = True elif start_2 < start_1 < end_2 < end_1: intersection_happened = True if intersection_happened: 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
n=int(input()) k=list(map(int,input().split())) new=[k[i:i+2]for i in range(n-1)] ans='no' for a,b in new: for c,d in new: if a<c<b<d or a<d<b<c or b<d<a<c or d<a<c<b: 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
/* * * Date: 07 September 2019 * Time: 01:03:00 */ import java.io.*; import java.util.*; public class dc { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } if(n<=3){ System.out.println("no"); return; } for(int i=0;i<n-1;i++){ int p=Math.min(a[i], a[i+1]); int q=Math.max(a[i],a[i+1]); for(int j=0;j<n-1;j++){ if((a[j]>p && a[j]<q && (a[j+1]<p || a[j+1]>q)) || ((a[j]<p || a[j]>q) && a[j+1]>p && a[j+1]<q)) { 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> int main() { int j, n, x[2000], i, p = 0, q = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &x[i]); } for (i = 1; i < n; i++) { p = q = 0; for (j = i + 2; j <= n; j++) { if ((x[j] > x[i] && x[j] > x[i + 1]) || (x[j] < x[i + 1] && x[j] < x[i])) p++; else q++; } if (p != 0 && q != 0) break; } if (p != 0 && q != 0) { printf("yes\n"); } else printf("no\n"); return 0; }
CPP