Codeforce Round #208 Div.2 A
A. Dima and Continuous Line
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture 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.
The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
4
0 10 5 15
yes
4
0 15 5 10
no
The first test from the statement is on the picture to the left, the second test is on the picture to the right.
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <iostream> 10 #include <algorithm> 11 using namespace std; 12 #define maxn 1005 13 #define mod 1000000007 14 #define ll long long 15 #define INF 0x7fffffff 16 int n, m; 17 int p[maxn]; 18 int main(){ 19 int cas = 1; 20 int t; 21 /*scanf("%d", &t); 22 while (t--){ 23 24 }*/ 25 while (~scanf("%d", &n)){ 26 for(int i=0;i<n;i++)scanf("%d", &p[i]); 27 int flag=1,a, b, c, d; 28 for (int i = 0; i < n-1; i++) 29 for (int j = 0; j<n - 1;j++){ 30 a = min(p[i], p[i + 1]); 31 b = max(p[i], p[i + 1]); 32 c = min(p[j], p[j + 1]); 33 d = max(p[j], p[j + 1]); 34 if (c>a&&c<b&&d>b || c<a&&d>a&&d < b){ 35 printf("yes\n"); flag = 0; i = n; j = n; 36 } 37 } 38 if(flag)printf("no\n"); 39 } 40 return 0; 41 }
浙公网安备 33010602011771号