andmej / acm

My solutions for problems from the UVa Online Judge (Valladolid).

This URL has Read+Write access

acm / 10078 - The Art Gallery / p10078.cpp
100644 87 lines (76 sloc) 1.88 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
struct Point{
  int x, y;
  bool operator < (const Point &p) const {
    return (x < p.x || (x == p.x && y < p.y));
  }
};
 
// 2D cross product.
// Return a positive value, if OAB makes a counter-clockwise turn,
// negative for clockwise turn, and zero if the points are collinear.
int cross(const Point &O, const Point &A, const Point &B)
{
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
 
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
vector<Point> convexHull(vector<Point> P)
{
int n = P.size(), k = 0;
vector<Point> H(2*n);
 
// Sort Points lexicographically
sort(P.begin(), P.end());
 
// Build lower hull
for (int i = 0; i < n; i++) {
while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
H[k++] = P[i];
}
 
// Build upper hull
for (int i = n-2, t = k+1; i >= 0; i--) {
while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
H[k++] = P[i];
}
 
H.resize(k);
return H;
}
 
void printPnt(const Point &p){
  cout << "(" << p.x << "," << p.y << ")";
}
 
int main(){
  int n;
  cin >> n;
  while (n){
    //procesar caso
    vector<Point> g(n);
    for (int i=0; i<n; ++i){
      int x, y;
      cin >> x >> y;
      g[i].x = x;
      g[i].y = y;
    }
    /*cout << "g.size() es: " << g.size() << endl;
for (int i=0; i<n; ++i){
printPnt(g[i]);
cout << " ";
}
cout << endl; */
    vector<Point> chull = convexHull(g);
    /*cout << "chull.size() es: " << chull.size() << endl;
for (int i=0; i<chull.size(); ++i){
printPnt(chull[i]);
cout << " ";
}
cout << endl;*/
    chull.pop_back();
    if (chull.size() == n){
      cout << "No\n";
    }else{
      cout << "Yes\n";
    }
    cin >> n;
  }
  return 0;
}