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;
bool intersecting(int c11, int c12, int c21, int c22);
int main() {
int n;
cin >> n;
vector<int> v(n, 0);
int flag = 0;
for (int i = 0; i < n; ++i) {
cin >> v[i];
if (!flag)
for (int j = 1; j < i; ++j)
if (intersecting(v[j], v[j - 1], v[i], v[i - 1])) flag = 1;
}
if (flag)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
bool intersecting(int c11, int c12, int c21, int c22) {
int c1min = c11, c1max = c12;
if (c1max < c1min) swap(c1min, c1max);
int c2min = c21, c2max = c22;
if (c2max < c2min) swap(c2min, c2max);
if ((c1min < c2min && c1max > c2min && c1max < c2max) ||
(c2min < c1min && c2max > c1min && c2max < c1max))
return true;
else
return false;
}
| 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, x = input(), map(int, raw_input().split())
if n < 4: print "no"
else:
l, r, p = min(x[:2]), max(x[:2]), x[2]
o = p < l or p > r
for x in x[3:]:
if x > l and x < r if o else x < l or x > r:
print "yes"
break
if not o:
l, r = l if x < p else p, r if x > p else p
elif (x-l)*(x-p) < 0:
o, l, r = 0, p if p < l else r, p if p > r else l
else:
l, r = min(l, p), max(r, p)
p = x
else:
print "no" | PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[1111];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++)
for (int j = i + 2; j < n; j++) {
int x1 = a[i], y1 = a[i + 1], x2 = a[j], y2 = a[j + 1];
if (x1 > y1) swap(x1, y1);
if (x2 > y2) swap(x2, y2);
if (x2 > x1 && x2 < y1 && (y2 < x1 || y2 > y1)) {
cout << "yes";
return 0;
}
if (y2 > x1 && y2 < y1 && (x2 < x1 || x2 > y1)) {
cout << "yes";
return 0;
}
}
cout << "no";
cin >> 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(raw_input())
a=[int(i) for i in raw_input().split()]
flag=0
def check(i):
l,r=min(a[i],a[i+1]),max(a[i],a[i+1])
for x in xrange(i):
L,R=min(a[x],a[x+1]),max(a[x],a[x+1])
if not( (L<=l and r<=R) or (l<=L and R<=r) or r<=L or R<=l):
#print '000'
# print l,r,L,R
# print 'i=',i
return 1
return 0
for x in xrange(n):
if x+1<n:
if check(x):
print 'yes'
# print check(x)
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int num;
int points[1000];
int i, j;
int tmp_max[1000], tmp_min[1000];
bool flag = false;
cin >> num;
for (i = 0; i < num; i++) cin >> points[i];
for (i = 0; i < num - 1; i++) {
(points[i] < points[i + 1])
? (tmp_min[i] = points[i], tmp_max[i] = points[i + 1])
: (tmp_max[i] = points[i], tmp_min[i] = points[i + 1]);
}
for (i = 0; i < num - 2; i++) {
for (j = i; j >= 0; j--) {
if ((tmp_min[i + 1] > tmp_min[j] && tmp_min[i + 1] < tmp_max[j] &&
tmp_max[i + 1] > tmp_max[j]) ||
(tmp_max[i + 1] > tmp_min[j] && tmp_max[i + 1] < tmp_max[j] &&
tmp_min[i + 1] < tmp_min[j])) {
flag = true;
break;
}
}
if (flag) {
cout << "yes";
break;
}
}
if (flag == false) 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 sys
_ = raw_input()
p = map(int, raw_input().split())
pairs = [(p[i], p[i+1]) for i in range(len(p) - 1)]
pairs = map(sorted, pairs)
pairs = sorted(pairs, key= lambda s: s[0])
for i, t in enumerate(pairs):
j = i + 1
while j < len(pairs) and pairs[j][0] < pairs[i][1]:
if pairs[i][0] != pairs[j][0] and pairs[j][1] > pairs[i][1]:
print "yes"
sys.exit(0)
j += 1
print "no" | PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import sys
from collections import defaultdict, Counter
from itertools import permutations, combinations
from math import sin, cos, asin, acos, tan, atan, pi
sys.setrecursionlimit(10 ** 6)
def pyes_no(condition, yes = "YES", no = "NO", none = "-1") :
if condition == None:
print (none)
elif condition :
print (yes)
else :
print (no)
def plist(a, s = ' ') :
print (s.join(map(str, a)))
def rint() :
return int(sys.stdin.readline())
def rstr() :
return sys.stdin.readline().strip()
def rints() :
return map(int, sys.stdin.readline().split())
def rfield(n, m = None) :
if m == None :
m = n
field = []
for i in xrange(n) :
chars = sys.stdin.readline().strip()
assert(len(chars) == m)
field.append(chars)
return field
def pfield(field, separator = '') :
print ('\n'.join(map(lambda x: separator.join(x), field)))
def check_field_equal(field, i, j, value) :
if i >= 0 and i < len(field) and j >= 0 and j < len(field[i]) :
return value == field[i][j]
return None
def digits(x, p) :
digits = []
while x > 0 :
digits.append(x % p)
x //= p
return digits[::-1]
def undigits(x, p) :
value = 0
for d in x :
value *= p
value += d
return value
def modpower(a, n, mod) :
r = a ** (n % 2)
if n > 1 :
r *= modpower(a, n // 2, mod) ** 2
return r % mod
def gcd(a, b) :
if a > b :
a, b = b, a
while a > 0 :
a, b = b % a, a
return b
def vector_distance(a, b) :
diff = vector_diff(a, b)
return scalar_product(diff, diff) ** 0.5
def vector_inverse(v) :
r = [-x for x in v]
return tuple(r)
def vector_diff(a, b) :
return vector_sum(a, vector_inverse(b))
def vector_sum(a, b) :
r = [c1 + c2 for c1, c2 in zip(a, b)]
return tuple(r)
def scalar_product(a, b) :
r = 0
for c1, c2 in zip(a, b) :
r += c1 * c2
return r
def check_rectangle(points) :
assert(len(points) == 4)
A, B, C, D = points
for A1, A2, A3, A4 in [
(A, B, C, D),
(A, C, B, D),
(A, B, D, C),
(A, C, D, B),
(A, D, B, C),
(A, D, C, B),
] :
sides = (
vector_diff(A1, A2),
vector_diff(A2, A3),
vector_diff(A3, A4),
vector_diff(A4, A1),
)
if all(scalar_product(s1, s2) == 0 for s1, s2 in zip(sides, sides[1:])) :
return True
return False
def check_square(points) :
if not check_rectangle(points) :
return False
A, B, C, D = points
for A1, A2, A3, A4 in [
(A, B, C, D),
(A, C, B, D),
(A, B, D, C),
(A, C, D, B),
(A, D, B, C),
(A, D, C, B),
] :
side_lengths = [
(first[0] - next[0]) ** 2 + (first[1] - next[1]) ** 2 for first, next in zip([A1, A2, A3, A4], [A2, A3, A4, A1])
]
if len(set(side_lengths)) == 1 :
return True
return False
def check_right(p) :
# Check if there are same points
for a, b in [
(p[0], p[1]),
(p[0], p[2]),
(p[1], p[2]),
] :
if a[0] == b[0] and a[1] == b[1] :
return False
a, b, c = p
a, b, c = vector_diff(a, b), vector_diff(b, c), vector_diff(c, a)
return scalar_product(a, b) * scalar_product(a, c) * scalar_product(b, c) == 0
def modmatrixproduct(a, b, mod) :
n, m1 = len(a), len(a[0])
m2, k = len(b), len(b[0])
assert(m1 == m2)
m = m1
r = [[0] * k for i in range(n)]
for i in range(n) :
for j in range(k) :
for l in range(m) :
r[i][j] += a[i][l] * b[l][j]
r[i][j] %= mod
return r
def modmatrixpower(a, n, mod) :
magic = 2
for m in [2, 3, 5, 7] :
if n % m == 0 :
magic = m
break
r = None
if n < magic :
r = a
n -= 1
else :
s = modmatrixpower(a, n // magic, mod)
r = s
for i in range(magic - 1) :
r = modmatrixproduct(r, s, mod)
for i in range(n % magic) :
r = modmatrixproduct(r, a, mod)
return r
n = rint()
a = rints()
pairs = []
pairs = [(min(x, y), max(x, y)) for x, y in zip(a, a[1:])]
l = len(pairs)
for i in range(l - 2) :
for j in range(i + 2, l) :
(a1, a2), (b1, b2) = pairs[i], pairs[j]
if a1 < b1 < a2 < b2 or b1 < a1 < b2 < a2 :
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 = input()
a = map(int, raw_input().split())
def solve():
for i in xrange(n-1):
for j in xrange(n-1):
x1 = a[i]
x2 = a[i+1]
if x1 > x2: x1, x2 = x2, x1
x3 = a[j]
x4 = a[j+1]
if x3 > x4: x3, x4 = x4, x3
if x1 < x3 < x2 and x3 < x2 < x4:
return "yes"
return "no"
print solve()
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
bool v = 0;
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 (v) break;
if (max(x[i], x[i + 1]) < max(x[j], x[j + 1]) &&
min(x[i], x[i + 1]) < min(x[j], x[j + 1]) &&
max(x[i], x[i + 1]) > min(x[j], x[j + 1])) {
v = 1;
break;
}
}
}
if (v)
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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// acc
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 | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class dimaAndContinuousLine {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
class Point{
int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
}
List<Point> pts=new ArrayList<>();
List<Integer> in=new ArrayList<>();
boolean isF=false;
while(n-->0){
in.add(sc.nextInt());
}
for(int i=0;i<in.size();i++){
if(i+1<in.size()){
int x=in.get(i);
int y=in.get(i+1);
if(x>y){
int temp=x;
x=y;
y=temp;
}
if(!pts.isEmpty()){
for(Point p:pts){
if( (x>p.x && x<p.y && y>p.y)
|| (x<p.x && y>p.x && y<p.y )
)
{
System.out.println("yes");
isF=true;
break;
}
}
}
if(!isF){
Point p=new Point(x,y);
pts.add(p);
}
else{
break;
}
}
}
if(!isF){
System.out.println("no");
}
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(input())
a = list(map(int, input().split()))
segs = [sorted((a[i], a[i + 1])) for i in range(n - 1)]
for i in range(n - 1):
for j in range(n - 1):
if segs[i][0] < segs[j][0] < segs[i][1] < segs[j][1]:
print('yes')
exit()
print('no') | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n=int(input())
a=[*map(int,input().split())]
b=[]
for i in range(n-1):
b.append([min(a[i],a[i+1]),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][0]<b[j][0]<b[i][1]<b[j][1] or b[i][0]<b[j][1]<b[i][1]<b[j][0]:
print('yes')
exit(0)
print('no') | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.*;
public class CF358A {
static class Point{
int a;
int b;
Point(int a,int b){
this.a = a;
this.b = b;
}
}
public static void main(String[] args) {
FastReader input = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
ArrayList<Point> list = new ArrayList<Point>();
int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0;i < n;i++){
arr[i] = input.nextInt();
}
for(int i = 0;i < n-1;i++){
int a = arr[i];
int b = arr[i+1];
if(a > b){
int temp = a;
a = b;
b = temp;
}
list.add(new Point(a,b));
}
boolean con = false;
for(int i = 0;i < list.size();i++){
int a = list.get(i).a;
int b = list.get(i).b;
for(int j = 0;j < list.size();j++){
if(j != i){
int a2 = list.get(j).a;
int b2 = list.get(j).b;
if(a > a2 && a < b2 && b > b2){
con = true;
break;
}
}
}
if(con)
break;
}
if(con){
pw.println("yes");
}
else{
pw.println("no");
}
// ****If sorting is required, use ArrayList
pw.flush();
pw.close();
}
static void sort(int[] arr){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i : arr)
list.add(i);
Collections.sort(list);
for(int i = 0;i < list.size();i++){
arr[i] = list.get(i);
}
return;
}
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;
}
}
}
| 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 | inf = int(1e9+5)
n = int(input())
ar = list(map(int, input().split()))
pairs = []
for i in range(n-1):
pair = (min(ar[i],ar[i+1]), max(ar[i], ar[i+1]))
pairs.append(pair)
# print(pairs)
flag = False
for i in range(len(pairs)-1):
x1 = pairs[i][0]
x2 = pairs[i][1]
for j in range(i+1, len(pairs)):
x3 = pairs[j][0]
x4 = pairs[j][1]
if x1<x3<x2<x4 or x3<x1<x4<x2:
# print(pairs[i], pairs[j])
flag = True
break
if(flag):
break
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 a[1005];
int main() {
int n;
while (cin >> n) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
bool flag = 0;
for (int i = 0; i < n - 1; i++) {
int l = min(a[i], a[i + 1]);
int r = max(a[i], a[i + 1]);
for (int j = 0; j < n - 1; j++) {
if (j == i) continue;
if (a[j] < l && (a[j + 1] > l && a[j + 1] < r)) flag = 1;
if (a[j] > l && a[j] < r && (a[j + 1] < l || a[j + 1] > r)) flag = 1;
if (a[j] > r && a[j + 1] > l && a[j + 1] < r) flag = 1;
}
}
if (flag)
puts("yes");
else
puts("no");
}
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int points[((int)1e3)];
class line {
public:
void init() {
int N;
cin >> N;
for (int i = 0; i < N; i += 1) cin >> points[i];
for (int i = 0; i < N - 1; i += 1) {
int x1 = points[i], x2 = points[i + 1];
if (x1 > x2) swap(x1, x2);
for (int j = 0; j < i; j += 1) {
int x3 = points[j], x4 = points[j + 1];
if (x3 > x4) swap(x3, x4);
if ((x3 < x1 && x4 > x1 && x4 < x2) ||
(x3 < x2 && x4 > x2 && x1 < x3)) {
cout << "yes" << endl;
return;
}
}
}
cout << "no" << endl;
}
};
int main() {
line obj;
obj.init();
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>
const long long MXN = 1e6 + 1;
const long long MNN = 1e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
using namespace std;
long long n, m, b[MXN], mx = -1, mn = INF, a, ans, k, sum, x, q, c[MXN],
pr[MXN];
bool used[MXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int l1 = min(b[i], b[i + 1]);
int r1 = max(b[i], b[i + 1]);
int l2 = min(b[j], b[j + 1]);
int r2 = max(b[j], b[j + 1]);
if (l2 < l1 && l1 < r2 && r2 < r1) {
cout << "yes";
return 0;
}
if (l1 < l2 && l2 < r1 && r1 < r2) {
cout << "yes";
return 0;
}
}
}
cout << "no";
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n,l=int(input()),list(map(int,input().split()))
for i in range(n-1):
for j in range(n-1):
x=sorted([l[i],l[i+1]])+sorted([l[j],l[j+1]])
if x[0]<x[2]<x[1]<x[3]:
exit(print('yes'))
print('no') | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
int p[20000];
bool pend(int a, int b, int c, int d) {
if (a > b) swap(a, b);
if (c > d) swap(c, d);
if (a > c) {
swap(a, c);
swap(b, d);
}
return a < c && b > c && b < d;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> p[i];
bool can = true;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < i; ++j)
if (pend(p[i], p[i + 1], p[j], p[j + 1])) {
can = false;
break;
}
if (!can) break;
}
if (can)
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 | #include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
long long int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
;
bool flag = true;
for (int i = 1; i < n - 1; i++) {
long long int maxi = max(a[i], a[i + 1]);
long long int mini = min(a[i], a[i + 1]);
for (int j = 0; j < n - 1; j++) {
long long int maxj = max(a[j], a[j + 1]);
long long int minj = min(a[j], a[j + 1]);
if ((mini < minj && (maxi < maxj && maxi > minj)) ||
((mini > minj && mini < maxj) && maxi > maxj)) {
flag = false;
break;
}
}
}
if (flag == false)
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 | n = int(input())
Min = -1e10
Max = 1e10
ans = "no"
def f(x, y):
return x[0] < y[0] < x[1] < y[1] or \
y[0] < x[0] < y[1] < x[1]
xs = list(map(int, input().split()))
for i in range(1, len(xs) - 1):
for j in range(0, i):
if f([min(xs[i:i+2]), max(xs[i:i+2])], [min(xs[j:j+2]), max(xs[j:j+2])]):
ans = "yes"
break
print(ans) | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | //package com.example.hackerranksolutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class CodeforcesProblems {
static boolean checkSelfIntersections(int[] start, int[] end, int pos, int x2, int y2) {
for(int i = pos; i>=0; i--) {
int x1 = start[i];
int y1 = end[i];
if(x1<x2 && x2<y1 && y2>y1) return true;
if(x2<x1 && x1<y2 && y1>y2) return true;
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int start[] = new int[n];
int end[] = new int[n];
int x = in.nextInt();
if(n == 1) {
System.out.println("no");
return;
}
int y = in.nextInt();
start[0] = x<y ? x:y;
end[0] = x>y ? x:y;
boolean isSelfIntersect = false;
for(int i = 2; i<n; i++) {
x = in.nextInt();
if(isSelfIntersect) {
continue;
}
start[i-1] = x<y?x:y;
end[i-1] = x>y?x:y;
isSelfIntersect = checkSelfIntersections(start, end, i-3, start[i-1], end[i-1]);
y = x;
}
System.out.println(isSelfIntersect ? "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.util.*;
public class DimaLine {
public static void swap(int a, int b)
{
}
public static void main(String[] args) {
int x1,x2,x3,x4;
int c = 0;
Scanner myObj = new Scanner(System.in);
int n = myObj.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = myObj.nextInt();
}
for(int i=0;i<n-1;i++)
{
//System.out.println("***inside 1st for loop*****");
x1=a[i];
x2=a[i+1];
for(int j=i+1;j<n-1;j++)
{
// System.out.println("******inside 2nd for loop*******");
x3=a[j];
x4=a[j+1];
if(x2<x1)
{
// System.out.println("******inside first swap*******");
int temp = x1;
x1 = x2;
x2 = temp;
}
if(x4<x3)
{
// System.out.println("******inside 2nd swap*******");
int temp = x3;
x3 = x4;
x4 = temp;
}
if((x1<x3 && x3<x2 && x2<x4) || (x3<x1 && x1<x4 && x4<x2))
{
c++;
}
}
}
if(c>0)
System.out.println("yes");
else
System.out.println("no");
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Temp2 {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < arr.length; i++)
{
arr[i] = Integer.parseInt(st.nextToken());
}
boolean found = false;
for (int i = 0; i < arr.length-1 && !found; i++)
{
int beg = arr[i] ;
int end = arr[i+1];
int tmp = beg;
beg = Math.min(beg, end);
end = Math.max(end, tmp);
for (int j = 0; j < arr.length-1; j++)
{
int beg1 = arr[j];
int end1 = arr[j+1];
int tmp1 = beg1;
beg1 = Math.min(beg1, end1);
end1 = Math.max(end1, tmp1);
if( (beg1<beg && end1<end && end1>beg) || (beg1>beg && beg1<end && end1>end ) )
{
found = true;
}
}
}
if(found)
System.out.println("yes");
else
System.out.println("no");
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | /*
Link of Question : http://codeforces.com/contest/358/problem/A
*/
import java.io.*;
import java.util.*;
public class DimaAndContinuousLine
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int A[]=new int[n];
for (int i = 0; i < n; i++)
{
A[i]=in.nextInt();
}
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n-1; j++)
{
int X=Math.min(A[i], A[i+1]);
int Y=Math.max(A[i], A[i+1]);
int X1=Math.min(A[j], A[j+1]);
int Y1=Math.max(A[j], A[j+1]);
if (X1<Y && Y1>Y && X1>X || X1<X && Y1>X &&Y>Y1 )
{
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 sys
#sys.stdin = open("in.txt", "r")
n = input()
a = map(int, raw_input().split())
if n < 3:
print "no"
else:
b = []
for i in xrange(n-1):
b.append([a[i], a[i+1]])
good = False
for i in xrange(n-1):
b[i].sort()
for i in xrange(n-1):
for j in xrange(i+1, n-1):
t1 = b[i][0] > b[j][0] and b[i][0] < b[j][1] and b[i][1] > b[j][1]
t2 = b[i][1] > b[j][0] and b[i][1] < b[j][1] and b[i][0] < b[j][0]
if t1 or t2:
good = True
break
if good:
print "yes"
else:
print "no"
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 77;
const double EPS = 1e-9;
const int MOD = 1000007;
const int INF = 0x7fffffff;
template <class Int>
inline int size(const Int &a) {
return (int)a.size();
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<pair<int, int> > ans;
vector<int> seq(n);
for (__typeof(n) i = 0; i < (n); i++) cin >> seq[i];
for (__typeof(n - 1) i = 0; i < (n - 1); i++) {
ans.push_back(make_pair(min(seq[i], seq[i + 1]), max(seq[i], seq[i + 1])));
}
sort(seq.begin(), seq.end());
for (__typeof(0) i = (0); i < (size(ans)); i++) {
for (__typeof(0) j = (0); j < (size(ans)); j++)
if (i == j)
continue;
else if ((ans[i].first < ans[j].first && ans[i].second > ans[j].first &&
ans[i].second < ans[j].second)) {
cout << "yes" << endl;
return 0;
}
}
cout << "no" << endl;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | def test():
n=int(input())
points=list(map(int,input().split()))
for i in range(n-1):
a=points[i]
b=points[i+1]
if(a>b):
temp=a
a=b
b=temp
for j in range(i):
if(points[j]>=a and points[j]<=b):
if(j>0 and (points[j-1]<a or points[j-1]>b or points[j+1]<a or points[j+1]>b)):
print('yes')
return 0
elif(j==0 and (points[j+1]<a or points[j+1]>b)):
print('yes')
return 0
return 1
if(test()):
print('no')
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = input()
a = map(int, raw_input().split())
for i in range(n - 1):
l1, r1 = min(a[i], a[i + 1]), max(a[i], a[i + 1])
for j in range(n - 1):
l2, r2 = min(a[j], a[j + 1]), max(a[j], a[j + 1])
if l1 < l2 < r1 < r2:
print 'yes'
exit()
print 'no'
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.PrintWriter;
import java.util.Scanner;
public class A358 {
static Scanner in = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int n = in.nextInt();
int data[] = new int [n];
int ans = 0;
boolean p = true;
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
}
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n-1; j++) {
int a = data[i] , b = data[i+1] , c = data[j] , d = data[j+1];
if(a>b){
int swap = a;
a = b;
b = swap;
}
if(c > d){
int swap = c;
c = d;
d = swap;
}
if(a < c && b > c && b < d || a > c && a<d && b>d)
p = false;
}
}
if(!p)
out.printf("yes \n");
else
out.printf("no \n");
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.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
int[] nums = new int[n];
StringTokenizer row = new StringTokenizer(br.readLine().trim());
for(int i = 0; i < n; i++){
nums[i] = Integer.parseInt(row.nextToken().trim());
}
boolean found = false;
for(int i = 1; i < n; i++){ // check window i - 1 & i
for(int j = 1; j < i; j++){ //window of 2, between j - 1 & j
int iSmall = Math.min(nums[i], nums[i - 1]);
int iLarge = Math.max(nums[i], nums[i - 1]);
int jSmall = Math.min(nums[j], nums[j - 1]);
int jLarge = Math.max(nums[j], nums[j - 1]);
if((iSmall > jSmall && iSmall < jLarge && iLarge > jLarge) || (jSmall > iSmall && jSmall < iLarge && jLarge > iLarge)){
found = true;
}
}
}
System.out.println(found ? "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 | n = int(input())
x = list(map(int,input().split()))
p = x[0]
m = []
for i in range(1, n):
for a, b in m[:-1]:
if (a < p < b) ^ (a < x[i] < b):
print("yes")
exit()
m.append([min(p,x[i]),max(p,x[i])])
p = x[i]
print("no") | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
const double eps = 1e-6;
int a[N];
int main() {
int n, i, j, f;
double d, r1, r2;
while (~scanf("%d", &n)) {
f = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
for (j = 1; j < i; j++) {
d = fabs((double)(a[j - 1] + a[j]) / 2.0 -
(double)(a[i] + a[i - 1]) / 2.0);
r1 = fabs((double)(a[i] - a[i - 1]) / 2.0);
r2 = fabs((double)(a[j] - a[j - 1]) / 2.0);
if (d > fabs(r1 - r2) + eps && d + eps < fabs(r1 + r2)) f = 1;
}
}
if (f == 1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ContinuousLine {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] strNumeros = br.readLine().split(" ");
int x1, x2, x3, x4;
boolean stop = false;
for (int i = 0; i < n-1; i++){
x1 = Integer.parseInt(strNumeros[i]);
x2 = Integer.parseInt(strNumeros[i+1]);
for (int j = i ; j > 0; j--){
x3 = Integer.parseInt(strNumeros[j]);
x4 = Integer.parseInt(strNumeros[j-1]);
if ((x4 < x1 && x1 < x3 && x3 < x2) || (x3 < x1 && x1 < x4 && x4 < x2) ||
(x1 < x3 && x3 < x2 && x2 < x4) || (x1 < x4 && x4 < x2 && x2 < x3) ||
(x4 < x2 && x2 < x3 && x3 < x1) || (x3 < x2 && x2 < x4 && x4 < x1) ||
(x2 < x3 && x3 < x1 && x1 < x4) || (x2 < x4 && x4 < x1 && x1 < x3)){
System.out.println("yes");
stop = true;
break;
}
}
if (stop){ break; }
}
if (!stop){ 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 div2a {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 1 || n == 2) {
System.out.println("no");
return;
}
int a[] = new int[n + 1], x[] = new int[n + 1], y[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
for (int i = 1; i < n; i++) {
x[i] = a[i];
y[i] = a[i + 1];
if (x[i] > y[i]) {
int r = x[i];
x[i] = y[i];
y[i] = r;
}
}
for (int i = 1; i <= n - 1; i++) {
for (int j = i + 1; j <= n ; j++) {
if (!((x[i] <= x[j] && y[j] <= y[i])
|| (x[j] >= y[i] && y[j] >= y[i])
|| (y[j] <= x[i] && x[j] <= x[i]) || (x[i] >= x[j] && y[i] <= y[j]))) {
System.out.println("yes");
return;
}
}
}
System.out.println("no");
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/*
* @author Mahmoud Aladdin <aladdin3>
*
*/
public class Main {
public static void main (String[] args) throws java.lang.Exception {
InputReader scan = new InputReader(System.in);
PrintWriter writer = new PrintWriter(System.out, true);
(new Main(scan, writer)).run();
writer.close();
}
private InputReader jin;
private PrintWriter jout;
public Main(InputReader in, PrintWriter out) {
this.jin = in;
this.jout = out;
}
public void run() throws Exception {
int n = jin.integer();
int[] px = new int[n - 1];
int[] py = new int[n - 1];
int pi = 0;
int a = jin.integer();
for(int i = 1; i < n; i++) {
int b = jin.integer();
for(int j = 0; j < pi; j++) {
boolean aout = a < px[j] || a > py[j];
boolean bout = b < px[j] || b > py[j];
if(a == px[j] || a == py[j] || b == px[j] || b == py[j]) {
continue;
}
if(aout ^ bout) {
jout.println("yes");
return;
}
}
px[pi] = Math.min(a, b);
py[pi++] = Math.max(b, a);
a = b;
}
jout.println("no");
}
}
class InputReader {
private static final int bufferMaxLength = 1024;
private InputStream in;
private byte[] buffer;
private int currentBufferSize;
private int currentBufferTop;
private static final String tokenizers = " \t\r\f\n";
public InputReader(InputStream stream) {
this.in = stream;
buffer = new byte[bufferMaxLength];
currentBufferSize = 0;
currentBufferTop = 0;
}
private boolean refill() throws IOException {
this.currentBufferSize = this.in.read(this.buffer);
this.currentBufferTop = 0;
return this.currentBufferSize > 0;
}
private Byte readChar() throws IOException {
if(currentBufferTop < currentBufferSize) {
return this.buffer[this.currentBufferTop++];
} else {
if(!this.refill()) {
return null;
} else {
return readChar();
}
}
}
public String token() throws IOException {
StringBuffer tok = new StringBuffer();
Byte first;
while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1));
if(first == null) return null;
tok.append((char)first.byteValue());
while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) {
tok.append((char)first.byteValue());
}
return tok.toString();
}
public String line() throws IOException {
Byte first;
StringBuffer line = new StringBuffer();
while ((first = readChar()) != null && (char)first.byteValue() != '\n') {
line.append((char)first.byteValue());
}
return (first == null && line.length() == 0)? null : line.toString();
}
public Integer integer() throws NumberFormatException, IOException {
String tok = token();
return tok == null? null : Integer.parseInt(tok);
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = in.nextInt();
TreeSet<Integer> tree = new TreeSet<Integer>();
int minNext = 0;
int maxNext = 0;
int inOut = 0;
while (tree.size() < max) {
int next = in.nextInt();
if (inOut > 0 && (next < maxNext && next > minNext)) {
System.out.println("yes");
in.close();
return;
}
if (inOut < 0 && (next > maxNext || next < minNext)) {
System.out.println("yes");
in.close();
return;
}
tree.add(next);
if (tree.size() < 3) continue;
if (tree.first() == next) {
minNext = tree.higher(next);
maxNext = tree.last();
inOut = 1;
}
else if (tree.last() == next) {
minNext = tree.first();
maxNext = tree.lower(next);
inOut = 1;
}
else {
minNext = tree.lower(next);
maxNext = tree.higher(next);
inOut = -1;
}
}
System.out.println("no");
in.close();
return;
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(input())
a = list(map(int,input().split()))
coordinates = []
for i in range(0,n-1):
x = a[i]
y = a[i+1]
coordinates.append([x,y])
# print(coordinates)
f = 'no'
for i in range(0,n-2):
if(f == 'yes'):
break
for j in range(i+1,n-1):
# print(j)
chota1 = min(coordinates[i])
bada1 = max(coordinates[i])
chota2 = min(coordinates[j])
bada2 = max(coordinates[j])
if(chota2>=chota1 and bada2<= bada1) or (chota2 <= chota1 and bada2>= bada1) or(chota2 >= bada1) or (bada2 <= chota1):
continue
else:
f = 'yes'
break
print(f)
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int EPS = 1e-6;
const int INF = (int)(INT_MAX - 100);
const long long mod = (int)(1e+9 + 7);
const int N = (int)(0);
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
n--;
vector<pair<int, int> > s(n);
for (int i = 0; i < n; i++) {
s[i] = {v[i], v[i + 1]};
if (s[i].first > s[i].second) swap(s[i].first, s[i].second);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
pair<int, int> p[2] = {s[i], s[j]};
sort(p, p + 2);
if (p[0].first < p[1].first && p[0].second < p[1].second &&
p[1].first < p[0].second)
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 | #include <bits/stdc++.h>
int main() {
int n, a[2000];
bool check = false;
scanf("%d", &n);
memset(a, sizeof(a), 0);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; ++i) {
if (!check)
for (int j = 0; j < n - 1; ++j) {
if (j != i && i != j + 1 && i != j - 1) {
if (a[i] <= a[j] && a[i] >= a[j + 1] &&
(a[i + 1] > a[j] || a[i + 1] < a[j + 1])) {
check = true;
break;
}
if (a[i] >= a[j] && (a[i + 1] < a[j] || a[i + 1] > a[j + 1]) &&
a[i] <= a[j + 1]) {
check = true;
break;
}
if (a[i + 1] <= a[j] && a[i + 1] >= a[j + 1] &&
(a[i] > a[j] || a[i] < a[j + 1])) {
check = true;
break;
}
if (a[i + 1] >= a[j] && (a[i] < a[j] || a[i] > a[j + 1]) &&
a[i + 1] <= a[j + 1]) {
check = true;
break;
}
}
}
}
if (check)
printf("yes\n");
else
printf("no\n");
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
bool check(pair<int, int> a, pair<int, int> b) {
if (a.first > b.first && a.first < b.second &&
(a.second > b.second || a.second < b.first))
return true;
if (a.second > b.first && a.second < b.second &&
(a.first > b.second || a.first < b.first))
return true;
return false;
}
int main() {
int n;
cin >> n;
int ar[n];
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
vector<pair<int, int> > vp;
for (int i = 1; i < n; i++) {
vp.push_back({ar[i], ar[i - 1]});
if (vp.back().first > vp.back().second)
swap(vp.back().first, vp.back().second);
}
bool f = false;
for (int i = 0; i < vp.size(); i++) {
for (int j = i + 1; j < vp.size(); j++) {
if (check(vp[i], vp[j])) {
f = true;
break;
}
}
}
if (f)
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 A[1005];
vector<pair<int, int> > V;
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i];
if (n == 1) {
cout << "no";
return 0;
}
for (int i = 1; i < n; i++)
V.push_back(make_pair(min(A[i], A[i - 1]), max(A[i], A[i - 1])));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (V[i].first < V[j].first && V[i].second < V[j].second &&
V[j].first < V[i].second) {
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 | t=raw_input()
a=map(int,raw_input().split())
def mi(p1,p2):
if p1<p2:
return p1
else:
return p2
def ma(p1,p2):
if p1>p2:
return p1
else:
return p2
def be(p1,p2,p):
if p>mi(p1,p2) and p<ma(p1,p2):
return 1
elif p>ma(p1,p2) or p<min(p1,p2):
return -1
else:
return 0
b='no'
for i in range(len(a)-1):
for j in range(i+1,len(a)-1):
#print be(a[i],a[i+1],a[j]),be(a[i],a[i+1],a[j+1]),a[i],a[i+1],a[j],a[j+1]
if be(a[i],a[i+1],a[j])+be(a[i],a[i+1],a[j+1])==0:
b='yes'
break
print b | 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())
s = [int(i) for i in input().split()]
s = [s[i:i+2] for i in range(n-1)]
f = 0
for i in range(n-1):
a,b = min(s[i]),max(s[i])
for j in range(n-1):
if i!=j:
c,d = min(s[j]),max(s[j])
if (a<c and c<b and b<d) or (c<a and a<d and d<b):
print('yes')
f = 1
break
if f == 1:
break
if f == 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 | n = int(raw_input())
m = map(int, raw_input().split())
res = "no"
for i in range(1, n):
for j in range(1, i - 1):
l = min(m[j], m[j - 1])
r = m[j] + m[j - 1] - l
if m[i]> l and m[i] < r:
if not m[i - 1] > l or not m[i - 1] < r:
res = "yes"
if m[i - 1]> l and m[i - 1] < r:
if not m[i] > l or not m[i] < r:
res = "yes"
print res
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.*;
import java.util.*;
import java.math.*;
import java.util.stream.Collectors;
import java.util.stream.*;
public class Main {
static class Pair {
int x,y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws java.lang.Exception {
Reader pm =new Reader();
//Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = 1;
while(t-- > 0){
int n = pm.nextInt();
int[] a = new int[n];
ArrayList<Pair> al = new ArrayList<>();
for(int i=0;i<n;i++) {
a[i] = pm.nextInt();
}
for(int i = 1; i < n; i++) {
if(a[i - 1] < a[i])
al.add(new Pair(a[i - 1], a[i]));
else
al.add(new Pair(a[i], a[i - 1]));
}
for(int i = 0; i < al.size(); i++) {
for(int j = 0; j < al.size(); j++) {
if(i == j)
continue;
int x1 = al.get(i).x;
int y1 = al.get(i).y;
int x2 = al.get(j).x;
int y2 = al.get(j).y;
if(x1 < x2 && x2 < y1 && y1 < y2) {
System.out.println("yes");
return;
}
if(x2 < x1 && x1 < y2 && y2 < y1) {
System.out.println("yes");
return;
}
}
}
System.out.println("no");
}
}
// function to count elements within given range
static int countInRange(int arr[], int n, int x, int y) {
// initialize result
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
static int lowerIndex(int arr[], int n, int x) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
// function to find last index <= y
static int upperIndex(int arr[], int n, int y) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
public static StringBuilder dec_to_bin(long n) {
// decimal to binary upto 30 binaries
if(n==0) {
StringBuilder str=new StringBuilder("");
for(int i=0;i<30;i++) {
str.append("0");
}
return str;
}
StringBuilder str=new StringBuilder("");
while(n!=0) {
str.append(n%2L);
n/=2L;
}
str=str.reverse();
StringBuilder tmp_str=new StringBuilder("");
int len=str.length();
while(len!=30) {
tmp_str.append("0");
len++;
}
tmp_str.append(str);
return tmp_str;
}
private static int binarySearchPM(int[] arr, int key){
int n = arr.length;
int mid = -1;
int begin = 0,end=n;
while(begin <= end){
mid = (begin+end) / 2;
if(mid == n){
return n;
}
if(key < arr[mid]){
end = mid-1;
} else if(key > arr[mid]){
begin = mid+1;
} else {
return mid;
}
}
//System.out.println(begin+" "+end);
return -begin; //expected Index
}
private static List<Integer> setToList(Set<Integer> s){
List<Integer> al = s.stream().collect(Collectors.toList());
return al;
}
private static List<Integer> mapValToList(HashMap<Integer,Integer> map){
return map.values().stream().collect(Collectors.toList());
}
private static List<Integer> mapKeyToList(HashMap<Integer,Integer> map){
return map.keySet().stream().collect(Collectors.toList());
}
private static HashMap<Integer, Integer> sortByKey(HashMap<Integer,Integer> map){
return map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String []str=br.readLine().split(" ");
int []arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
for(int i=n-2;i>=0;i--){
int st=Math.min(arr[i],arr[i+1]);
int e=Math.max(arr[i],arr[i+1]);
for(int j=i-2;j>=0;j--){
int st1=Math.min(arr[j],arr[j+1]);
int e1=Math.max(arr[j],arr[j+1]);;
if((st1>=e && e1>=e) || (e1<=st && st1<=st) || (st1>=st && st1<=e && e1>=st && e1<=e) || (st1<=st && e1>=e)){
continue;
}else{
System.out.println("yes");
return;
}
}
}
System.out.println("no");
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 |
// author: Ahmed A.M
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//S1 p1 = new S1(); p1.solve();
//S2 p2 = new S2(); p2.solve();
//S3 p3 = new S3(); p3.solve();
//S4 p4 = new S4(); p4.solve();
//S5 p5 = new S5(); p5.solve();
//S6 p6 = new S6(); p6.solve();
//S7 p7 = new S7(); p7.solve();
//S8 p8 = new S8(); p8.solve();
//S9 p9 = new S9(); p9.solve();
//S10 p10 = new S10(); p10.solve();
//S11 p11 = new S11(); p11.solve();
//S12 p12 = new S12(); p12.solve();
//S13 p13 = new S13(); p13.solve(); * confusing
//S14 p14 = new S14(); p14.solve();
//S15 p15 = new S15(); p15.solve();
//S16 p16 = new S16(); p16.solve();
//S17 p17 = new S17(); p17.solve();
//S18 p18 = new S18(); p18.solve(); * without help?
//S19 p19 = new S19(); p19.solve(); * without set ?
//S20 p20 = new S20(); p20.solve(); * confusing * Data Types
//S21 p21 = new S21(); p21.solve(); * confusing and fuzzy thinking
//S22 p22 = new S22(); p22.solve();
//S23 p23 = new S23(); p23.solve(); * very annoying problem in very annoying moments
//S24 p24 = new S24(); p24.solve();
//S25 p25 = new S25(); p25.solve();
//S26 p26 = new S26(); p26.solve();
//S27 p27 = new S27(); p27.solve();
//S28 p28 = new S28(); p28.solve();
//S29 p29 = new S29(); p29.solve();
//S30 p30 = new S30(); p30.solve();
//S31 p31 = new S31(); p31.solve();
//S32 p32 = new S32(); p32.solve();
//S33 p33 = new S33(); p33.solve();
//S34 p34 = new S34(); p34.solve();
//S35 p35 = new S35(); p35.solve();
//S36 p36 = new S36(); p36.solve(); * Repeated mistakes
//S37 p37 = new S37(); p37.solve();
//S38 p38 = new S38(); p38.solve();
//S39 p39 = new S39(); p39.solve(); * Not good
//S40 p40 = new S40(); p40.solve();
//S41 p41 = new S41(); p41.solve(); * Observations
//S42 p42 = new S42(); p42.solve();
//S43 p43 = new S43(); p43.solve(); * don't understand it
//S45 p45 = new S45(); p45.solve(); * 2
//S46 p46 = new S46(); p46.solve(); * 1
//S47 p47 = new S47(); p47.solve(); * 1
//S48 p48 = new S48(); p48.solve(); * 1
//S49 p49 = new S49(); p49.solve(); * 4 * Cascading of mistakes
//S50 p50 = new S50(); p50.solve(); * 2 I've to eat now
//S51 p51 = new S51(); p51.solve(); * 1
//S52 p52 = new S52(); p52.solve(); * 2
//S53 p53 = new S53(); p53.solve(); * 3
//S54 p54 = new S54(); p54.solve(); * 1
//S55 p55 = new S55(); p55.solve(); * 1
//S56 p56 = new S56(); p56.solve(); * 1
//S57 p57 = new S57(); p57.solve(); * 1
//S58 p58 = new S58(); p58.solve(); * 3
//S59 p59 = new S59(); p59.solve(); * 1
//S60 p60 = new S60(); p60.solve(); * 1
//S61 p61 = new S61(); p61.solve(); * 1
//S62 p62 = new S62(); p62.solve(); * 1
//S63 p63 = new S63(); p63.solve(); * 1
//S64 p64 = new S64(); p64.solve(); *
// -----
//S67 p67 = new S67(); p67.solve(); * 2
//S68 p68 = new S68(); p68.solve(); * 1
//S69 p69 = new S69(); p69.solve();
//S70 p70 = new S70(); p70.solve(); * 1
//S71 p71 = new S71(); p71.solve();
//S72 p72 = new S72(); p72.solve();
//S73 p73 = new S73(); p73.solve();
S74 p74 = new S74(); p74.solve();
}
}
class S74 {
public static void solve(){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-1; j++) {
if (i!=j){
int x1 = Math.min(arr[i], arr[i+1]), x2 = Math.max(arr[i], arr[i+1]), x3 = Math.min(arr[j], arr[j+1]), x4 = Math.max(arr[j], arr[j+1]);
if ((x1 < x3 && x3 < x2 && x2 < x4) || (x3 < x1 && x1 < x4 && 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;
const int L = 1010;
struct semicircle {
int a, b;
} b[L];
int a[L], n, flag;
void init(void) {
cin >> n;
int i;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i < n; i++)
b[i].a = min(a[i], a[i + 1]), b[i].b = max(a[i], a[i + 1]);
}
int pd(int a, int b, int c, int d) {
if (a < b && b < c && c < d) return 1;
return 0;
}
void work(void) {
int i, j;
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++)
if (i != j && pd(b[i].a, b[j].a, b[i].b, b[j].b)) {
flag = 1;
break;
}
if (flag) break;
}
if (flag)
printf("yes\n");
else
printf("no\n");
}
int main(void) {
init();
work();
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 Check(I,J):
if Num[I][0]==Num[J][0]:
if Num[I][1]==Num[J][1]:
return 1
elif Num[J][0]<Num[I][1]and Num[I][1]<Num[J][1]:
return 1
return 0
N=int(input())
Tmp=list(map(int,input().split()))
Num=[(min(Tmp[I-1],Tmp[I]),max(Tmp[I-1],Tmp[I]))for I in range(1,N)]
Num.sort()
Flg=0
for I in range(len(Num)):
for J in range(I+1,len(Num)):
if Check(I,J)==1:
Flg=1
break
if Flg:
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.math.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream inputstream = System.in;
OutputStream outputstream = System.out;
InputReader in = new InputReader(inputstream);
OutputWriter out = new OutputWriter(outputstream);
mysolver mysol = new mysolver();
mysol.solve(in, out);
out.close();
}
}
class mysolver {
public void solve(InputReader in,OutputWriter out)
{
int i,j,k,l,m,n,t;
n = in.readInt();
int A[] = new int[n];
int check=0;
int ans=0;
for(i=0;i<n;i++)
{
A[i]=in.readInt();
if(check!=2)
check=0;
else
continue;
if(i!=0 && check!=2)
{
for(j=0;j<i-1;j++)
{
if((A[j]>A[i-1] && A[j]<A[i]) || (A[j]<A[i-1] && A[j]>A[i]))
{
check++;
break;
}
}
for(j=0;j<i-1;j++)
{
if((A[j] > A[i-1] && A[j] > A[i]) || (A[j] < A[i-1] && A[j] < A[i]))
{
check++;
break;
}
}
}
}
if(check==2)
System.out.println("yes");
else
System.out.println("no");
}
}
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 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 isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
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 OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | def line(lst):
b = list()
for i in range(len(lst) - 1):
b.append((min(lst[i], lst[i + 1]), max(lst[i], lst[i + 1])))
for i in range(len(b)):
for j in range(len(b)):
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]:
return "yes"
return "no"
n = int(input())
x = [int(z) for z in input().split()]
print(line(x))
| 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>
int main() {
int n;
scanf("%d", &n);
int a[n];
int i;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
int f1 = 0, j, f2;
for (i = 0; i < (n - 1); i++) {
f1 = 0;
f2 = 0;
for (j = i + 2; j < n; j++) {
if ((a[j] > a[i] && a[j] > a[i + 1]) || (a[j] < a[i] && a[j] < a[i + 1]))
f1 = 1;
else
f2 = 1;
}
if (f1 == 1 && f2 == 1) break;
}
if (f1 == 1 && f2 == 1)
printf("yes\n");
else
printf("no\n");
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | def do_pairs_intersect(start1, end1, start2, end2):
if ((end1 <= start2 or start1 >= end2) or
(start2>=start1 and start2<=end1 and end2>=start1 and end2<=end1) or
(start1>=start2 and start1<=end2 and end1>=start2 and end1<=end2)):
return False
return True
def is_intersecting(points):
if len(points) == 1:
return False
else:
n = len(points) - 1
for i in xrange(n-1):
for j in xrange(i+1,n):
if points[i] <= points[i+1]:
start1, end1 = points[i], points[i+1]
else:
start1, end1 = points[i+1], points[i]
if points[j] <= points[j+1]:
start2, end2 = points[j], points[j+1]
else:
start2, end2 = points[j+1], points[j]
#print 'comparing(',start1,',',end1,') and (',start2,',',end2,')'
if ((start1 == start2 and end1 == end2) or
(do_pairs_intersect(start1,end1,start2,end2))):
return True
return False
nums = map(int, raw_input('').split())
nums = map(int, raw_input('').split())
if is_intersecting(nums):
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.Arrays;
import java.util.Scanner;
public class Dima_and_Continuous_Line {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
for (int i = 0 ; i < n ; i++) {
arr[i] = scan.nextInt();
}
System.out.println(solve(arr));
}
static String solve(int[] arr) {
for (int i = 0; i < arr.length-2; i++) {
int in = 0, out = 0;
for (int j = i + 2; j < arr.length; j++) {
if ((arr[j] > arr[i] && arr[j] > arr[i+1]) || (arr[j] < arr[i] && arr[j] < arr[i+1])) out++;
else in++;
if (in > 0 && out > 0) {
return "yes";
}
}
}
return "no";
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n=int(input())
li=[int(x) for x in input().split()]
if n==1 or n==2 or n==3:
print('no')
else:
possible=True
for i in range(1,n):
x1,x2=min(li[i-1],li[i]),max(li[i-1],li[i])
for j in range(i+1,n):
x3,x4=min(li[j-1],li[j]),max(li[j-1],li[j])
if x1<x3 and x3<x2 and x2<x4:
print('yes')
possible=False
break
elif x3<x1 and x1<x4 and x4<x2:
print('yes')
possible=False
break
if possible==False:
break
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 sys
def fun(x1,x2,y1,y2):
if x1<x2:
if (y1<x1 and y2>x1 and y2<x2) or (y2<x1 and y1>x1 and y1<x2):
return True
elif (y1>x1 and y1<x2 and y2>x2) or (y2>x1 and y2<x2 and y1>x2):
return True
else:
if (y1<x2 and y2>x2 and y2<x1) or (y2<x2 and y1>x2 and y1<x1):
return True
elif (y1>x2 and y1<x1 and y2>x1) or (y2>x2 and y2<x1 and y1>x1):
return True
return False
n=int(input())
points=list(map(int,input().split()))
i=0
while i<n-1:
j=i+1
while j<n-1:
if fun(points[i],points[i+1],points[j],points[j+1]):
print("yes")
sys.exit()
j+=1
i+=1
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 | l=lambda: map(int,raw_input().split())
n=input()
a=l()
d=[(min(a[i-1],a[i]),max(a[i-1],a[i]),abs(a[i]-a[i-1])) for i in range(1,n)]
d.sort(key=lambda e:(e[0],e[2]))
for i in range(n-1):
x,y=d[i][0],d[i][1]
for j in range(n-1):
if i==j:continue
X,Y=d[j][0],d[j][1]
if (x<X<y and Y>y) or (x<Y<y and X<x):
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())
c = list(map(int, input().split()))
for i in range(n - 1):
min1 = min(c[i], c[i + 1])
max1 = max(c[i], c[i + 1])
for j in range(i + 1, n - 1):
min2 = min(c[j], c[j + 1])
max2 = max(c[j], c[j + 1])
if (min2 > min1 and min2 < max1) and max2 > max1:
print('yes')
exit()
elif (max2 > min1 and max2 < max1) and min2 < min1:
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 main() {
int n, i, j;
int a[1000];
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
int x, y, f1 = 0, f2 = 0;
x = min(a[i], a[i + 1]);
y = max(a[i], a[i + 1]);
for (j = i + 2; j < n; j++) {
if (a[j] < x || a[j] > y) {
f1 = 1;
} else {
f2 = 1;
}
}
if (f1 == 1 && f2 == 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 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n)))
cin >> a[i];
if (n <= 3) {
cout << "no";
return 0;
}
bool inter = false;
for (__typeof(a.size() - 3) i = (0) - ((0) > (a.size() - 3));
i != (a.size() - 3) - ((0) > (a.size() - 3));
i += 1 - 2 * ((0) > (a.size() - 3))) {
for (__typeof(a.size() - 1) j = (i + 2) - ((i + 2) > (a.size() - 1));
j != (a.size() - 1) - ((i + 2) > (a.size() - 1));
j += 1 - 2 * ((i + 2) > (a.size() - 1))) {
if (min(a[j], a[j + 1]) > min(a[i], a[i + 1]) &&
min(a[j], a[j + 1]) < max(a[i], a[i + 1]) &&
max(a[j], a[j + 1]) > max(a[i], a[i + 1])) {
inter = true;
break;
} else if (min(a[j], a[j + 1]) < min(a[i], a[i + 1]) &&
max(a[j], a[j + 1]) < max(a[i], a[i + 1]) &&
max(a[j], a[j + 1]) > min(a[i], a[i + 1])) {
inter = true;
break;
}
}
}
cout << (inter ? "yes" : "no");
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int arr[1001];
int n;
bool isi(int x1, int x2, int y1, int y2) {
if (x2 <= y1) return 0;
if (y2 <= x2) return 0;
if (x2 <= y2 && x1 == y1) return 0;
return 1;
}
void get(int x1, int y1, int x2, int y2, int &x, int &y) {
x = -1;
y = -1;
if (x1 >= x2 && y1 <= y2) {
x = x1;
y = y1;
} else if (x2 >= x1 && y2 <= y1) {
x = x2;
y = y2;
} else if (x1 < x2 && x2 <= y1) {
x = x2;
y = min(y1, y2);
} else if (x2 < x1 && x1 <= y2) {
x = x1;
y = min(y1, y2);
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
bool b = 0;
for (int i = 0; i < n - 2; i++) {
if (b) break;
for (int j = i + 1; j < n - 1; j++) {
int x1, x2, y1, y2;
x1 = arr[i];
x2 = arr[i + 1];
y1 = arr[j];
y2 = arr[j + 1];
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
if (x1 <= y1)
b = isi(x1, x2, y1, y2);
else
b = isi(y1, y2, x1, x2);
if (b) break;
}
if (b) break;
}
if (b)
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 min(int a, int b) {
if (a < b)
return (a);
else
return (b);
}
int main() {
int n, arr[1005], flag = 0, a, b, i, j, c, d;
cin >> n;
for (i = 1; i <= n; i++) cin >> arr[i];
if (n == 1 || n == 2 || n == 3)
flag = 1;
else {
for (i = 3; i <= n - 1; i++) {
flag = 0;
c = min(arr[i], arr[i + 1]);
if (c == arr[i])
d = arr[i + 1];
else
d = arr[i];
for (j = 1; j + 2 <= i; j++) {
a = min(arr[j], arr[j + 1]);
if (a == arr[j])
b = arr[j + 1];
else
b = arr[j];
if (arr[i] < a && arr[i + 1] < a)
flag = 1;
else if (arr[i] > b && arr[i + 1] > b)
flag = 1;
else if (a <= arr[i] && b >= arr[i] && a <= arr[i + 1] &&
b >= arr[i + 1])
flag = 1;
else if (c <= a && d >= b)
flag = 1;
else {
flag = 0;
break;
}
}
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int len;
cin >> len;
vector<pair<int, int>> join;
int arr[len];
for (int i = 0; i < len; i++) cin >> arr[i];
for (int i = 1; i < len; i++) {
int mi = min(arr[i - 1], arr[i]);
int ma = max(arr[i - 1], arr[i]);
join.push_back(make_pair(mi, ma));
}
int flag = 0;
for (auto i : join) {
for (auto j : join) {
if (j.first < i.first && j.second > i.first && j.second < i.second) {
cout << "yes" << endl;
flag++;
break;
} else if (j.first < i.second && j.second > i.second &&
j.first > i.first) {
cout << "yes" << endl;
flag++;
break;
}
}
if (flag) break;
}
if (!flag) 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.BufferedInputStream;
import java.util.Scanner;
/**
* Created by jizhe on 2015/12/18.
*/
public class DimaAndContinuousLine {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
int N = in.nextInt();
int[] left = new int[N-1];
int[] right = new int[N-1];
int count = 0;
int prev = in.nextInt();
int current;
for( int i = 1; i < N; i++ )
{
current = in.nextInt();
int l = Math.min(prev, current);
int r = Math.max(prev, current);
for( int j = 0; j < i-1; j++ )
{
if( l > left[j] && l < right[j] )
{
if( r > right[j] )
{
System.out.printf("yes\n");
//System.out.printf("l: %d, r: %d, l in range(%d, %d)\n", l, r, left[j], right[j]);
return;
}
}
if( r > left[j] && r < right[j] )
{
if( l < left[j] )
{
System.out.printf("yes\n");
//System.out.printf("l: %d, r: %d, r in range(%d, %d)\n", l, r, left[j], right[j]);
return;
}
}
}
left[i-1] = l;
right[i-1] = r;
prev = current;
}
/*System.out.printf("segments:\n");
for( int i = 0; i < N-1; i++ )
{
System.out.printf("range(%d, %d)\n", left[i], right[i]);
}*/
System.out.printf("no\n");
}
}
| 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.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str, " ");
if (n > 2) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < n - 1; i++) {
int left = Math.min(arr[i], arr[i + 1]);
int right = Math.max(arr[i], arr[i + 1]);
semiCircle s1 = new semiCircle(left, right);
for (int j = i + 1; j < n - 1; j++) {
left = Math.min(arr[j], arr[j + 1]);
right = Math.max(arr[j], arr[j + 1]);
semiCircle s2 = new semiCircle(left, right);
if (s1.left < s2.left && s2.left < s1.right && s1.right < s2.right) {
bw.write("yes\n");
bw.flush();
return;
} else if (s2.left < s1.left && s1.left < s2.right && s2.right < s1.right) {
bw.write("yes\n");
bw.flush();
return;
}
}
}
}
bw.write("no\n");
bw.flush();
}
public static class semiCircle {
int left;
int right;
public semiCircle(int l, int r) {
this.left = l;
this.right = r;
}
}
}
| 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;
string ans = "no";
cin >> n;
vector<int> lineas(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
lineas[i] = a;
}
int minimo;
int maximo;
for (int i = 0; i < n - 1; i++) {
minimo = min(lineas[i], lineas[i + 1]);
maximo = max(lineas[i], lineas[i + 1]);
for (int j = 0; j < n - 1; j++) {
if (j != i) {
int anterior = min(lineas[j], lineas[j + 1]);
int siguiente = max(lineas[j], lineas[j + 1]);
if (siguiente < maximo and minimo < siguiente and anterior < minimo) {
ans = "yes";
}
if (minimo < anterior and maximo < siguiente and anterior < maximo) {
ans = "yes";
}
}
}
}
cout << ans << 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.*;
public class Dima_and_Continuous_Line
{
public static void main(String args[])
{
Scanner s1 = new Scanner(System.in);
int n = s1.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = s1.nextInt();
s1.close();
for(int i=1;i<n;i++)
for(int j=i+1;j<n;j++)
if(intersect(Math.min(a[i],a[i-1]),Math.max(a[i],a[i-1]),Math.min(a[j],a[j-1]),Math.max(a[j],a[j-1])))
{
System.out.println("yes");
return;
}
System.out.println("no");
}
static boolean intersect(int a,int b,int c,int d)
{
if(a < c && c < b && b < d)
return true;
if(c < a && a < d && d < b)
return true;
return false;
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import sys
import math
from sys import stdin, stdout
# Most Frequently Used Number Theory Concepts
# SOE(helper function of get gcd)
def sieve(N):
primeNumbers = [True]*(N+1)
primeNumbers[0] = False
primeNumbers[1] = False
i = 2
while i*i <= N:
j = i
if primeNumbers[j]:
while j*i <= N:
primeNumbers[j*i] = False
j += 1
i += 1
return primeNumbers
# get prime number form 1 to N
def getPrime(N):
primes = sieve(N)
result = []
for i in range(len(primes)):
if primes[i]:
result.append(i)
return result
# factors of n
def factor(N):
factors = []
i = 1
while i*i <= N:
if N % i == 0:
factors.append(i)
if i != N//i:
factors.append(N//i)
i += 1
return sorted(factors)
# reduce a string in number under range(1000000007)
def reduceB(b) :
# Initialize result
mod = 0
# Calculating mod of b with a
# to make b like 0 <= b < a
for i in range(0, len(b)) :
mod = (mod * 10 + ord(b[i])) % 1000000007
# print(b[i], ord(b[i]), mod)
return mod # return modulo
# gcd of two numbers
def gcd(a, b):
if a < b:
return gcd(b, a)
if b == 0:
return a
return gcd(b, a % b)
# TAKE INPUT
def get_ints_in_variables():
return map(int, sys.stdin.readline().strip().split())
def get_int(): return int(input())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
def main():
# Write Your Code Here
# Then this semis-circles intersect if one of the conditions is true
# x1β<βx3β<βx2β<βx4
# x3β<βx1β<βx4β<βx2
n = int(input())
a = get_ints_in_list()
for i in range(n - 1):
l1, r1 = min(a[i], a[i + 1]), max(a[i], a[i + 1])
for j in range(n - 1):
l2, r2 = min(a[j], a[j + 1]), max(a[j], a[j + 1])
if l1 < l2 < r1 < r2:
print ('yes')
return
print ('no')
# for printing format
# print("Case #{}: {}".format(t+1, ans))
# calling main Function
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())
x = list(map(int,input().split()))
f=0
for i in range(n-1):
a=x[i] if x[i]<x[i+1] else x[i+1]
b=x[i+1] if a==x[i] else x[i]
for j in range(i+2,n-1):
c= min(x[j],x[j+1])
d = max(x[j],x[j+1])
if a<c and c<b and b<d:
f=1
break
elif c<a and a<d and d<b:
f=1
break
if f:
break
if f :
print("yes")
else:
print("no")
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = input()
v = list(map(int, raw_input().split()))
w = [(min(v[x], v[x+1]), max(v[x], v[x+1])) for x in range(0, n-1)]
for i in range(0, len(w)):
for j in range(0, len(w)):
if i == j: continue
a, b = w[i][0], w[i][1]
c, d = w[j][0], w[j][1]
if a < c and c < b and (d < a or d > b):
print 'yes'
exit()
print 'no'
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 |
import java.util.*;
import java.lang.*;
import java.io.*;
public class DimaandContinuousLine {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t =1;// sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
pair[] p=new pair[n-1];
for(int i=0;i<n-1;i++){
int min=Math.min(arr[i],arr[i+1]);
int max=Math.max(arr[i],arr[i+1]);
p[i]=new pair(min,max);
}
boolean pos=false;
for(int i=0;i<n-1;i++){
int mst=p[i].st;
int mend=p[i].end;
for(int j=0;j<n-1;j++){
if(j!=i){
int tst=p[j].st;
int tend=p[j].end;
if(tst>mst && tst<mend && tend>mend){
pos=true;
break;
}
else if(tst<mst && tend>mst && tend <mend){
pos=true;
break;
}
}
}
if(pos){
break;
}
}
if(pos){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
} catch (Exception e) {
return;
}
}
public static class pair{
int st;
int end;
pair(int st,int end){
this.st=st;
this.end=end;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
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());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ 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 | def main():
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]) and min(a[j],a[j+1])<max(a[i],a[i+1]) and max(a[i],a[i+1])<max(a[j],a[j+1]):
print("yes")
return
print("no")
return
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 | import java.util.Scanner;
//import java.math.*;
public class A {
static Seg s[];
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int p[] = new int[n];
s = new Seg[n];
for(int i = 0; i < n; i++) p[i] = sc.nextInt();
for(int i = 0; i < n-1; i++) {
Seg tmp = new Seg(p[i], p[i+1]);
s[i] = tmp;
}
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n-1; j++) {
if(interset(i, j)) {
System.out.print("yes");
return ;
}
}
}
System.out.println("no");
sc.close();
}
static boolean interset(int x, int y){
if( ( (s[x].l > s[y].l && s[x].l < s[y].r) || (s[x].r > s[y].l && s[x].r < s[y].r) ) &&
(s[x].l < s[y].l || s[x].l > s[y].r || s[x].r < s[y].l || s[x].r > s[y].r)) return true;
else return false;
}
}
class Seg{
public Seg(int a, int b) { l = Math.min(a, b); r = Math.max(a, b); }
int l, r;
} | 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;
long x, y, num[2009];
bool flag = true;
scanf("%d%ld", &n, &num[1]);
for (int i = 2; i <= n; i++) {
scanf("%ld", &num[i]);
}
num[0] = num[1];
num[n + 1] = num[n];
for (int i = 1; i <= n; i++) {
x = min(num[i - 1], num[i]);
y = max(num[i - 1], num[i]);
for (int j = 1; j <= n; j++) {
if (num[j] > x && num[j] < y) {
if (num[j - 1] < x || num[j - 1] > y || num[j + 1] < x ||
num[j + 1] > y) {
flag = false;
break;
}
}
}
if (!flag) break;
}
if (flag)
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())
l=list(map(int,input().split()))
maxx=l[0]
minn=l[0]
for i in range(0,n-1):
if l[i+1]>l[i]:
if l[i]>minn and l[i]<maxx and l[i+1]>maxx:
print("yes")
exit()
elif l[i]<minn and l[i+1]>minn and l[i+1]<maxx :
print("yes")
exit()
else:
if l[i]>maxx:
maxx=l[i]
if l[i]<minn:
minn=l[i]
elif l[i+1]<l[i]:
if l[i+1]>minn and l[i+1]<maxx and l[i]>maxx:
print("yes")
exit()
elif l[i+1]<minn and l[i]>minn and l[i]<maxx:
print("yes")
exit()
else:
if l[i]>maxx:
maxx=l[i]
if l[i]<minn:
minn=l[i]
print("no")
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.*;
import java.util.*;
public class Solution{
public static void main(String []args){
Solution ob=new Solution();
ob.go();
}
void go() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if (n<=3) {pry();return;}
int dir=1,max,min=sc.nextInt();
int t=sc.nextInt(),prev=min;
if (t<min) {
max=min;
min=t;
dir=-1;
} else max=t;
boolean state=true;
for(int i=3;i<=n;i++) {
// pr(i+"");
t=sc.nextInt();
if (state) {
if (dir==1) {
if (t>max) {prev=max;max=t;}
else if (t<min) {
dir=-1;
prev=min;
min=t;
} else if (t>prev) {
min=prev;
state=false;
prev=t;
} else {
prn();
return;
}
} else {
if (t>max) {prev=max;max=t;dir=1;}
else if (t<min) {
dir=-1;
prev=min;
min=t;
} else if (t<prev) {
max=prev;
state=false;
prev=t;
}
else {prn();return;}
}
} else {
if (t>max || t<min) {prn();return;}
if (t>prev) min=prev;
else max=prev;
prev=t;
}
}
pry();
}
void pry() {
System.out.println("no");
}
void prn() {
System.out.println("yes");
}
void pr(String s) {
System.out.println(s);
}
} | 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;
using namespace chrono;
int SetBit(int n, int x) { return n | (1 << x); }
int ClearBit(int n, int x) { return n & ~(1 << x); }
int ToggleBit(int n, int x) { return n ^ (1 << x); }
bool CheckBit(int n, int x) { return (bool)(n & (1 << x)); }
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx4[] = {1, 0, -1, 0};
int dy4[] = {0, 1, 0, -1};
bool sortbysecdesc(const pair<int, int> a, const pair<int, int> b) {
return a.second >= b.second;
}
int __gcd(int a, int b) {
if (a + b == 0) return a + b;
if (a % b == 0) return b;
return __gcd(b, a % b);
}
long long fact(int n) {
long long res = 1;
for (int i = 2; i <= n; i++) res = res * i;
return res;
}
long long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); }
void solve() {
int n;
cin >> n;
vector<pair<int, int> > v(n - 1);
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
if (n == 1) {
cout << "no"
<< "\n";
;
return;
}
for (int i = 0; i < n - 1; i++) {
v[i] = {min(arr[i], arr[i + 1]), max(arr[i], arr[i + 1])};
}
sort((v).begin(), (v).end());
for (int i = 0; i < v.size() - 1; i++) {
for (int j = i + 1; j < v.size(); j++) {
if (v[i].second > v[j].first and v[i].second < v[j].second and
v[i].first != v[j].first) {
cout << "yes"
<< "\n";
;
return;
}
}
}
cout << "no"
<< "\n";
;
return;
}
int32_t 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 | #include <bits/stdc++.h>
using namespace std;
int n, a[1111], x, y, x2, y2;
bool inter(int a, int b, int x, int y) {
if (a > b) swap(a, b);
if (x > y) swap(x, y);
if (a < x && b > x && b < y) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
x = a[i];
y = a[i + 1];
for (int j = i + 1; j < n - 1; j++) {
x2 = a[j];
y2 = a[j + 1];
if (inter(x, y, x2, y2)) {
cout << "yes";
return 0;
}
if (inter(x2, y2, x, y)) {
cout << "yes";
return 0;
}
}
}
cout << "no";
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, x[1000];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> x[i];
for (int j = 1; j < i - 1; ++j) {
bool l = (x[i] > min(x[j], x[j - 1]) && x[i] < max(x[j - 1], x[j]));
bool r =
(x[i - 1] > min(x[j], x[j - 1]) && x[i - 1] < max(x[j - 1], x[j]));
if (l != r) {
cout << "yes";
return 0;
}
}
}
cout << "no";
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = input()
x = map(int,raw_input().split())
for i in xrange(n-1):
a,b = sorted((x[i],x[i+1]))
for j in xrange(n-1):
c,d = sorted((x[j],x[j+1]))
if a < c < b < d:
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(raw_input())
r = [int(c) for c in raw_input().split()]
r = [list(sorted(r[i:i+2])) for i in xrange(n-1)]
print ['no','yes'][any(a<c<b<d for a,b in r for c,d in r)] | PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int N = sc.ni();
int M = 2000001;
int[] vals = new int[N];
for (int i = 0; i < N; i++)
vals[i] = sc.ni()+1000000;
int[][] nums = new int[N-1][2];
for (int i = 0; i < N-1; i++) {
nums[i][0] = vals[i];
nums[i][1] = vals[i+1];
Arrays.sort(nums[i]);
}
nums = sort(nums);
BinaryIndexedTree bit = new BinaryIndexedTree(M);
for (int[] num: nums) {
if (num[1] > num[0]+1) {
int s = bit.sum(num[0]+1, num[1]);
if (s > 0) {
pw.println("yes");
pw.close();
return;
}
}
bit.add(1, num[1]);
}
pw.println("no");
pw.close();
}
static class BinaryIndexedTree {
public int[] arr;
public BinaryIndexedTree (int N) {
arr = new int[N+1];
}
//add k to the i-th element.
public void add(int k, int i) {
int node = i+1;
while (node < arr.length) {
arr[node] += k;
node += node & (-node);
}
}
//sum up the elements from input[s_i] to input[e_i], from [s_i,e_i).
public int sum(int s_i, int e_i) {
return sum(e_i) - sum(s_i);
}
public int sum(int i) {
int total = 0;
int node = i;
while (node > 0) {
total += arr[node];
node -= node & (-node);
}
return total;
}
}
public static int[][] sort(int[][] arr) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int randomPosition = rgen.nextInt(arr.length);
int[] temp = arr[i];
arr[i] = arr[randomPosition];
arr[randomPosition] = temp;
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] arr1, int[] arr2) {
if (arr1[0] != arr2[0])
return arr1[0]-arr2[0];
else
return arr2[1]-arr1[1];
//Ascending order.
}
});
return arr;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
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 |
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[]args)throws IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bf.readLine());
String s=bf.readLine();
String[]sa=s.split(" ");
int[]a=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(sa[i]);
}
if(n<4)
System.out.println("no");
else
{
boolean flag=true;
for(int i=2;i<n-1 && flag;i++){
for(int j=0;j<i-1 && flag;j++){
int min=Math.min(a[j], a[j+1]);
int max=Math.max(a[j], a[j+1]);
if(((a[i]>max ||a[i]<min) && (a[i+1]>min && a[i+1]<max ))||((a[i+1]>max ||a[i+1]<min) && (a[i]>min && a[i]<max )))
flag=false;
}
}
System.out.println((flag)? "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 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 | #include <bits/stdc++.h>
using namespace std;
int n, m, a, b, s1, s2, s;
void faster() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
faster();
int n, a = 0, b = 0, y = 0, x = 0;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = 0; i < n - 1; ++i) {
a = max(arr[i], arr[i + 1]);
b = min(arr[i], arr[i + 1]);
for (int j = 0; j < n - 1; ++j) {
x = max(arr[j], arr[j + 1]);
y = min(arr[j], arr[j + 1]);
if (b > y && b < x && a > x) {
cout << "yes";
return 0;
}
}
}
cout << "no";
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n=input()
a=map(int,raw_input().split())
b=[]
for i in range(n-1):
b.append([a[i],a[i+1]])
b[i].sort()
for i in range(n-1):
for j in range(i+1,n-1):
if (b[j][1]>b[i][1] and b[j][0]>b[i][0] and b[j][0]<b[i][1]) or (b[i][1]>b[j][1] and b[i][0]>b[j][0] and b[i][0]<b[j][1]):
# print b[i],b[j]
print "yes"
exit()
print "no" | PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct pr {
int a;
int b;
};
int main() {
int n, i, j, cnt = 0, flag = 0, a, b;
vector<pr> vec;
vector<int> point;
cin >> n;
for (i = 0; i < n; i++) {
cin >> j;
point.push_back(j);
}
for (i = 0; i < n - 1; i++) {
if (point[i] > point[i + 1]) {
a = point[i + 1];
b = point[i];
} else {
a = point[i];
b = point[i + 1];
}
pr p;
p.a = a;
p.b = b;
vec.push_back(p);
}
int m = vec.size();
for (i = 1; i < m; i++) {
for (j = 0; j < i; j++) {
if ((vec[j].b > vec[i].a && vec[j].a < vec[i].a && vec[i].b > vec[j].b) ||
(vec[i].a < vec[j].a && vec[i].b > vec[j].a && vec[i].b < vec[j].b)) {
flag = 1;
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>
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 + 2; 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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int a[1005];
long n, i, x = 0, y = 0, j;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n; i++) {
x = y = 0;
for (j = i + 2; j < n; j++)
if ((a[j] > a[i] && a[j] > a[i + 1]) || (a[j] < a[i] && a[j] < a[i + 1]))
x++;
else
y++;
if (x != 0 && y != 0) {
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 x[1111];
int judge(int i, int j) {
int a = x[i], b = x[i + 1], c = x[j], d = x[j + 1];
if (a > b) swap(a, b);
if (c > d) swap(c, d);
if (a < c && b < d && b > c) return 1;
if (c < a && d < b && d > a) return 1;
return 0;
}
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &x[i]);
}
for (i = 1; i < n; i++)
for (j = i + 1; j < n; j++)
if (judge(i, j)) {
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 | def self_intersect(points:list):
for i in range(len(points)):
check_point = points[i]
for j in range(len(points)):
if i != j:
check_point2 = points[j]
if check_point[0] < check_point2[0] < check_point[1] < check_point2[1]:
return True
if check_point2[0] < check_point[0] < check_point2[1] < check_point[1]:
return True
return False
n = int(input())
points = list(map(int,input().split(' ')))
semi_circles = []
for i in range(0,len(points)-1):
if points[i] < points[i+1]:
semi_circles.append([points[i],points[i+1]])
else:
semi_circles.append([points[i+1],points[i]])
if self_intersect(semi_circles):
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.*;
public class Main {
public static void main(String [] args)throws IOException
{
FastReader sc = new FastReader();
int n = sc.nextInt();
int [] arr = new int[n];
for (int i =0;i<n;i++) {
arr[i] = sc.nextInt();
}
boolean flag = true;
for (int i =0;i<n-1;i++) {
for (int j=0;j<n-1;j++) {
int start = Math.min(arr[i], arr[i+1]);
int end = Math.max(arr[i], arr[i+1]);
int s = Math.min(arr[j], arr[j+1]);
int e = Math.max(arr[j], arr[j+1]);
//System.out.println(start+" "+end+" "+s+" "+e);
if (s<start && e>start && e<end) {
flag= false;
break;
}else if (start<s && s<end && e>end) {
flag=false;
break;
}
}
}
if (flag)
System.out.println("no");
else
System.out.println("yes");
}
}
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;
}
}
| 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()
a=map(int,raw_input().split())
for i in range(n-1):
z,b=min(a[i],a[i+1]),max(a[i],a[i+1])
for j in range(n-1):
c,d=min(a[j],a[j+1]),max(a[j],a[j+1])
if z < 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 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int point;
vector<pair<int, int> > segment(n);
for (int i = 0; i < n; i++) {
cin >> point;
if (i == 0)
segment[i] = make_pair(point, point);
else
segment[i] = make_pair(segment[i - 1].second, point);
}
int x1, x2, x3, x4;
bool intersects = false;
for (int i = 0; i < n; i++) {
x1 = min(segment[i].first, segment[i].second);
x2 = max(segment[i].first, segment[i].second);
for (int j = 0; j < n; j++) {
if (i == j) continue;
x3 = min(segment[j].first, segment[j].second);
x4 = max(segment[j].first, segment[j].second);
if (x3 < x2 && x3 > x1 && x4 > x2) intersects = true;
if (x1 < x4 && x1 > x3 && x2 > x4) intersects = true;
}
}
if (intersects)
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class DimaAndContinuousLine {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String input = bf.readLine();
String input2 = bf.readLine();
String[] inputArrayTemp = input2.split(" ");
String finalString = input2 + " " + inputArrayTemp[inputArrayTemp.length - 1]
+ " " + inputArrayTemp[inputArrayTemp.length - 1]
+ " " + inputArrayTemp[inputArrayTemp.length - 1];
String[] inputArray = finalString.split(" ");
int i = 0;
boolean retorno = false;
int n = Integer.parseInt(input) + 3;
while (i < n-3 && !retorno) {
int j = 0;
while (j < n - 3) {
int index = j;
//System.out.println("Index Val=" + index);
//System.out.println("Index + 3=" + inputArray[index + 3]);
//System.out.println("Index + 2=" + inputArray[index + 2]);
//System.out.println("i Val=" + i);
//System.out.println("i + 1=" + inputArray[i + 1]);
//System.out.println("i=" + inputArray[i]);
if (
// CASO 1
(
Integer.parseInt(inputArray[index]) < Integer.parseInt(inputArray[i + 1])
&& Integer.parseInt(inputArray[index]) > Integer.parseInt(inputArray[i])
&& Integer.parseInt(inputArray[index + 1]) > Integer.parseInt(inputArray[i + 1])
)
||
//NUEVO CASO 1
(
Integer.parseInt(inputArray[index]) < Integer.parseInt(inputArray[i])
&& Integer.parseInt(inputArray[index]) > Integer.parseInt(inputArray[i + 1])
&& Integer.parseInt(inputArray[index + 1]) > Integer.parseInt(inputArray[i])
)
||
// CASO 2
(
Integer.parseInt(inputArray[index + 1]) < Integer.parseInt(inputArray[i + 1])
&& Integer.parseInt(inputArray[index + 1]) > Integer.parseInt(inputArray[i])
&& Integer.parseInt(inputArray[index]) > Integer.parseInt(inputArray[i + 1])
)
||
// NUEVO CASO 2
(
Integer.parseInt(inputArray[index + 1]) < Integer.parseInt(inputArray[i])
&& Integer.parseInt(inputArray[index + 1]) > Integer.parseInt(inputArray[i + 1])
&& Integer.parseInt(inputArray[index]) > Integer.parseInt(inputArray[i])
)
) {
System.out.println("yes");
retorno = true;
break;
}
j++;
}
i = i + 1;
}
if (!retorno)
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.lang.*;
public class Solution{
public static void main(String[] args) {
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();
for(int i = 0 ; i < n - 2 ; i++){
int x1 = Math.min(arr[i], arr[i+1]);
int x2 = Math.max(arr[i], arr[i+1]);
int c = i + 2;
while(c < n - 1){
int c1 = Math.min(arr[c], arr[c+1]);
int c2 = Math.max(arr[c], arr[c+1]);
if((x1 < c1 && c1 < x2) &&(x2 > c1 && x2 < c2))
{
System.out.println("yes");
return;
}
if((c1 < x1 && c2 > x1) && (c2 < x2 && c2 > x1))
{
System.out.println("yes");
return;
}
c++;
}
}
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=input()
a=list(map(int,raw_input().split()))
if n<=2:
print "no"
else:
lst=[]
for i in range(n-1):
x=max(a[i],a[i+1])
y=min(a[i],a[i+1])
lst.append((y,x))
l=len(lst)
flag=0
for i in range(l):
for j in range(i+1,l):
if lst[i][0]<lst[j][0]<lst[i][1] and lst[j][1]>lst[i][1]:
flag=1
if lst[i][0]<lst[j][1]<lst[i][1] and lst[j][0]<lst[i][0]:
flag=1
if flag:print "yes"
else:print "no"
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
int main() {
int n, *a, i, flag, r1, r2, in, k = 1;
scanf("%d", &n);
a = new int[n];
for (i = 0; i < n; i++) scanf("%d", &a[i]);
if (n <= 3)
printf("no");
else {
if (a[0] < a[1]) {
r1 = a[0];
r2 = a[1];
} else {
r1 = a[1];
r2 = a[0];
}
flag = a[2] > r2 ? 1 : -1;
in = (a[2] < r2 && a[2] > r1) ? 1 : 0;
for (i = 3; i < n; i++) {
if (in) {
if (a[i] < r2 && a[i] > r1) {
if (flag == 1) {
if (a[i] < a[i - 1]) {
flag = -1;
r2 = a[i - 1];
} else {
flag = 1;
r1 = a[i - 1];
}
} else {
if (a[i] > a[i - 1]) {
r1 = a[i - 1];
flag = 1;
} else {
r2 = a[i - 1];
flag = -1;
}
}
} else {
printf("yes");
k = 0;
break;
}
} else {
if (a[i] < r2 && a[i] > r1) {
printf("yes");
k = 0;
break;
} else {
if (flag == 1) {
if (a[i] < a[i - 1] && a[i] > r2) {
in = 1;
r1 = r2;
r2 = a[i - 1];
flag = -1;
} else if (a[i] < a[i - 1]) {
r2 = a[i - 1];
flag = -1;
} else {
r2 = a[i - 1];
flag = 1;
}
} else {
if (a[i] > a[i - 1] && a[i] < r1) {
in = 1;
r1 = r2;
r2 = a[i - 1];
flag = 1;
} else if (a[i] > a[i - 1]) {
r1 = a[i - 1];
flag = 1;
} else {
r1 = a[i - 1];
flag = -1;
}
}
}
}
}
if (k) printf("no");
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.