ural 1246. Tethered Dog
1246. Tethered Dog
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
A dog is tethered to a pole with a rope. The pole is located inside a fenced polygon (not necessarily convex) with nonzero area. The fence has no self-crosses. The Olympian runs along the fence bypassing the vertices of the polygon in a certain order which is not broken during the jog. A dog pursues him inside the fenced territory and barks. Your program is to determine how (clockwise or counter-clockwise) the rope will wind after several rounds of the Olympian's jog.
Input
The first input line contains a number N that is the number of the polygon vertices. It’s known that 3 ≤ N ≤ 200000. The next N lines consist of the vertices plane coordinates, given in an order of Olympian’s dog. The coordinates are a pair of integers separated with a space. The absolute value of each coordinate doesn’t exceed 50000.
Output
You are to output "cw", if the rope is winded in a clockwise order and "ccw" otherwise.
Sample
input | output |
---|---|
4 0 0 0 1 1 1 1 0 |
cw |
Problem Author: Evgeny Kobzev
Problem Source: Ural State University Personal Programming Contest, March 1, 2003
Problem Source: Ural State University Personal Programming Contest, March 1, 2003
Tags: geometry
Difficulty: 364
题意:问一个多边形的给出顺序是不是顺时针。
分析:叉积判断方向。用最左或最优的点作为基准点。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 #include <iomanip> 14 using namespace std; 15 typedef long long LL; 16 typedef double DB; 17 #define For(i, s, t) for(int i = (s); i <= (t); i++) 18 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 19 #define Rep(i, t) for(int i = (0); i < (t); i++) 20 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 21 #define rep(i, x, t) for(int i = (x); i < (t); i++) 22 #define MIT (2147483647) 23 #define INF (1000000001) 24 #define MLL (1000000000000000001LL) 25 #define sz(x) ((int) (x).size()) 26 #define clr(x, y) memset(x, y, sizeof(x)) 27 #define puf push_front 28 #define pub push_back 29 #define pof pop_front 30 #define pob pop_back 31 #define ft first 32 #define sd second 33 #define mk make_pair 34 inline void SetIO(string Name) 35 { 36 string Input = Name+".in", 37 Output = Name+".out"; 38 freopen(Input.c_str(), "r", stdin), 39 freopen(Output.c_str(), "w", stdout); 40 } 41 42 inline int Getint() 43 { 44 int Ret = 0; 45 char Ch = ' '; 46 bool Flag = 0; 47 while(!(Ch >= '0' && Ch <= '9')) 48 { 49 if(Ch == '-') Flag ^= 1; 50 Ch = getchar(); 51 } 52 while(Ch >= '0' && Ch <= '9') 53 { 54 Ret = Ret * 10 + Ch - '0'; 55 Ch = getchar(); 56 } 57 return Flag ? -Ret : Ret; 58 } 59 60 const int N = 200010; 61 struct Point 62 { 63 DB x, y; 64 inline void Read() 65 { 66 scanf("%lf%lf", &x, &y); 67 } 68 } Arr[N]; 69 int n; 70 71 inline void Input() 72 { 73 scanf("%d", &n); 74 For(i, 1, n) Arr[i].Read(); 75 } 76 77 inline DB Det(const Point &A, const Point &O, const Point &B) 78 { 79 DB X1 = A.x - O.x, X2 = B.x - O.x; 80 DB Y1 = A.y - O.y, Y2 = B.y - O.y; 81 return X1 * Y2 - X2 * Y1; 82 } 83 84 inline void Solve() 85 { 86 int p = 1; 87 For(i, 2, n) 88 if(Arr[i].x > Arr[p].x) p = i; 89 Arr[0] = Arr[n], Arr[n + 1] = Arr[1]; 90 if(Det(Arr[p - 1], Arr[p], Arr[p + 1]) >= 0.0) puts("cw"); 91 else puts("ccw"); 92 } 93 94 int main() 95 { 96 #ifndef ONLINE_JUDGE 97 SetIO("E"); 98 #endif 99 Input(); 100 Solve(); 101 return 0; 102 }