Algorithm


A. Dima and Continuous Line
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.

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

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

Input

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

Output

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

Examples
input
Copy
4
0 10 5 15
output
Copy
yes
input
Copy
4
0 15 5 10
output
Copy
no
Note

The first test from the statement is on the picture to the left, the second test is on the picture to the right.



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <stdio.h>
#include <algorithm>

using namespace std;

int main() {
	int a[3001], n;
	scanf("%d", &n);

	for(int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}

	if(n <= 2) {
		puts("no");
		return 0;
	}

	for(int i = 0; i < n - 1; ++i) {
		for(int j = 0; j < n - 1; ++j) {
			if((min(a[i], a[i+1]) < min(a[j], a[j+1]) && min(a[j], a[j+1]) < max(a[i], a[i+1]) && max(a[i], a[i+1]) < max(a[j], a[j+1])) ||
			   (min(a[j], a[j+1]) < min(a[i], a[i+1]) && min(a[i], a[i+1]) < max(a[j], a[j+1]) && max(a[j], a[j+1]) < max(a[i], a[i+1]))) {
				puts("yes");
				return 0;
			}
		}
	}

	puts("no");

	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
0 10 5 15

Output

x
+
cmd
yes
Advertisements

Demonstration


Codeforces Solution-Dima and Continuous Line-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+