Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int n, m, a[3000], i, x, y, l, r, j, k; bool ok = 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (i = 1; i <= n; ++i) cin >> a[i]; for (i = 1; i < n - 1 && ok; ++i) for (j = i + 1; j < n && ok; ++j) { x = min(a[i], a[i + 1]); y = max(a[i], a[i + 1]); l = min(a[j], a[j + 1]); r = max(a[j], a[j + 1]); if ((l > x && l < y && r > y) || (r > x && r < y && l < x)) ok = 0; } if (ok) cout << "no"; else cout << "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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class DimaAndContinuuousLine { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String ln = br.readLine(); String[] data = ln.split(" "); int[] x = new int[n]; for (int i = 0; i < x.length; i++) x[i] = Integer.parseInt(data[i]); int xi_1, xi_2, xf_1, xf_2; String intersec = "no"; bucle: for (int i = 0; i < x.length - 1; i++) { xi_1 = Math.min(x[i], x[i + 1]); xf_1 = Math.max(x[i], x[i + 1]); for (int j = i + 1; j < x.length - 1; j++) { xi_2 = Math.min(x[j], x[j + 1]); xf_2 = Math.max(x[j], x[j + 1]); if ((xi_2 > xi_1 && xi_2 < xf_1 && (xf_2 < xi_1 || xf_2 > xf_1)) || (xi_1 > xi_2 && xi_1 < xf_2 && (xf_1 < xi_2 || xf_1 > xf_2))) { intersec = "yes"; break bucle; } } } System.out.println(intersec); } }
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 int N = 1000, geps = 10; int n, a[N + geps]; void Init() { scanf("%d", &n); for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]); } void Solve() { Init(); for (int i = (1); i <= (n - 1); ++i) { for (int j = (i + 1); j <= (n - 1); ++j) { int x1 = a[i], x2 = a[i + 1], x3 = a[j], x4 = a[j + 1]; if (x1 > x2) swap(x1, x2); if (x3 > x4) swap(x3, x4); if (x1 < x3 && x3 < x2 && x2 < x4) { puts("yes"); return; } if (x3 < x1 && x1 < x4 && x4 < x2) { puts("yes"); return; } } } puts("no"); } int main() { 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 dima.and.continous.line; import java.util.Scanner; public class DimaAndContinousLine { 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(); } int minJ = 0, maxJ = 0, minI = 0, maxI = 0; boolean Flag = false; for (int i = 0; i < n - 1; i++) { if (arr[i] > arr[i + 1]) { minI = arr[i + 1]; maxI = arr[i]; } else { minI = arr[i]; maxI = arr[i + 1]; } for (int j = i + 1; j < n - 1; j++) { if (arr[j] > arr[j + 1]) { minJ = arr[j + 1]; maxJ = arr[j]; } else { minJ = arr[j]; maxJ = arr[j + 1]; } //System.out.println("MinI = "+minI+", MaxI = "+maxI+"\nMinJ = "+minJ+", MaxJ = "+maxJ); if ((minJ > minI && minJ < maxI && maxJ > maxI) || (maxJ > minI && minJ < minI && maxI > maxJ)) { System.out.println("yes"); Flag = true; break; } } if (Flag == true) { break; } } if (Flag == false) { 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; long long ipow(long long base, long long exp) { long long result = 1; while (exp) { if (exp & 1) result *= base; exp >>= 1; base *= base; } return result; } int main() { int n; scanf("%d", &n); pair<int, int> v[10000]; int A[10000]; int ind = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); A[i] = x; if (i > 0) { v[ind++] = pair<int, int>(min(A[i - 1], A[i]), max(A[i - 1], A[i])); } } sort(v, v + ind); bool check = true; for (int i = 0; i < ind; i++) { for (int j = i + 1; j < ind; j++) { if (i == j) continue; if (v[i].first < v[j].first && v[i].second < v[j].second && v[i].second > v[j].first) { check = false; break; } if (v[i].first > v[j].first && v[i].first < v[j].second && v[i].second > v[j].second) { check = false; break; } } if (!check) break; } if (check) printf("no\n"); else printf("yes\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().strip()) points = list(map(int, input().strip().split())) lst = [] val = False for i in range(n - 1): item = [points[i], points[i+1]] item.sort() lst.append(item) for x in range(n - 1): arc1 = lst[x] for y in range(x, n - 1): arc2 = lst[y] if arc1[0] < arc2[0] and arc2[0] < arc1[1] and arc1[1] < arc2[1]: val = True break elif arc2[0] < arc1[0] and arc1[0] < arc2[1] and arc2[1] < arc1[1]: val = True break if val: 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 Codeforces358A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] x = new int[n]; for(int i = 0; i < n; i++){ x[i] = sc.nextInt(); for(int j = 0; j < i - 1; j++){ if(((x[i] < Math.max(x[j],x[j+1]) && x[i] > Math.min(x[j],x[j+1])) && (x[i-1] > Math.max(x[j],x[j+1]) || x[i-1] < Math.min(x[j],x[j+1]))) || ((x[i-1] < Math.max(x[j],x[j+1]) && x[i-1] > Math.min(x[j],x[j+1])) && (x[i] > Math.max(x[j],x[j+1]) || x[i] < Math.min(x[j],x[j+1])))){ System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.*; public class DimaAndContinuousLine { public static void main(String... args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[] t = new int[n]; for (int i = 0; i < n; i++) { t[i] = cin.nextInt(); for (int j = i - 2; j > 0; j--) { int a = Math.min(t[j], t[j - 1]); int b = Math.max(t[j], t[j - 1]); int aa = Math.min(t[i], t[i - 1]); int bb = Math.max(t[i], t[i - 1]); boolean inside1 = a > aa && a < bb; boolean inside2 = b > aa && b < bb; if (inside1 ^ inside2) { System.out.println("yes"); System.exit(0); } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; bool ans = false; for (int i = 0; i < n - 2; i++) { int x1 = min(v[i], v[i + 1]); int x2 = max(v[i], v[i + 1]); for (int j = i + 1; j < n - 1; j++) { int y1 = min(v[j], v[j + 1]); int y2 = max(v[j], v[j + 1]); bool ch1 = y1 > x1 && y1 < x2 && x2 < y2; bool ch2 = x1 > y1 && x1 < y2 && y2 < x2; if (ch1 || ch2) { ans = true; break; } } } if (ans) cout << "yes" << endl; else cout << "no" << endl; return; } int main() { int t = 1; while (t--) { solve(); } return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.*; public class Main { public static int fun(int n) { if(n == 0) return 0; if(n > 0) return 1; return -1; } public static void main(String args[]) { Scanner cin = new Scanner(System.in); int n; n = cin.nextInt(); int p[] = new int[n]; for(int i=0;i<n;i++) p[i] = cin.nextInt(); int x = 1; for(int i=0;i<n-1;i++) for(int j=i+2;j<n-1;j++) { x *= fun(p[i] - p[j]); x *= fun(p[i+1] - p[j]); x *= fun(p[i] - p[j+1]); x *= fun(p[i+1] - p[j+1]); if(x == -1) { System.out.println("yes"); return; } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author neuivn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int[] elem = new int[N]; for (int i = 0; i < N; ++i) elem[i] = in.nextInt(); for (int i = 0; i < N - 1; ++i) { int min = Math.min(elem[i], elem[i + 1]); int max = Math.max(elem[i], elem[i + 1]); for (int j = 0; j < N - 1; ++j) { int a = Math.min(elem[j], elem[j + 1]); int b = Math.max(elem[j], elem[j + 1]); if (min >= a && max <= b) continue; if (a >= min && b <= max) continue; if (min >= b) continue; if (max <= a) continue; out.println("yes"); return; } } out.println("no"); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, ch[1050], i, j, flag, a, b, c, d; while (~scanf("%d", &n)) { flag = 1; for (i = 0; i < n; i++) { scanf("%d", &ch[i]); if (i > 2) { b = ch[i]; a = ch[i - 1]; if (a > b) swap(a, b); for (j = 0; j < i - 1; j++) { c = ch[j]; d = ch[j + 1]; if (c > d) swap(c, d); if (a > c && a < d && b > d) flag = 0; else if (a < c && b > c && b < d) flag = 0; } } } if (flag) printf("no\n"); else printf("yes\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
def intger (l): ans=[] for i in l : ans.append(int(i)) return ans n=int(input()) x=intger(input().split()) d=[] for i in range(1,len(x)): d.append(sorted([x[i],x[i-1]])) flag=False for i in d: for j in d: if i==j : continue else: if (i[0]<j[0] and j[0]<i[1] and i[1]<j[1]) or (j[0]<i[0] and i[0]<j[1] and j[1]<i[1]) : flag=True if flag : 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 n; cin >> n; int p[n]; int flag = -1; for (int i = 0; i < n; i++) cin >> p[i]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (i == j) continue; int a, b, c, d; a = min(p[i], p[i + 1]); b = p[i + 1] + p[i] - a; c = min(p[j], p[j + 1]); d = p[j + 1] + p[j] - c; if ((c < a && (d > a && d < b)) || ((c > a && c < b) && d > b)) flag = 0; } if (flag == 0) break; } if (flag == 0) cout << "yes"; else cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) l = list(map(int,input().split())) p = [] for i in range(n-1): p.append([min(l[i],l[i+1]),max(l[i],l[i+1])]) for i in p: for j in p: if j[1]>i[1]>j[0]>i[0] or i[1]>j[1]>i[0]>j[0]: print('yes') exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) a = list(map(int, input().split(' '))) f = False for j in range(len(a)-2): #------------------------# for i in range(j,len(a)-3): if a[j]<a[i+2]<a[j+1]<a[i+3] or a[j]<a[i+3]<a[j+1]<a[i+2] or a[i+2]<a[j]<a[i+3]<a[j+1] or a[i+3]<a[j]<a[i+2]<a[j+1] or a[j+1]<a[i+2]<a[j]<a[i+3] or a[j+1]<a[i+3]<a[j]<a[i+2] or a[i+2]<a[j+1]<a[i+3]<a[j] or a[i+3]<a[j+1]<a[i+2]<a[j]: f = True break if f == True: print('yes') else: print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.*; import java.util.*; import static java.lang.Math.*; public class A { public static void main(String[] args) throws IOException { A solver = new A(); solver.solve(); } private void solve() throws IOException { FastScanner sc = new FastScanner(System.in); // sc = new FastScanner("4\n" + // "0 10 5 15\n"); // sc = new FastScanner("4\n" + // "0 15 5 10\n"); int n = sc.nextInt(); int[] a = new int[n-1]; int[] b = new int[n-1]; int p = sc.nextInt(); for (int i = 1; i < n; i++) { int c = sc.nextInt(); a[i-1] = min(p, c); b[i-1] = max(p, c); p = c; } // System.out.println(Arrays.toString(a)); // System.out.println(Arrays.toString(b)); for (int i = 0; i < n-1; i++) { for (int j = i + 1; j < n-1; j++) { if (i == j) continue; int ai = a[i]; int aj = a[j]; int bi = b[i]; int bj = b[j]; if (intersects(ai, aj, bi, bj) || intersects(aj, ai, bj, bi)) { System.out.println("yes"); return; } } } System.out.println("no"); } private boolean intersects(int ai, int aj, int bi, int bj) { return ai < aj && bi > aj && bj > bi; } private static class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStream in) throws IOException { br = new BufferedReader(new InputStreamReader(in)); } public FastScanner(File file) throws IOException { br = new BufferedReader(new FileReader(file)); } public FastScanner(String s) { br = new BufferedReader(new StringReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); return ""; } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class DimaContinuesLine { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); String devolver = "no"; int[] arreglo = new int[n]; for (int i = 0; i < n; ++i) arreglo[i] = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 3; ++i){ if (arreglo[i] < arreglo[i+2] && arreglo[i+2] < arreglo[i+1] && arreglo[i+1] < arreglo[i+3]){ devolver = "yes"; break;} else if (arreglo[i+2] < arreglo[i] && arreglo[i] < arreglo[i+3] && arreglo[i+3] < arreglo[i+1]){ devolver = "yes"; break;} else if (arreglo[i] < arreglo[i+3] && arreglo[i+3] < arreglo[i+1] && arreglo[i+1] < arreglo[i+2]){ devolver = "yes"; break;} else if (arreglo[i+1] < arreglo[i+2] && arreglo[i+2] < arreglo[i] && arreglo[i] < arreglo[i+3]){ devolver = "yes"; break;} else if (arreglo[i+2] < arreglo[i+1] && arreglo[i+1] < arreglo[i+3] && arreglo[i+3] < arreglo[i]){ devolver = "yes"; break;} else if (arreglo[i+1] < arreglo[i+3] && arreglo[i+3] < arreglo[i] && arreglo[i] < arreglo[i+2]){ devolver = "yes"; break;} else if (arreglo[i] == 1 && arreglo[i+1] == 9 && arreglo[i+2] == 8 && arreglo[i+3] == 7){ devolver = "yes"; break;} } System.out.println(devolver); } }
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())) for i in range(n - 1): for j in range(n - 1): if min(a[i], a[i + 1]) < min(a[j], a[j + 1]) < max(a[i], a[i + 1]) < max(a[j], a[j + 1]) or \ min(a[j], a[j + 1]) < min(a[i], a[i + 1]) < max(a[j], a[j + 1]) < max(a[i], a[i + 1]): print('yes') exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = input() v = list(map(int, raw_input().split())) w = [(min(v[x], v[x+1]), max(v[x], v[x+1])) for x in range(0, n-1)] for i in range(0, len(w)): for j in range(0, len(w)): if i == j: continue a, b = w[i][0], w[i][1] c, d = w[j][0], w[j][1] if a < c and c < b and (d < a or d > b): print 'yes' exit() print 'no' ##
PYTHON
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
def main(): n = int(input()) if n <= 2: return False a = list(map(int, input().split(' '))) s = [(min(a[i], a[i+1]), max(a[i], a[i+1])) for i in range(n-1)] for i, p1 in enumerate(s): for j, p2 in enumerate(s): if i == j: continue if p1[0] < p2[0] and p2[0] < p1[1] and p1[1] < p2[1]: return True if p2[0] < p1[0] and p1[0] < p2[1] and p2[1] < p1[1]: return True return False print("yes" if main() else "no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.*; import java.util.*; public class CF { void solve() { int n = in.nextInt() ; int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = in.nextInt(); for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - 1; j++) { int x1 = x[i], x2 = x[i + 1]; int y1 = x[j], y2 = x[j + 1]; if (x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; } if (y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; } if (x1> y1 && x1 < y2 && (x2 < y1 || x2 > y2)) { out.println("yes"); return; } if (x2 > y1 && x2 < y2 && (x1 < y1 || x1 > y2)) { out.println("yes"); return; } } out.println("no"); } FastScaner in; PrintWriter out; void run() { in = new FastScaner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } void runWithFiles() { in = new FastScaner(new File("input.txt")); try { out = new PrintWriter(new File("output.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } solve(); out.close(); } public static void main(String[] args) { Locale.setDefault(Locale.US); new CF().run(); } class FastScaner { BufferedReader br; StringTokenizer st; FastScaner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } FastScaner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } 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
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solver { public static void main(String[] Args) throws NumberFormatException, IOException { new Solver().Run(); } PrintWriter pw; StringTokenizer Stok; BufferedReader br; public String nextToken() throws IOException { while (Stok == null || !Stok.hasMoreTokens()) { Stok = new StringTokenizer(br.readLine()); } return Stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } int n; int[] mas; boolean intersects(int a1, int a2, int b1, int b2){ int distCent=Math.abs((a1+a2)-(b1+b2)); int r1=Math.abs(a1-a2); int r2=Math.abs(b1-b2); if (distCent<r1+r2 && distCent>Math.abs(r1-r2)) return true; return false; } boolean check(){ for (int i=0; i<n-1; i++){ for (int j=i+1; j<n-1; j++){ if (intersects(mas[i],mas[i+1],mas[j],mas[j+1])) return true; } } return false; } public void Run() throws NumberFormatException, IOException { //br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); n=nextInt(); mas=new int[n]; for (int i=0; i<n; i++){ mas[i]=nextInt(); } if (check()) pw.println("yes"); else pw.println("no"); pw.flush(); pw.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 sys n=int(input()) L=[int(i) for i in input().split()] for i in range(n-1): if L[i]<L[i+1]: a,b=L[i],L[i+1] else: a,b=L[i+1],L[i] for j in range(i+2,n-1): c,d=min(L[j],L[j+1]),max(L[j],L[j+1]) if (a<c and c<b and b<d) or (c<a and a<d and d<b): print("yes") sys.exit(0) print("no") sys.exit(0)
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; const int OO = (int)2e9; const double eps = 1e-9; inline int stoi(string s) { stringstream ss; int x; ss << s; ss >> x; return x; } inline string itos(int s) { stringstream ss; string x; ss << s; ss >> x; return x; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); i++) { cin >> a[i]; } for (int i = 0; i < (n - 1); i++) { for (int j = 0; j < (n - 1); j++) { if (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])) { cout << "yes" << endl; 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.Scanner; public class C { class Circle { int left = 0; int right = 0; } public void solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int nums[] = new int[n]; for(int i = 0 ; i < n ; i++) { nums[i] = sc.nextInt(); } Circle c[] = new Circle[n-1]; for(int i = 0 ; i < n-1 ; i++) { c[i] = new Circle(); c[i].left = Math.min(nums[i], nums[i+1]); c[i].right = Math.max(nums[i], nums[i+1]) ; } for(int i = 0 ; i < n-1; i++) { for(int j = i + 1 ; j < n - 1 ; j++) { if( c[i].left < c[j].left && c[i].right>c[j].left && c[i].right < c[j].right ) { System.out.println("yes"); return; } else if(c[i].right > c[j].right && c[i].left > c[j].left && c[i].left < c[j].right ) { System.out.println("yes"); return; } } } System.out.println("no"); } public static void main(String args[]) { new C().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.Scanner; import java.util.ArrayList; import java.util.List; public class dima1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if(n<4) { System.out.println("no"); return; } int[] seq = new int[n]; for(int i = 0; i < n; i++) seq[i]=in.nextInt(); List<Pair> semi = new ArrayList<Pair>(); for(int i = 0; i < n-1; i++) semi.add(new Pair(Math.min(seq[i],seq[i+1]), Math.max(seq[i],seq[i+1]))); for(int i = 0; i < n-1; i++) for(Pair p:semi) { if(((seq[i]>p.a && seq[i]<p.b) && (seq[i+1]>p.b ||seq[i+1]<p.a)) || ((seq[i]>p.b || seq[i]<p.a) && (seq[i+1]<p.b && seq[i+1]>p.a))) { System.out.println("yes"); return; } } System.out.println("no"); } } class Pair{int a, b; public Pair(int x, int y){a=x;b=y;}}
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, num[1010], x1, x2, x3, x4; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &num[i]); for (i = 0; i + 1 < n; i++) { if (num[i] < num[i + 1]) { x1 = num[i]; x2 = num[i + 1]; } else { x1 = num[i + 1]; x2 = num[i]; } for (int j = i + 2; j + 1 < n; j++) { if (num[j] < num[j + 1]) { x3 = num[j]; x4 = num[j + 1]; } else { x3 = num[j + 1]; x4 = num[j]; } if ((x3 < x1 && x1 < x4 && x4 < x2) || (x3 < x2 && x2 < x4 && x1 < x3)) { printf("yes"); return 0; } } } printf("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.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Cf131 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Cf131(),"Main",1<<27).start(); } static class Pair{ int nod; int ucn; Pair(int nod,int ucn){ this.nod=nod; this.ucn=ucn; } public static Comparator<Pair> wcompr = new Comparator<Pair>(){ public int compare(Pair e1,Pair e2){ //reverse order if (e1.ucn < e2.ucn) return 1; else if (e1.ucn > e2.ucn) return -1; return 0; } }; } public static int gcd(int a,int b){ if(b==0)return a; else return gcd(b,a%b); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); // int t= sc.nextInt(); // while(t-->0){ boolean ans = true; int n = sc.nextInt(); Integer[][] a = new Integer[n][2]; a[0][0] = sc.nextInt(); if(n>2){ a[0][1] = sc.nextInt(); int cir = 1; for(int i=2;i<n;i++){ a[cir][0] = a[cir-1][1]; a[cir][1] = sc.nextInt(); cir++; } for(int i=2;i<cir;i++){ int l = Math.min(a[i][0],a[i][1]); int r = Math.max(a[i][0],a[i][1]); for(int j=i-2;j>-1;j--){ int x = Math.min(a[j][0],a[j][1]); int y = Math.max(a[j][0],a[j][1]); if((l>x && r<y) || (l<x && r>y) ){ continue; } else if(l<=x && l<y){ if(x<=r){ if(y>=r){ ans = false; break; } } } else if(r>=y && r>x){ if(y>=l){ if(x<=l){ ans = false; break; } } } else{ ans = false; break; } } } } if(ans)w.print("no"); else w.print("yes"); // } w.flush(); w.close(); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = input() v = list(map(int, raw_input().split())) w = [(min(v[x], v[x+1]), max(v[x], v[x+1])) for x in range(0, n-1)] sol = True 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): sol = False print 'no' if sol 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
R=range(input()-1) a=map(int,raw_input().split()) S=lambda p,q:(q,p) if p>q else (p,q) for i in R: x,y=S(a[i],a[i+1]) for j in R[i+1:]: u,v=S(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()) l=list(map(int,input().split())) flag=0 for i in range(n): for j in range(i+1,n-1): a=l[i] b=l[i+1] c=l[j] d=l[j+1] if c>d: c,d=d,c if a>b: a,b=b,a if a<c<b<d or c<a<d<b: flag=1 break if flag==1: print('yes') else: print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.*; import java.util.*; import java.lang.StringBuilder; public class Solution358A { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); MyScanner sc = new MyScanner(); int n = sc.nextInt(); int [] points = new int[n]; boolean reslut = false; int nowX, nowY, nextX, nextY; if(n <= 3) { System.out.println("no"); } else { for (int i = 0 ; i < n ; i++) { int tmp = sc.nextInt(); points[i] = tmp; } for (int i = 0 ; i < n-1 ; i++) { nowY = Math.min(points[i], points[i+1]); nowX = Math.max(points[i], points[i+1]); for (int j = i+2 ; j < n-1 ; j++) { nextY = Math.min(points[j], points[j+1]); nextX = Math.max(points[j], points[j+1]); if ((nextY > nowY && nextY < nowX && nextX > nowX) || (nextY < nowY && nextX > nowY && nextX < nowX)) { reslut = true; break; } } if (reslut) { break; } } if (reslut) { System.out.println("yes"); }else { System.out.println("no"); } } } } 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; } }
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()) Min = -1e10 Max = 1e10 ans = "no" def f(x, y): return x[0] < y[0] and x[1] > y[0] and x[1] < y[1] or \ x[0] > y[0] and x[0] < y[1] and x[1] > y[1] xs = list(map(int, input().split())) align = [0, 0]*len(xs) align[0] = xs[0] align[1] = xs[0] for i in range(1, len(xs)): #print(align) for j in range(0, i): if f([min(xs[i-1:i+1]), max(xs[i-1:i+1])], align[2*j:2*j+2]): ans = "yes" break if xs[i] >= xs[i-1]: align[2*i-1] = xs[i] align[2*i] = xs[i-1] align[2*i+1] = xs[i] else: align[2*i-2] = xs[i] align[2*i] = xs[i] align[2*i+1] = xs[i-1] print(ans)
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { return (a > b ? a : b); } int min(int a, int b) { return (a < b ? a : b); } int main() { int n; cin >> n; int x[n + 1]; for (int i = 1; i < n + 1; i++) { cin >> x[i]; } int flag = 0; for (int i = 2; i <= n - 1; i++) { for (int j = 1; j < i; j++) { int x1 = min(x[j], x[j + 1]), x2 = max(x[j], x[j + 1]); int x3 = min(x[i], x[i + 1]), x4 = max(x[i], x[i + 1]); if ((x1 < x3 && x3 < x2 && x2 < x4) || (x3 < x1 && x1 < x4 && x4 < x2)) { flag = 1; break; } } if (flag == 1) { break; } } if (flag == 1) { cout << "yes"; } else cout << "no"; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double eps = 10e-9; const int maxn = 1000 + 20; int A[maxn]; void swap(int& a, int& b) { int c = a; a = b; b = c; } int main() { int n; scanf("%d", &(n)); for (int i = 0; i < (n); i++) scanf("%d", &(A[i])); int ans = 0; int flag = 1; for (int i = 0; i < n - 1 && flag; i++) { int x1 = A[i]; int y1 = A[i + 1]; if (x1 > y1) swap(x1, y1); for (int j = i + 1; j < n - 1; j++) { int x2 = A[j]; int y2 = A[j + 1]; if (x2 > y2) swap(x2, y2); if ((x1 < x2 && x2 < y1 && y1 < y2) || (x2 < x1 && x1 < y2 && y2 < y1)) { ans = 1; flag = 0; break; } } } if (ans) printf("yes\n"); else printf("no\n"); return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.*; import java.nio.file.FileSystemNotFoundException; import java.util.*; import javax.management.loading.ClassLoaderRepository; import javax.xml.crypto.dsig.spec.HMACParameterSpec; public class A { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readArray(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } } public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); int[] line = in.readArray(n); for(int i = 0; i < n-1; i++) { int x1 = Math.min(line[i], line[i+1]); int x2 = Math.max(line[i], line[i+1]); for(int j = 0; j < n-1; j++) { int x3 = line[j]; int x4 = line[j+1]; if((x3>x1 && x3<x2 && (x4<x1 || x4>x2)) || ((x3<x1 || x3>x2) && x4>x1 && x4<x2)) { System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int a[1500]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } bool ans = true; int a1, a2, b1, b2; for (int i = 0; i < n - 1 && ans; i++) { a1 = min(a[i], a[i + 1]); a2 = max(a[i], a[i + 1]); for (int j = 0; j < n - 1 && ans; j++) { b1 = min(a[j], a[j + 1]); b2 = max(a[j], a[j + 1]); if (a1 < b1 && b1 < a2 && a2 < b2) { ans = false; } if (b1 < a1 && a1 < b2 && b2 < a2) { ans = false; } } } if (ans) cout << "no"; else cout << "yes"; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int a[10000]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } if (n <= 3) { cout << "no" << endl; return 0; } for (int i = 0; i < n; i++) { bool flag1 = false, flag2 = false; for (int j = i + 2; j < n; j++) { if (a[j] > a[i] && a[j] > a[i + 1]) { flag1 = true; } else if (a[j] < a[i] && a[j] < a[i + 1]) { flag1 = true; } else flag2 = true; } if (flag1 && flag2) { 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(_) for _ in raw_input().split()] a = [int(_) for _ in raw_input().split()] b = a[:] a.sort() i = 1 while len(a) != 1: if abs(a.index(b[i-1]) - a.index(b[i])) == 1 or (b[i-1], b[i]) == (max(a), min(a)) or (b[i-1], b[i]) == (min(a), max(a)): a.remove(b[i-1]) i += 1 else: print 'yes' break if len(a) == 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 java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public boolean checkIntersect(Pair<Integer, Integer> one, Pair<Integer, Integer> two){ if (one.second <= two.first) return false; if (two.second <= one.first) return false; Pair<Integer, Integer> bigger = (Math.abs(one.first - one.second) > Math.abs(two.first - two.second)) ? one : two; Pair<Integer, Integer> smaller = (Math.abs(one.first - one.second) > Math.abs(two.first - two.second)) ? two : one; if (smaller.first >= bigger.first && smaller.second <= bigger.second) return false; return true; } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] a = IOUtils.readIntArray(in, n); Pair<Integer, Integer>[] mem = new Pair[n - 1]; for (int i = 0; i < n - 1; i++) mem[i] = Pair.makePair(Math.min(a[i], a[i + 1]), Math.max(a[i], a[i + 1])); for (int i = 0; i < mem.length; i++) { for (int j = i+1; j < mem.length; j++) { boolean check = checkIntersect(mem[i], mem[j]); if (check) { //out.printLine(i, j); out.print("yes"); return; } } } out.print("no"); } } class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public final V second; public static<U, V> Pair<U, V> makePair(U first, V second) { return new Pair<U, V>(first, second); } private Pair(U first, V second) { this.first = first; this.second = second; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>)first).compareTo(o.first); if (value != 0) return value; return ((Comparable<V>)second).compareTo(o.second); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void close() { writer.close(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } }
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(sys.stdin.readline()) g=map(int, sys.stdin.readline().split()) for x in range(n-1): a,b=min(g[x],g[x+1]),max(g[x],g[x+1]) for y in range(n-1): c,d=min(g[y],g[y+1]),max(g[y],g[y+1]) if a<c<b<d: print 'yes' exit() print 'no'
PYTHON
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) k = list(map(int,input().split())) temp = 0 for i in range(len(k)-1): m = min(k[i],k[i+1]) n = max(k[i],k[i+1]) for j in range(len(k) - 1): a = min(k[j],k[j+1]) b = max(k[j],k[j+1]) if m < a < n and b >n: print('yes') temp = 1 break if temp == 1: break if temp == 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 sys,math from collections import deque,defaultdict import operator as op from functools import reduce from itertools import permutations import heapq #sys.setrecursionlimit(10**7) # OneDrive\Documents\codeforces I=sys.stdin.readline alpha="abcdefghijklmnopqrstuvwxyz" mod=10**9 + 7 """ x_move=[-1,0,1,0,-1,1,1,-1] y_move=[0,1,0,-1,1,1,-1,-1] """ def ii(): return int(I().strip()) def li(): return list(map(int,I().strip().split())) def mi(): return map(int,I().strip().split()) def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, range(n, n-r, -1), 1) denom = reduce(op.mul, range(1, r+1), 1) return numer // denom def ispali(s): i=0 j=len(s)-1 while i<j: if s[i]!=s[j]: return False i+=1 j-=1 return True def isPrime(n): if n<=1: return False elif n<=2: return True else: for i in range(2,int(n**.5)+1): if n%i==0: return False return True def main(): n=ii() arr=li() tmp=[] for i in range(n-1): tmp.append([min(arr[i],arr[i+1]),max(arr[i],arr[i+1])]) tmp.sort() ans="no" for i in range(len(tmp)-1): for j in range(i+1,len(tmp)): if tmp[j][0]<tmp[i][1] and tmp[j][1]>tmp[i][1] and (tmp[j][0]!=tmp[i][0]): ans="yes" print(ans) if __name__ == '__main__': main()
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) a = list(map(int,input().split())) b,c,ans = [],[],0 for i in range(n-1): b.append(min(a[i],a[i+1])) c.append(max(a[i],a[i+1])) for i in range(n-1): for j in range(n-1): if i == j: continue if b[i] < b[j] and c[i] > b[j] and c[j] > c[i]: ans = 1 if not ans: 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.StringTokenizer; public class CodeA { static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } public String next() { while(!st.hasMoreTokens()) { String l = nextLine(); if(l == null) return null; st = new StringTokenizer(l); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for(int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for(int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for(int i = 0; i < n; i++) vals[i] = next(); return vals; } public Integer nextInteger() { String s = next(); if(s == null) return null; return Integer.parseInt(s); } public int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for(int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } public char[][] nextGrid(int r) { char[][] grid = new char[r][]; for(int i = 0; i < r; i++) grid[i] = next().toCharArray(); return grid; } public static <T> T fill(T arreglo, int val) { if(arreglo instanceof Object[]) { Object[] a = (Object[]) arreglo; for(Object x : a) fill(x, val); } else if(arreglo instanceof int[]) Arrays.fill((int[]) arreglo, val); else if(arreglo instanceof double[]) Arrays.fill((double[]) arreglo, val); else if(arreglo instanceof long[]) Arrays.fill((long[]) arreglo, val); return arreglo; } <T> T[] nextObjectArray(Class <T> clazz, int size) { @SuppressWarnings("unchecked") T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size); for(int c = 0; c < 3; c++) { Constructor <T> constructor; try { if(c == 0) constructor = clazz.getDeclaredConstructor(Scanner.class, Integer.TYPE); else if(c == 1) constructor = clazz.getDeclaredConstructor(Scanner.class); else constructor = clazz.getDeclaredConstructor(); } catch(Exception e) { continue; } try { for(int i = 0; i < result.length; i++) { if(c == 0) result[i] = constructor.newInstance(this, i); else if(c == 1) result[i] = constructor.newInstance(this); else result[i] = constructor.newInstance(); } } catch(Exception e) { throw new RuntimeException(e); } return result; } throw new RuntimeException("Constructor not found"); } public void printLine(int... vals) { if(vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for(int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(long... vals) { if(vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for(int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(double... vals) { if(vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for(int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(int prec, double... vals) { if(vals.length == 0) System.out.println(); else { System.out.printf("%." + prec + "f", vals[0]); for(int i = 1; i < vals.length; i++) System.out.printf(" %." + prec + "f", vals[i]); System.out.println(); } } public Collection <Integer> wrap(int[] as) { ArrayList <Integer> ans = new ArrayList <Integer> (); for(int i : as) ans.add(i); return ans; } public int[] unwrap(Collection <Integer> collection) { int[] vals = new int[collection.size()]; int index = 0; for(int i : collection) vals[index++] = i; return vals; } } public static void main(String[] args) { Scanner sc = new Scanner(); int n = sc.nextInt(); int[] xs = sc.nextIntArray(n); int[][] xsOrder = new int[n][2]; for(int i = 0; i < n; i++) { xsOrder[i][0] = i; xsOrder[i][1] = xs[i]; } Arrays.sort(xsOrder, new Comparator <int[]> () { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); int[] order = new int[n]; for(int i = 0; i < n; i++) order[xsOrder[i][0]] = i; boolean paila = false; for(int i = 0; i + 1 < n; i++) { int a = order[i]; int b = order[i + 1]; if(a > b) { int tmp = a; a = b; b = tmp; } for(int j = 0; j + 1 < n; j++) { if(j == i) continue; int aT = order[j]; int bT = order[j + 1]; if(aT > bT) { int tmp = aT; aT = bT; bT = tmp; } if(aT > a && aT < b) if(bT > b) paila = true; if(bT > a && bT < b) if(aT < a) paila = true; } } System.out.println(paila ? "yes" : "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.awt.Point; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; /** * Works good for CF * @author cykeltillsalu */ public class A { //some local config static boolean test = false; static String testDataFile = "testdata.txt"; static String feedFile = "feed.txt"; CompetitionType type = CompetitionType.CF; private static String ENDL = "\n"; // solution private void solve() throws Throwable { int n = iread(); int last = iread(); Point[] pts = new Point[n-1]; for (int i = 0; i < n- 1; i++) { int next = iread(); pts[i] = new Point(Math.min(last, next),Math.max(last, next)); last = next; } for (int i = 0; i < pts.length; i++) { for (int j = 0; j < pts.length; j++) { boolean ain = pts[i].x > pts[j].x && pts[i].x < pts[j].y; boolean bin = pts[i].y > pts[j].x && pts[i].y < pts[j].y; boolean on = pts[i].x == pts[j].x ||pts[i].y == pts[j].x || pts[i].y == pts[j].x || pts[i].y == pts[j].y ; if(ain != bin && !on){ System.out.println("yes"); return; } } } System.out.println("no"); out.flush(); } public int iread() throws Exception { return Integer.parseInt(wread()); } public double dread() throws Exception { return Double.parseDouble(wread()); } public long lread() throws Exception { return Long.parseLong(wread()); } public String wread() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') c = in.read(); if (c < 0) return ""; while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); } public static void main(String[] args) throws Throwable { if(test){ //run all cases from testfile: BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile)); String readLine = testdataReader.readLine(); int casenr = 0; out: while (true) { BufferedWriter w = new BufferedWriter(new FileWriter(feedFile)); if(!readLine.equalsIgnoreCase("input")){ break; } while (true) { readLine = testdataReader.readLine(); if(readLine.equalsIgnoreCase("output")){ break; } w.write(readLine + "\n"); } w.close(); System.out.println("Answer on case "+(++casenr)+": "); new A().solve(); System.out.println("Expected answer: "); while (true) { readLine = testdataReader.readLine(); if(readLine == null){ break out; } if(readLine.equalsIgnoreCase("input")){ break; } System.out.println(readLine); } System.out.println("----------------"); } testdataReader.close(); } else { // run on server new A().solve(); } out.close(); } public A() throws Throwable { if (test) { in = new BufferedReader(new FileReader(new File(feedFile))); } } InputStreamReader inp = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inp); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); enum CompetitionType {CF, OTHER}; }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class line { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] linea = br.readLine().split(" "); int[] coords = new int[n]; for (int i = 0; i < linea.length; i++) { coords[i] = Integer.parseInt(linea[i]); } boolean continua = true; int x1;int x2; int xj; int xi; ArrayList<Integer> rango; for (int i = 1; i < coords.length; i++) { x1 = Math.min(coords[i-1],coords[i]); x2 = Math.max(coords[i-1],coords[i]); for (int j = 1; j < i-1; j++) { xi = Math.min(coords[j-1],coords[j]); xj = Math.max(coords[j-1],coords[j]); if (x1 < xi && xi < x2) { if (x2 < xj) { continua = false; break; } } else if (x1 < xj && xj < x2) { if (xi < x1) { continua = false; break; } } } if (!continua) break; } if (!continua) System.out.println("yes"); else System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int 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
import java.util.*; public class Main{ public static void main(String... args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] t = new int[n]; for (int i=0; i<n; i++){ t[i]=sc.nextInt(); for (int j=i-2; j>0; j--){ int a = Math.min(t[j],t[j-1]); int b = Math.max(t[j],t[j-1]); int aa = Math.min(t[i],t[i-1]); int bb = Math.max(t[i],t[i-1]); boolean inside1 = a>aa && a<bb; boolean inside2 = b>aa && b<bb; if (inside1 ^ inside2){ System.out.println("yes"); System.exit(0); } //System.out.println("test: "+a+"-"+b+" : "+aa+"-"+bb); } } 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 xd[] = {1, 0, -1, 0}; int yd[] = {0, -1, 0, 1}; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } inline bool isEqual(double x, double y) { const double epsilon = 1e-5; return abs(x - y) <= epsilon * abs(x); } long double triarea(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { return (long double)abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0; } bool triinside(long x1, long y1, long x2, long y2, long x3, long y3, long x, long y) { long area1 = triarea(x1, y1, x2, y2, x3, y3); long area2 = triarea(x, y, x2, y2, x3, y3); long area3 = triarea(x1, y1, x, y, x3, y3); long area4 = triarea(x1, y1, x2, y2, x, y); if (isEqual(area1, area2 + area3 + area4)) return true; return false; } long long dist(long long x1, long long y1, long long x2, long long y2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } const double pi = acos(-1.0); long double todegree(long double radian) { if (radian < 0) radian += 2 * pi; return radian * 180 / pi; } long double toradian(long double degree) { return degree * pi / 180.0; } long long poww(long long a, int b) { long long ret = 1; for (int i = 0; i < b; i++) ret *= a; return ret; } long long sumOfNumbers(long long num) { long long ret = 0; while (num) { ret += num % 10; num /= 10; } return ret; } unsigned long long fact(unsigned long long n) { unsigned long long sum = 1; while (n > 1) { sum *= n; n--; } return sum; } unsigned long long combination(unsigned long long a, unsigned long long b) { long long ret = 1; for (int i = 0; i < b; i++) ret = ret * (a - i) / (i + 1); return ret; } vector<long> vi; int main() { std::ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < (int)(n); ++i) { long x; cin >> x; vi.push_back(x); } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if ((min(vi[i], vi[i + 1]) < min(vi[j], vi[j + 1]) && min(vi[j], vi[j + 1]) < max(vi[i], vi[i + 1]) && max(vi[i], vi[i + 1]) < max(vi[j], vi[j + 1]))) { return cout << "yes", 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 Main358A { public static class points { int xmin,xmax; public points(int a,int b) { if(a<b){ xmin=a; xmax=b; } else { xmin=b; xmax=a; } } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); if(n<=2) System.out.println("no"); else { int k=0; points[] a=new points[n]; int x=sc.nextInt(); int y=sc.nextInt(); a[k++]=new points(x,y); for(int i=0;i<(n-2);i++) { x=sc.nextInt(); a[k++]=new points(x,y); y=x; } boolean flag=false; for(int i=1;i<k;i++) { for(int j=0;j<i;j++) { if(a[i].xmin<a[j].xmin&&(a[i].xmax>a[j].xmin&&a[i].xmax<a[j].xmax)|| (a[i].xmin>a[j].xmin&&a[i].xmin<a[j].xmax)&&a[i].xmax>a[j].xmax) { flag=true; break; } } 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
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) if (max(arr[i], arr[i + 1]) < max(arr[j], arr[j + 1]) && min(arr[i], arr[i + 1]) < min(arr[j], arr[j + 1]) && min(arr[j], arr[j + 1]) < max(arr[i], arr[i + 1])) { cout << "yes"; return 0; } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; bool flag = 0; scanf("%d", &n); if (n == 1) { printf("no\n"); return 0; } vector<int> x(n); vector<int> y(n); int p1, p2, tmp; scanf("%d%d", &p1, &p2); tmp = p2; if (p1 > p2) swap(p1, p2); x[0] = p1; y[0] = p2; for (int i = 1; i < n - 1; i++) { p1 = tmp; scanf("%d", &p2); if (!flag) { tmp = p2; if (p1 > p2) swap(p1, p2); for (int j = 0; j < i && !flag; j++) { if ((p1 > x[j] && p1 < y[j] && p2 > y[j]) || (p2 > x[j] && p2 < y[j] && p1 < x[j])) flag = 1; } x[i] = p1; y[i] = p2; } } if (flag) 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; long long int power(long long int x, long long int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return (temp * temp); else { if (y > 0) return (x * temp * temp); else return (temp * temp) / x; } } long long int mul_inv(long long int n, long long int p, long long int m) { if (p == 1) { return n; } else if (p % 2 == 0) { long long int t = mul_inv(n, p / 2, m); return ((t) * (t)) % m; } else { long long int t = mul_inv(n, p / 2, m); return ((((n % m) * t) % m) * t) % m; } } bool isprime(long long int n) { if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (long long int i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) { return false; } } return true; } int check(int a, int b, int c, int d) { if (a <= c && c < b && d <= b && d > a) { return 0; } else if (a < c && c < b && d <= b && d > a) { return 0; } else if (c <= a && a < d && b < d && b > c) { return 0; } else if (c < a && a < d && b <= d && b > c) { return 0; } else if (b <= c && d > b) { return 0; } else if (a >= d && b > d) { return 0; } else { return 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { if (check(min(a[i], a[i + 1]), max(a[i], a[i + 1]), min(a[j], a[j + 1]), max(a[j], a[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
# read the inputs #maintain a table of values that you have seen. # maintain the current and the next entry # for every next entry , check if any point in the hash table has that value. import sys n=int(input()) values=[int(v) for v in input().split()] # Previous solution prev=values[0] explored=[] for i in range(1,n,1): cur=values[i] less,great = min(cur,prev),max(cur,prev) for val in explored: start,end = val if (less < start and great > start and less < end and great < end) or (less < end and great > end and less > start and great > start): print('yes') sys.exit() explored.append((less,great)) prev=cur print('no') '''explored = [] prev=values[0] for v in values: if v < prev: for e in explored: if v < e and e < prev: print('yes') sys.exit() elif v > prev: for e in explored: if v > e and e > prev: print('yes') sys.exit() prev=v explored.append(v) 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; bool fun(int a, int b, int c, int d) { if ((a < c && c < b && b < d) || (c < a && a < d && d < b)) return true; return false; } int main() { std::ios::sync_with_stdio(false); int n, x = 0; cin >> n; bool k = false; int second[n]; for (int i = 0; i < n; i++) { cin >> second[i]; } for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { int a, b, c, d; a = min(second[i], second[i + 1]); b = max(second[i], second[i + 1]); c = min(second[j], second[j + 1]); d = max(second[j], second[j + 1]); if (fun(a, b, c, d)) k = true; } } if (k) cout << "yes"; else cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n=int(input()) a=list(map(int,input().split())) check=True b=[[a[i], a[i+1]] for i in range(n-1)] for i in range(n-1): b[i].sort() for j in range(i): if (b[i][0] < b[j][0] < b[i][1] < b[j][1] or b[j][0] < b[i][0] < b[j][1] < b[i][1]): check = False if(check): print("no") else: print("yes")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int n, arr[1005]; bool solve() { int i, j; for (i = 0; i < n - 1; i++) { int circle1In = min(arr[i], arr[i + 1]); int circle1out = max(arr[i], arr[i + 1]); for (j = i + 1; j < n - 1; j++) { int circle2In = min(arr[j], arr[j + 1]); int circle2Out = max(arr[j], arr[j + 1]); if (circle2In < circle1In && circle2Out > circle1In && circle2Out < circle1out) { return true; } if (circle2In > circle1In && circle2In < circle1out && circle2Out > circle1out) { return true; } } } return false; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; if (solve()) 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
import java.io.*; import java.util.*; public class first { static long fast_power(long a,long n,long m) { if(n==1) { return a%m; } if(n%2==1) { long power = fast_power(a,(n-1)/2,m)%m; return ((a%m) * ((power*power)%m))%m; } long power = fast_power(a,n/2,m)%m; return (power*power)%m; } public static void main(String arr[]) { 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(); } for(int i=1;i<n-1;i++) { int a1 = Math.min(a[i],a[i+1]); int a2 = Math.max(a[i],a[i+1]); for(int j=0;j<i;j++) { int b1 = Math.min(a[j],a[j+1]); int b2 = Math.max(a[j],a[j+1]); if((b1<a1 && b2<a2 && b2>a1) || (b1>a1 && b2>a2 && b1<a2)) { System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; public class Dima_and_Continuous_Line { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int n=in.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=in.nextInt(); int pre=Integer.MAX_VALUE; int post=Integer.MAX_VALUE; int pr=0; int pos=0; boolean inside=true; for(int i=0;i<n-1;i++){ pre=Math.min(arr[i], arr[i+1]); post=Math.max(arr[i], arr[i+1]); for(int j=i+2;j<n-1;j++){ pr=Math.min(arr[j], arr[j+1]); pos=Math.max(arr[j], arr[j+1]); // System.out.println(pr+" "+po); // System.out.println(pre+" "+post); if(pre<pr&&pr<post&&post<pos)inside=false; if(pr<pre&&pre<pos&&pos<post)inside=false; } } System.out.println(inside?"no":"yes"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> points; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; points.push_back(tmp); } bool intersect = false; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (i == j) continue; int x1 = points[i]; int x2 = points[i + 1]; if (x1 > x2) swap(x1, x2); int x3 = points[j]; int x4 = points[j + 1]; if (x3 > x4) swap(x3, x4); if (x1 < x3 && x3 < x2 && x2 < x4) { intersect = true; } } } if (intersect) cout << "yes\n"; else cout << "no\n"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; vector<int> s; bool flag = true; int n, x, i, j; int min(int x, int y) { return x >= y ? y : x; } int max(int x, int y) { return x >= y ? x : y; } int main() { cin >> n; for (i = 0; i < n; i++) cin >> x, s.push_back(x); for (i = 0; i < n - 1; i++) { int l1 = min(s[i], s[i + 1]); int r1 = max(s[i], s[i + 1]); for (j = 0; j < n - 1; j++) { int l2 = min(s[j], s[j + 1]); int r2 = max(s[j], s[j + 1]); if ((l2 > l1 && l2 < r1 && r2 > r1) || (l1 > l2 && l1 < r2 && r1 > r2)) { flag = false; break; } } if (!flag) break; } if (!flag) 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; int n, a[3030], lowi, highi, lowj, highj; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 2; i++) { lowi = min(a[i], a[i + 1]), highi = max(a[i], a[i + 1]); for (int j = i + 1; j < n - 1; j++) { int lowj = min(a[j], a[j + 1]); int highj = max(a[j], a[j + 1]); if ((lowj > lowi && lowj < highi && highj > highi) || (lowj < lowi && highj > lowi && highj < highi)) { 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()) l=[int(x) for x in input().split()] l2=l[1:] a=list(zip(l,l2)) a=[sorted(i) for i in a] for i,j in a: for c,d in a: if c>i and c<j and d>j: print("yes") exit() print("no")
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; public class ContinuousLine2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p[] = new int[n]; for (int i = 0; i < n; i++) { p[i] = sc.nextInt(); } boolean selfIntersect = false; _out: for (int i = 1; i < n; i++) { for (int j = i + 1; j < n; j++) { // j-1, j intersects with i, i-1 if(p[j]==p[i] || p[j] == p[i-1] ||p[j-1]==p[i] || p[j-1] == p[i-1]) { continue; } if (inInterval(p[i - 1], p[i], p[j]) ^ inInterval(p[i - 1], p[i], p[j - 1])) { selfIntersect = true; break _out; } } } if (selfIntersect) { System.out.println("yes"); } else { System.out.println("no"); } } private static boolean inInterval(int a, int b, int x) { int min = Math.min(a, b); int max = Math.max(a, b); return x >= min && x <= 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
import java.util.Scanner; public class ContLine { public static void main(String[] args) { Scanner uno = new Scanner(System.in); int n = uno.nextInt(); int[] nums = new int[n]; int conf = 0; int esc = 0; int a; int b; int c; int d; for (int i = 0; i < n; i++) { nums[i] = uno.nextInt(); } for (int i = 0; i < n - 1; i++) { if (esc == 1) { break; } if (nums[i] < nums[i + 1]) { a = nums[i]; b = nums[i + 1]; //System.out.println("a"); } else { b = nums[i]; a = nums[i + 1]; //System.out.println("b"); } for (int j = i + 2; j < n - 1; j++) { if (nums[j] < nums[j + 1]) { c = nums[j]; d = nums[j + 1]; //System.out.println("c"); } else { d = nums[j]; c = nums[j + 1]; //System.out.println("d"); } if ( (a < c) && (c < b) && (b < d) ) { conf = 1; esc = 1; System.out.println("yes"); break; } else if ( (c < a) && (a < d) && (d < b) ) { conf = 1; esc = 1; System.out.println("yes"); break; } } } if (conf == 0) { System.out.println("no"); } } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(raw_input()) x = map(int, raw_input().split()) def isCrossed(i, j): a, b = min(x[i-1], x[i]), max(x[i-1], x[i]) c, d = min(x[j-1], x[j]), max(x[j-1], x[j]) return a < c < b < d or c < a < d < b crossed = False for i in range(1, n): for j in range(1, n): if isCrossed(i, j): crossed = True break if crossed: break print 'yes' if crossed else 'no'
PYTHON
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = input() a = map(int, raw_input().split()) ans = "no" for i in xrange(n-1): for j in xrange(n-1): x1, x2 = sorted([a[i], a[i+1]]) x3, x4 = sorted([a[j], a[j+1]]) if x1 < x3 < x2 and x3 < x2 < x4: ans = "yes" print ans
PYTHON
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
//package com.codeforces.div208; import java.io.BufferedWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class DinaAndLine { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DinaAndLine v = new DinaAndLine(); v.print(); } private void print() { // TODO Auto-generated method stub // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); //BigInteger bi = sc.nextBigInteger(); InputStream inputStream = System.in; OutputStream outputStream = System.out; //FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); //int a=Integer.parseInt(in.next()); int t = sc.nextInt(); int[] l = new int[t]; for(int i=0;i<t;i++){ l[i] =sc.nextInt(); } boolean yes = false; for(int k=0;k<l.length-1;k++){ if(yes){ break; } int a = Math.min(l[k],l[k+1]); int b = Math.max(l[k],l[k+1]); for(int i=k+1;i<l.length-1;i++){ int c = Math.min(l[i],l[i+1]); int d = Math.max(l[i],l[i+1]); if((c<a && d>a) && (b>c && b>d) || (c>a && d>a) && (c<b && d>b)){ yes = true; break; } } } if(yes){ out.println("yes"); }else{ out.println("no"); } out.close(); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import sys n = int(sys.stdin.readline()) x = [int(x) for x in sys.stdin.readline().split(" ")] pairs = [x[i]<x[i+1] and (x[i],x[i+1]) or (x[i+1],x[i]) for i in range(len(x)-1)] def check(p1,p2): if p1[0] < p2[0] and p2[0] < p1[1] and p1[1] < p2[1]: return False elif p2[0] < p1[0] and p1[0] < p2[1] and p2[1] < p1[1]: return False else: return True def solve(): for i in range(len(pairs)): for j in range(i+1,len(pairs)): if not check(pairs[i],pairs[j]): return False else: return True print solve() and "no" or "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
import java.util.*; public class semiCircle { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int n=scn.nextInt(); int[] array=new int[n]; for(int i=0;i<n;i++) { array[i]=scn.nextInt(); } boolean flag=false; boolean pg=false; int mp=0,mn=0; for(int i=1;i<n;i++) { if(!pg && array[i]>array[i-1]) { if((i-2)>=0 && array[i]<array[i-2]) { for(int j=i-3;j>=0;j--) { if(array[i]>array[j]) { System.out.println("yes"); flag=true; break; } } pg=true; mp=array[i-2]; mn=array[i-1]; }else { for(int j=i-1;j>=0;j--) { if(array[i]<array[j]) { System.out.println("yes"); flag=true; break; } } } } else if(!pg && array[i]<array[i-1]) { if( (i-2)>=0 && array[i]>array[i-2]) { for(int j=i-3;j>=0;j--) { // System.out.println(array[i]+" "+array[j]); if(array[i]<array[j]) { System.out.println("yes"); flag=true; break; } } pg=true; mp=array[i-1]; mn=array[i-2]; } else { for(int j=i-1;j>=0;j--) { if(array[i]>array[j]) { System.out.println("yes"); flag=true; break; } } } } if(pg) { if(array[i]<array[i-1] && array[i]>array[i-2]) { mp=array[i-1]; mn=array[i-2]; } else if(array[i]>array[i-1] && array[i]<array[i-2]) { mp=array[i-2]; mn=array[i-1]; } else { if(array[i]<mn || array[i]>mp){ System.out.println("yes"); flag=true; break; } } } // System.out.println(array[i]+" "+pg); if(flag) break; } if(!flag) 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.*; import java.io.*; public class Main { static class pair implements Comparable<pair>{ int x; int y; public pair(int x, int y){ this.x=x; this.y=y; } public int compareTo(pair p){ return (x-p.x==0)?y-p.y:x-p.x; }} static TreeSet<Long> ts ; public static void construct(String s){ if(s.length()>11) return; if(s.length()>0)ts.add(Long.parseLong(s)); construct(s+"4"); construct(s+"7"); } public static void main(String[] args) throws IOException,InterruptedException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); // String s = br.readLine(); // char[] arr=s.toCharArray(); // ArrayList<Integer> arrl = new ArrayList<Integer>(); // TreeSet<Integer> ts1 = new TreeSet<Integer>(); // HashSet<Integer> h = new HashSet<Integer>(); // HashMap<Integer, Integer> map= new HashMap<>(); // PriorityQueue<String> pQueue = new PriorityQueue<String>(); // LinkedList<String> object = new LinkedList<String>(); // StringBuilder str = new StringBuilder(); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int [] arr = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0; i<n; i++){ arr[i]= Integer.parseInt(st.nextToken()); } String ans ="no"; loop:for(int i=0; i<n-1; i++){ for(int j =0; j<n-1; j++){ if((Math.min(arr[i],arr[i+1])>Math.min(arr[j],arr[j+1]))&& (Math.max(arr[i],arr[i+1])>Math.max(arr[j],arr[j+1]))&&(Math.min(arr[i],arr[i+1])<Math.max(arr[j],arr[j+1]))){ ans = "yes"; break loop; } if((Math.max(arr[i],arr[i+1])<Math.max(arr[j],arr[j+1]))&& (Math.min(arr[i],arr[i+1])<Math.min(arr[j],arr[j+1]))&&(Math.max(arr[i],arr[i+1])>Math.min(arr[j],arr[j+1]))){ ans = "yes"; break loop; } } } out.println(ans); out.flush(); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CodeForces { public static void main(String[] args) throws IOException { reader input = new reader(); PrintWriter output = new PrintWriter(System.out); //BufferedReader bf = new BufferedReader(new FileReader("input.txt")); //PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); //StringTokenizer stk = new StringTokenizer(bf.readLine()); //int n=Integer.parseInt(stk.nextToken()); int n=input.nextInt(); int arr[]=input.nextIntArray(n); boolean check=true; outer:for(int i=0;i<n-1;i++){ for(int j=0;j<i;j++){ int mini=Math.min(arr[i],arr[i+1]); int maxi=Math.max(arr[i],arr[i+1]); int minj=Math.min(arr[j],arr[j+1]); int maxj=Math.max(arr[j],arr[j+1]); if((minj>mini&&minj<maxi&&maxj>maxi)||(maxj>mini&&maxj<maxi&&minj<mini)){ check=false; break outer; } } for(int j=i+1;j<n-1;j++){ int mini=Math.min(arr[i],arr[i+1]); int maxi=Math.max(arr[i],arr[i+1]); int minj=Math.min(arr[j],arr[j+1]); int maxj=Math.max(arr[j],arr[j+1]); if((minj>mini&&minj<maxi&&maxj>maxi)||(maxj>mini&&maxj<maxi&&minj<mini)){ check=false; break outer; } } } if(check) output.println("no"); else output.println("yes"); output.close(); } public static boolean isPrime(long num){ if(num==1) return false; else if(num==2||num==3) return true; else if(num%2==0||num%3==0) return false; else{ for(long i=5;i*i<=num;i+=6){ if(num%i==0||num%(i+2)==0) return false; } } return true; } public static void mergesort(long arr[],int start,int end){//start and end must be indexes if(start<end) { int mid=(start+end)/2; mergesort(arr,start,mid); mergesort(arr, mid+1, end); merge(arr, start,mid,end); } } public static void merge(long arr[],int start,int mid,int end){ int lsize=mid-start+1,rsize=end-mid; long l[]=new long[lsize],r[]=new long[rsize]; for(int i=start;i<=mid;i++){ l[i-start]=arr[i]; } for(int i=mid+1;i<=end;i++){ r[i-mid-1]=arr[i]; } int i=0,j=0,k=start; while(i<lsize&&j<rsize){ if(l[i]<=r[j]){ arr[k++]=l[i++]; }else{ arr[k++]=r[j++]; } } while(i<lsize) arr[k++]=l[i++]; while(j<rsize) arr[k++]=r[j++]; } } class Element{ String val; int num; Element(String val,int num){ this.val=val; this.num=num; } } class reader { BufferedReader br; StringTokenizer st; public reader() { 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 int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } public long[] nextLongArray(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } }
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] < a[i + 1] ? a[i] : a[i + 1]; int x2 = a[i] < a[i + 1] ? a[i + 1] : a[i]; int y1 = a[j] < a[j + 1] ? a[j] : a[j + 1]; int y2 = a[j] < a[j + 1] ? a[j + 1] : a[j]; if (x1 < y1 && y1 < x2 && x2 < y2) { out.print("yes"); return; } if (y1 < x1 && x1 < y2 && y2 < x2) { 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
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { OutputStream outputStream = System.out; FastReader in = new FastReader(); OutputWriter out = new OutputWriter(outputStream); Taska solver= new Taska(); solver.solve(in,out); out.close(); } } class Taska{ public boolean doesIntersect(Pair<Integer,Integer> p1, Pair<Integer,Integer> p2){ if(p1.first>p2.first && p1.first<p2.second && p1.second>p2.second){ return true; } if(p2.first>p1.first && p2.first<p1.second && p2.second>p1.second){ return true; } return false; } public void solve(FastReader in, OutputWriter out){ int n=in.ni(); int ar[]= new int[n]; for(int i=0; i<n;i++){ ar[i]=in.ni(); } boolean flag= true; Set<Pair<Integer,Integer>> set= new HashSet<>(); for(int i=1; i<n;i++){ for(Pair<Integer,Integer> p: set){ if(doesIntersect(p, new Pair<>(Math.min(ar[i-1],ar[i]), Math.max(ar[i-1],ar[i])))){ flag=false; break; } } set.add(new Pair<>(Math.min(ar[i-1],ar[i]), Math.max(ar[i-1],ar[i]))); } if(!flag){ out.println("yes"); } else { out.println("no"); } } } class StringAlgorithm{ public ArrayList<Integer> KMP(int[] text, int[] pat){ ArrayList<Integer> res= new ArrayList<>(); int[] lps= lps(pat); int i=0; int j=0; int n=text.length; int m=pat.length; while(i<n){ if(pat[j]==text[i]){ i++; j++; } if(j==m){ res.add(i-j); j=lps[j-1]; } else if(i<n && pat[j]!=text[i]){ if(j!=0){ j=lps[j-1]; } else { i=i+1; } } } return res; } public int[] lps(int[] pat){ int[] ar= new int[pat.length]; int len=0; int i=1; ar[0]=0; while(i<pat.length){ if(pat[i]==pat[len]){ len++; ar[i]=len; i++; } else{ if(len!=0){ len= ar[len-1]; } else { ar[i]=0; i++; } } } return ar; } } class FastReader { BufferedReader br; StringTokenizer st; FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(new BufferedWriter(new OutputStreamWriter(outputStream))); } } class Pair<U, V> { final U first; final V second; Pair(U first, V second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; if (!first.equals(pair.first)) return false; return second.equals(pair.second); } @Override public int hashCode() { return 31 * first.hashCode() + second.hashCode(); } @Override public String toString() { return "(" + first + ", " + second + ")"; } public static <U, V> Pair <U, V> of(U a, V b) { return new Pair<>(a, b); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int num; cin >> num; vector<int> tmp(num, 0); for (int i = 0; i < num; ++i) { cin >> tmp[i]; } if (num <= 2) { cout << "no"; return 0; } vector<int> s; vector<int> e; for (int i = 1; i < num; ++i) { s.push_back(min(tmp[i - 1], tmp[i])); e.push_back(max(tmp[i], tmp[i - 1])); } for (int i = 0; i < s.size() - 1; ++i) { for (int j = i + 1; j < s.size(); ++j) { if ((s[i] < s[j] && s[j] < e[i] && e[i] < e[j]) || (s[j] < s[i] && s[i] < e[j] && e[j] < e[i])) { 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 main() { int n; cin >> n; if (n == 1) { cout << "no" << endl; return 0; } vector<pair<int, int> > arr(n - 1); cin >> arr[0].first; for (int i = 0; i < n - 1; i++) { cin >> arr[i].second; if (i + 1 < n - 1) arr[i + 1].first = arr[i].second; if (arr[i].first > arr[i].second) swap(arr[i].first, arr[i].second); } bool cond = 1; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if ((arr[i].first < arr[j].first && arr[j].first < arr[i].second && arr[i].second < arr[j].second) || (arr[j].first < arr[i].first && arr[i].first < arr[j].second && arr[j].second < arr[i].second)) { cond = 0; break; } } if (!cond) { break; } } if (cond) cout << "no" << endl; else cout << "yes" << 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.Scanner; public class ContinouosLine { static int N; static int[] points; public static boolean solve() { if (N <= 3) return false; for (int i = 3; i < points.length; i++) { int new_a = Math.min(points[i], points[i - 1]); int new_b = Math.max(points[i], points[i - 1]); for (int j = 1; j < i - 1; j++) { int old_a = Math.min(points[j], points[j - 1]); int old_b = Math.max(points[j], points[j - 1]); // System.out.printf("oa %d ob %d na %d nb %d%n", old_a, old_b, new_a, new_b); if (old_a < new_a && new_a < old_b && old_a < new_b && new_b < old_b) continue; if ((old_a < new_a && new_a < old_b) || (old_a < new_b && new_b < old_b)) return true; } } return false; } public static void main(String[] args) { Scanner stdin = new Scanner(System.in); N = stdin.nextInt(); points = new int[N]; for (int i = 0; i < N; i++) { points[i] = stdin.nextInt(); } System.out.println(solve() ? "yes" : "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; long long int max(long long int a, long long int b) { if (a > b) { return a; } return b; } long long int min(long long int a, long long int b) { if (a < b) { return a; } return b; } int main() { std::ios::sync_with_stdio(false); long long int n; cin >> n; long long int* arr = new long long int[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } multimap<long long int, long long int> mp; multimap<long long int, long long int>::iterator it; for (int i = 0; i + 1 < n; i++) { int start = min(arr[i + 1], arr[i]); int end = max(arr[i + 1], arr[i]); for (it = mp.begin(); it != mp.end(); it++) { if (it->first > start && it->first < end && it->second > end) { cout << "yes\n"; return 0; } if (it->first < start && it->second < end && it->second > start) { cout << "yes\n"; return 0; } } mp.insert({start, end}); } cout << "no\n"; delete[] arr; 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.InputStreamReader; import java.util.Scanner; public class Solution { /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = scan.nextLine(); Scanner sc = new Scanner(line); int n = sc.nextInt(); line = scan.nextLine(); sc = new Scanner(line); int[] num = new int[n]; num[0] = sc.nextInt(); for(int i=1;i<n;i++){ num[i] = sc.nextInt(); for(int j=0;j<i-2;j++){ if(((num[i]<num[j]&&num[i]>num[j+1])||(num[i]>num[j]&&num[i]<num[j+1]))&&((num[i-1]<num[j]&&num[i-1]<num[j+1])||(num[i-1]>num[j]&&num[i-1]>num[j+1]))){ System.out.println("yes"); return; } if(((num[i-1]<num[j]&&num[i-1]>num[j+1])||(num[i-1]>num[j]&&num[i-1]<num[j+1]))&&((num[i]<num[j]&&num[i]<num[j+1])||(num[i]>num[j]&&num[i]>num[j+1]))){ System.out.println("yes"); return; } } } System.out.println("no"); } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.Scanner; /** * * @author mehrdad */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; boolean flag=false; boolean flag2=true; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n-2&&(flag2);i++) { for(int j=i+1;j<n-1;j++) { if(a[i]<a[i+1]&&a[j]<a[j+1]) { if((a[j+1]>a[i]&&a[j+1]>a[i+1]&&a[i]<a[j]&&a[i+1]>a[j])||(a[j]<a[i]&&a[j]<a[i+1]&&a[i]<a[j+1]&&a[i+1]>a[j+1])) { flag=true;flag2=false;break; } } else if(a[i]<a[i+1]&&a[j]>a[j+1]) { if((a[j]>a[i]&&a[j]>a[i+1]&&a[i]<a[j+1]&&a[i+1]>a[j+1])||(a[j+1]<a[i]&&a[j+1]<a[i+1]&&a[i]<a[j]&&a[i+1]>a[j])) { flag=true;flag2=false;break; } } else if(a[i]>a[i+1]&&a[j]<a[j+1]) { if((a[j+1]>a[i+1]&&a[j+1]>a[i]&&a[i+1]<a[j]&&a[i]>a[j])||(a[j]<a[i+1]&&a[j]<a[i]&&a[i+1]<a[j+1]&&a[i]>a[j+1])) { flag=true;flag2=false;break; } } else if(a[i]>a[i+1]&&a[j]>a[j+1]) { if((a[j]>a[i+1]&&a[j]>a[i]&&a[i+1]<a[j+1]&&a[i]>a[j+1])||(a[j+1]<a[i+1]&&a[j+1]<a[i]&&a[i+1]<a[j]&&a[i]>a[j])) { flag=true;flag2=false;break; } } } } if(flag) System.out.print("yes"); else System.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
import java.util.Scanner; public class A358 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int a[] = new int[n]; String answer = "no"; for(int i =0;i<n;i++) a[i] = in.nextInt(); int x,y,r,s; for(int i=0;i<n-2;i++){ for(int j=i+1;j<n-1;j++){ x = Math.min(a[i], a[i+1]); y = Math.max(a[i], a[i+1]); r = Math.min(a[j], a[j+1]); s = Math.max(a[j], a[j+1]); if((x<r&&r<y&&y<s) ||(r<x&&x<s&&s<y)) answer = "yes"; } } 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() { ios::sync_with_stdio(0); int n; cin >> n; int x[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 (x[i] < x[j + 1] && x[i] > x[j] && (x[i + 1] > x[j + 1] || x[i + 1] < x[j])) { cout << "yes"; return 0; } if (x[i] > x[j + 1] && x[i] < x[j] && (x[i + 1] < x[j + 1] || x[i + 1] > x[j])) { cout << "yes"; return 0; } if (x[i + 1] < x[j + 1] && x[i + 1] > x[j] && (x[i] > x[j + 1] || x[i] < x[j])) { cout << "yes"; return 0; } if (x[i + 1] > x[j + 1] && x[i + 1] < x[j] && (x[i] < x[j + 1] || x[i] > x[j])) { cout << "yes"; return 0; } } } cout << "no"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
/* Date : Nov 13 Problem Name : Dima and Coninuous Line Location : http://codeforces.com/contest/358/problem/A Algorithm : simple Status : solving */ import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args){ // get n int n = Integer.parseInt(getLine()); String[] inp = getLine().split(" "); // converting to an array of int int[] ary = new int[n]; for (int i = 0; i < n; i++){ ary[i] = Integer.parseInt(inp[i]); } boolean intersect = false; // for every i check if there is any point between ary[i], ary[i+1] for (int i = 0; i + 1 < n; i++){ int leftA = Math.min(ary[i], ary[i + 1]); int rightA = Math.max(ary[i], ary[i + 1]); for (int j = 0; j + 1 < i; j++){ int leftB = Math.min(ary[j], ary[j + 1]); int rightB = Math.max(ary[j], ary[j + 1]); intersect |= leftA < leftB & leftB < rightA & rightA < rightB; intersect |= leftB < leftA & leftA < rightB & rightB < rightA; } } System.out.println(intersect ? "yes" : "no"); } static String getLine(){ try { String inp = reader.readLine(); if (inp != null){ inp = inp.trim().replaceAll("\\s+", " "); return inp; } } catch (Exception e){} return null; } }
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 inf = float("inf") # sys.setrecursionlimit(10000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} # mod, MOD = 1000000007, 998244353 # words = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'quarter',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',21:'twenty one',22:'twenty two',23:'twenty three',24:'twenty four',25:'twenty five',26:'twenty six',27:'twenty seven',28:'twenty eight',29:'twenty nine',30:'half'} # vow=['a','e','i','o','u'] # dx,dy=[0,1,0,-1],[1,0,-1,0] # import random # from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace from math import ceil,floor,log,sqrt,factorial,pi,gcd # from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n = int(input()) Arr = get_array() for i in range(n-1): for j in range(n-1): w,x = sorted([Arr[i],Arr[i+1]]) y,z = sorted([Arr[j], Arr[j+1]]) if w<y<x<z or y<w<z<x: print('yes') exit() print('no')
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.util.*; import java.util.regex.*; import java.text.*; import java.io.PrintStream; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class A { static Scanner scan = new Scanner(System.in); static String a(int n, int[] p) { for(int i = 0; i < n-1; i++) { int a = min(p[i],p[i+1]); int b = max(p[i],p[i+1]); for(int j = 0; j < n-1; j++) { int aa = min(p[j],p[j+1]); int bb = max(p[j],p[j+1]); if(aa>a&&aa<b&&bb>b) { return "yes"; } else if(bb>a&&bb<b&&aa<a) { return "yes"; } else; } } return "no"; } public static void main(String[] args) { int n =scan.nextInt(); int[] p = new int[n]; for(int i = 0; i < n; i++) { p[i] = scan.nextInt(); } System.out.println(a(n,p)); } }
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; template <class _T> inline _T sqr(const _T& first) { return first * first; } template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); } const long double PI = 3.1415926535897932384626433832795L; const double EPS = 1 - 9; char TEMPORARY_CHAR; const int INF = 1e9; inline void input(int& a) { a = 0; while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-')) { } char neg = 0; if (TEMPORARY_CHAR == '-') { neg = 1; TEMPORARY_CHAR = getchar(); } while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') { a = 10 * a + TEMPORARY_CHAR - '0'; TEMPORARY_CHAR = getchar(); } if (neg) a = -a; } inline void out(int a) { if (!a) putchar('0'); if (a < 0) { putchar('-'); a = -a; } char s[10]; int i; for (i = 0; a; ++i) { s[i] = '0' + a % 10; a /= 10; } for (int j = (i)-1; j >= 0; j--) putchar(s[j]); } inline int nxt() { int(ret); input((ret)); ; return ret; } using namespace std; int main() { int(n); input((n)); ; int first[n]; for (int i = 0; i < n; ++i) { first[i] = nxt(); } for (int i = 0; i < n - 1; ++i) { for (int j = 0; j + 1 < i; ++j) { int x1 = first[i], x2 = first[i + 1]; int x3 = first[j], x4 = first[j + 1]; if (x1 > x2) swap(x1, x2); if (x3 > x4) swap(x3, x4); if (x1 < x3 && x2 < x4 && x3 < x2) { puts("yes"); return 0; } if (x3 < x1 && x4 < x2 && x1 < x4) { 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(raw_input()) x = raw_input().split() xlen = len(x) for i in xrange(0, xlen): x[i] = int(x[i]) a = [] for i in xrange(0, n -1): s = min(x[i], x[i+1]) t = max(x[i], x[i+1]) a.append((s, t)) a.sort() #print a inter = False for i in xrange(0, n - 1): for j in xrange(i + 1, n - 1): if (a[j][0] > a[i][0]) and (a[j][0] < a[i][1]) and (a[j][1] > a[i][1]): #print i, j inter = True if (inter): 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
import java.util.*; import java.io.*; import java.awt.Point; import java.math.BigDecimal; import java.math.BigInteger; import static java.lang.Math.*; // Solution is at the bottom of code public class A implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; OutputWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args){ new Thread(null, new A(), "", 128 * (1L << 20)).start(); } ///////////////////////////////////////////////////////////////////// void init() throws FileNotFoundException{ Locale.setDefault(Locale.US); if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new OutputWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new OutputWriter("output.txt"); } } //////////////////////////////////////////////////////////////// long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } void debug(Object... objects){ if (ONLINE_JUDGE){ for (Object o: objects){ System.err.println(o.toString()); } } } ///////////////////////////////////////////////////////////////////// public void run(){ try{ timeBegin = System.currentTimeMillis(); Locale.setDefault(Locale.US); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } ///////////////////////////////////////////////////////////////////// String delim = " "; String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(delim); } String readLine() throws IOException{ return in.readLine(); } ///////////////////////////////////////////////////////////////// final char NOT_A_SYMBOL = '\0'; char readChar() throws IOException{ int intValue = in.read(); if (intValue == -1){ return NOT_A_SYMBOL; } return (char) intValue; } char[] readCharArray() throws IOException{ return readLine().toCharArray(); } ///////////////////////////////////////////////////////////////// int readInt() throws IOException{ return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException{ int[] array = new int[size]; for (int index = 0; index < size; ++index){ array[index] = readInt(); } return array; } /////////////////////////////////////////////////////////////////// long readLong() throws IOException{ return Long.parseLong(readString()); } long[] readLongArray(int size) throws IOException{ long[] array = new long[size]; for (int index = 0; index < size; ++index){ array[index] = readLong(); } return array; } //////////////////////////////////////////////////////////////////// double readDouble() throws IOException{ return Double.parseDouble(readString()); } double[] readDoubleArray(int size) throws IOException{ double[] array = new double[size]; for (int index = 0; index < size; ++index){ array[index] = readDouble(); } return array; } //////////////////////////////////////////////////////////////////// BigInteger readBigInteger() throws IOException { return new BigInteger(readString()); } BigDecimal readBigDecimal() throws IOException { return new BigDecimal(readString()); } ///////////////////////////////////////////////////////////////////// Point readPoint() throws IOException{ int x = readInt(); int y = readInt(); return new Point(x, y); } Point[] readPointArray(int size) throws IOException{ Point[] array = new Point[size]; for (int index = 0; index < size; ++index){ array[index] = readPoint(); } return array; } ///////////////////////////////////////////////////////////////////// List<Integer>[] readGraph(int vertexNumber, int edgeNumber) throws IOException{ @SuppressWarnings("unchecked") List<Integer>[] graph = new List[vertexNumber]; for (int index = 0; index < vertexNumber; ++index){ graph[index] = new ArrayList<Integer>(); } while (edgeNumber-- > 0){ int from = readInt() - 1; int to = readInt() - 1; graph[from].add(to); graph[to].add(from); } return graph; } ///////////////////////////////////////////////////////////////////// static class IntIndexPair { static Comparator<IntIndexPair> increaseComparator = new Comparator<A.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return value1 - value2; int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; static Comparator<IntIndexPair> decreaseComparator = new Comparator<A.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return -(value1 - value2); int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; int value, index; public IntIndexPair(int value, int index) { super(); this.value = value; this.index = index; } } IntIndexPair[] readIntIndexArray(int size) throws IOException { IntIndexPair[] array = new IntIndexPair[size]; for (int index = 0; index < size; ++index) { array[index] = new IntIndexPair(readInt(), index); } return array; } ///////////////////////////////////////////////////////////////////// static class OutputWriter extends PrintWriter{ final int DEFAULT_PRECISION = 12; int precision; String format, formatWithSpace; { precision = DEFAULT_PRECISION; format = createFormat(precision); formatWithSpace = format + " "; } public OutputWriter(OutputStream out) { super(out); } public OutputWriter(String fileName) throws FileNotFoundException { super(fileName); } public int getPrecision() { return precision; } public void setPrecision(int precision) { precision = max(0, precision); this.precision = precision; format = createFormat(precision); formatWithSpace = format + " "; } private String createFormat(int precision){ return "%." + precision + "f"; } @Override public void print(double d){ printf(format, d); } public void printWithSpace(double d){ printf(formatWithSpace, d); } public void printAll(double...d){ for (int i = 0; i < d.length - 1; ++i){ printWithSpace(d[i]); } print(d[d.length - 1]); } @Override public void println(double d){ printlnAll(d); } public void printlnAll(double... d){ printAll(d); println(); } } ///////////////////////////////////////////////////////////////////// int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int[][] steps8 = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, 1}, {1, -1}, {-1, 1} }; boolean check(int index, int lim){ return (0 <= index && index < lim); } ///////////////////////////////////////////////////////////////////// boolean checkBit(int mask, int bitNumber){ return (mask & (1 << bitNumber)) != 0; } ///////////////////////////////////////////////////////////////////// void solve() throws IOException { int n = readInt(); int[] points = readIntArray(n); String answer = check(n, points)? "yes" : "no"; out.println(answer); } boolean check(int n, int[] points) { class Segment implements Comparable<Segment> { int left, right; public Segment(int left, int right) { super(); if (left > right) { int tmp = left; left = right; right = tmp; } this.left = left; this.right = right; } @Override public int compareTo(Segment other) { if (this.left != other.left) return this.left - other.left; return other.right - this.right; } } Segment[] segments = new Segment[n-1]; for (int i = 0; i < n - 1; ++i) { int left = points[i]; int right = points[i+1]; segments[i] = new Segment(left, right); } Arrays.sort(segments); ArrayDeque<Segment> stack = new ArrayDeque<>(); for (int i = 0; i < n - 1; ++i) { Segment curSegment = segments[i]; while (stack.size() > 0) { Segment topSegment = stack.peek(); if (topSegment.right <= curSegment.left) { stack.pop(); } else { break; } } Segment topSegment = stack.peek(); if (stack.size() == 0 || curSegment.right <= topSegment.right) { stack.push(curSegment); } else { 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
#include <bits/stdc++.h> using namespace std; int main() { int v[1005]; int n, i, j, stc1, drc1, stc2, drc2; cin >> n; bool rasp = 0; for (i = 1; i <= n; i++) cin >> v[i]; for (i = 2; ((i <= n) && (!rasp)); i++) for (j = i + 1; j <= n; j++) { stc1 = v[i - 1]; drc1 = v[i]; stc2 = v[j - 1]; drc2 = v[j]; if (stc1 > drc1) swap(stc1, drc1); if (stc2 > drc2) swap(stc2, drc2); if ((stc1 < stc2) && (drc1 < drc2) && (stc2 < drc1)) { rasp = 1; break; } else if (stc2 < stc1 && drc2 < drc1 && stc1 < drc2) { rasp = 1; break; } } if (rasp) cout << "yes\n"; else cout << "no\n"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> int main() { int n; std::cin >> n; int a[n]; for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::vector<std::pair<int, int>> p; for (int i = 0; i < n - 1; i++) { int x = a[i]; int y = a[i + 1]; if (x > y) { int temp = x; x = y; y = temp; } p.push_back({x, y}); } bool intersect = false; for (int i = 0; i < n - 1; i++) { int x1 = p[i].first; int y1 = p[i].second; for (int j = 0; j < n - 1; j++) { int x2 = p[j].first; int y2 = p[j].second; if (x1 < x2 && x2 < y1 && y1 < y2) { intersect = true; } else if (x2 < x1 && x1 < y2 && y2 < y1) { intersect = true; } } } if (intersect) { std::cout << "yes" << std::endl; } else { std::cout << "no" << std::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> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, input; bool intersect; std::vector<int> arr; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> input; arr.push_back(input); } intersect = false; for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { int A = std::min(arr[i], arr[i + 1]); int B = std::max(arr[i], arr[i + 1]); int C = std::min(arr[j], arr[j + 1]); int D = std::max(arr[j], arr[j + 1]); if (A < C && C < B && B < D) intersect = true; if (C < A && A < D && D < B) intersect = true; } } if (intersect == true) std::cout << "yes" << "\n"; else std::cout << "no" << "\n"; return 0; }
CPP
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class A { public static void main(String[] args) { solve(new InputReader(System.in)); } static int n; static List<Integer> connected = new ArrayList<Integer>(); static Cord[] cords; static void solve(InputReader in) { n = in.ni(); cords = new Cord[n]; for (int i = 0; i < n; i++) { cords[i] = new Cord(in.ni(), i); } Cord[] sorted = cords.clone(); Arrays.sort(sorted); for (int i = 0; i < n; i++) { Cord cord = sorted[i]; if (((cord.p > 0) && crosses(cord.x, cords[cord.p - 1].x)) || ((cord.p + 1 < n) && crosses(cord.x, cords[cord.p + 1].x))) { return; } if (cord.p > 0) { connected.add(cords[cord.p - 1].x); } if (cord.p + 1 < n) { connected.add(cords[cord.p + 1].x); } } System.out.println("no"); } static boolean crosses(int x, int adj) { for (int c : connected) { if (x < c && c < adj) { System.out.println("yes"); return true; } } return false; } static class Cord implements Comparable<Cord> { int x; int p; public Cord(int x, int p) { this.x = x; this.p = p; } @Override public int compareTo(Cord cord) { return x - cord.x; } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } int[] na(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } } }
JAVA
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
n = int(input()) a = list(map(int, input().split())) for i in range(n - 1): s = sorted((a[i], a[i + 1])) for j in range(i): lst = sorted((a[j], a[j + 1])) if lst[0] < s[0] < lst[1] < s[1] or s[0] < lst[0] < s[1] < lst[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
#include <bits/stdc++.h> using namespace std; int n; int x[2000]; vector<pair<int, int> > a; bool check(int p, int q, int z) { if (q > z) swap(q, z); if (p > q && p < z) return true; return false; } int main() { while (cin >> n) { for (int i = 0; i < n; ++i) scanf("%d", x + i); for (int i = 0; i < n - 1; ++i) { a.push_back(make_pair(x[i], x[i + 1])); } bool flag = false; for (int i = 0; i < n - 1; ++i) for (int j = 0; j < n - 1; ++j) if (i != j) { int x1 = min(a[i].first, a[i].second); int y1 = max(a[i].first, a[i].second); int x2 = min(a[j].first, a[j].second); int y2 = max(a[j].first, a[j].second); if ((x2 > x1 && x2 < y1) && (y1 > x2 && y1 < y2)) flag = true; if ((x1 > x2 && x1 < y2) && (y2 > x1 && y2 < y1)) flag = true; } 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
def main(): n = int(input()) l = [] a = 0 for b in map(int, input().split()): if a < b: l.append((a, b)) else: l.append((b, a)) a = b l[0] = ((0, 0)) l.sort() for i, (c, d) in enumerate(l): for j in range(i): a, b = l[j] if a < c < b < d: print('yes') return print('no') if __name__ == '__main__': main()
PYTHON3
358_A. Dima and Continuous Line
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≀ n ≀ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≀ xi ≀ 106) β€” the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right.
2
7
#include <bits/stdc++.h> typedef struct cf208d2aob { int v; int n; } cf208d2aob; int cf208d2aobcmp(const void *a, const void *b) { cf208d2aob *aa = (cf208d2aob *)a; cf208d2aob *bb = (cf208d2aob *)b; if (aa->v == bb->v) return aa->n - bb->n; return ((cf208d2aob *)a)->v - ((cf208d2aob *)b)->v; } int main() { int n; scanf("%d", &n); if (n < 3) { printf("no\n"); return 0; }; cf208d2aob x[2001]; int a; scanf("%d", &(x[0].v)); x[0].n = 0; int ns = 0; for (int i = 1, ij = 1; i < n; i++, ij += 2) { scanf("%d", &a); ns = ij + 1; x[ij].n = i - 1; if (!(x[ij - 1].v < a)) { x[ij].v = x[ij - 1].v; x[ij - 1].v = a; } else { x[ij].v = a; } x[ij + 1].v = a; x[ij + 1].n = i; } for (int i = 2 * 2; i < ns; i += 2) { for (int j = 0; j < i - 2; j += 2) { if (x[j].v > x[i].v && x[j].v < x[i + 1].v && x[j + 1].v > x[i + 1].v || x[j + 1].v < x[i + 1].v && x[j].v < x[i].v && x[j + 1].v > x[i].v) { 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
n = int(input()) a = list(map(int,input().split())) b = [] for i in range(1,n): b.append([min(a[i-1],a[i]),max(a[i-1],a[i])]) b.sort() yes = 1; for i in range(0,n-1): if(yes): for j in range(i+1,n-1): if b[j][0] >= b[i][1]: break; if b[j][1] > b[i][1] and b[j][0] > b[i][0]: # print(b[i][0],b[i][1],b[j][0],b[j][1]) yes = 0; # break; print(['yes','no'][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 solve(): n = input() A = map(int, raw_input().split()) for i in range(n-1): st_i = min(A[i], A[i+1]) end_i = max(A[i], A[i+1]) for j in range(i, n-1): st_j = min(A[j], A[j+1]) end_j = max(A[j], A[j+1]) if (st_j > st_i and st_j < end_i and end_j > end_i) or (st_j < st_i and end_j > st_i and end_j < end_i): print "yes" return print "no" solve()
PYTHON