Problem Link:
http://uva.onlinejudge.org/external/3/361.htmlDon't know whats going wrong, getting WA in this problem. I have tested inputs already posted on these forums, but those could not help me to find the bug. Here is my code:
- Code: Select all
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 256;
struct point { int x, y; };
struct region { bool ok; int np; point P[MAXN]; };
region K[2];
point P[MAXN], C[MAXN], P0;
inline int triArea2(const point &a, const point &b, const point &c) {
return (a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y));
}
inline int sqDist(const point &a, const point &b) {
return ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
inline bool comp(const point &a, const point &b) {
int d = triArea2(P0, a, b);
if(d < 0) return false;
if(!d && sqDist(P0, a) > sqDist(P0, b)) return false;
return true;
}
inline bool normal(const point &a, const point &b) {
return ((a.x==b.x) ? a.y < b.y : a.x < b.x);
}
inline bool issame(const point &a, const point &b) {
return (a.x == b.x && a.y == b.y);
}
inline void makeUnique(int &np) {
sort(&P[0], &P[np], normal);
np = unique(&P[0], &P[np], issame) - P;
}
void convexHull(int &np, int &nc) {
int i, j, pos = 0;
for(i = 1; i < np; i++)
if(P[i].y<P[pos].y || (P[i].y==P[pos].y && P[i].x<P[pos].x))
pos = i;
swap(P[0], P[pos]);
P0 = P[0];
sort(&P[1], &P[np], comp);
for(i = 0; i < 3; i++) C[i] = P[i];
for(i = j = 3; i < np; i++) {
while(triArea2(C[j-2], C[j-1], P[i]) < 0) j--;
C[j++] = P[i];
}
nc = j;
}
bool inside(point *p, int n, point &m) {
if(n == 1) return p[0].x == m.x && p[0].y == m.y;
if(n == 2) return (sqDist(p[0], m) + sqDist(m, p[1]) <= sqDist(p[0], p[1]));
int lt = n - 1, rt = 1, mid, dir, temp;
while(lt - rt > 1) {
mid = (lt + rt) >> 1;
dir = triArea2(p[0], p[mid], m);
if(dir < 0) lt = mid;
else if(dir > 0) rt = mid;
else return (sqDist(p[0], m) + sqDist(m, p[mid]) <= sqDist(p[0], p[mid]));
}
temp = abs(triArea2(p[0], p[rt], m)) + abs(triArea2(p[rt], p[lt], m)) + abs(triArea2(p[lt], p[0], m));
return (temp == abs(triArea2(p[0], p[rt], p[lt])));
}
int main() {
int n[2], c[2], i, kd, cit, cs = 1;
point m;
while(scanf("%d%d%d", &n[0], &n[1], &cit)==3 && (n[0] + n[1] + cit)) {
for(kd = 0; kd < 2; kd++) {
for(i = 0; i < n[kd]; i++) scanf("%d%d", &P[i].x, &P[i].y);
if(n[kd] < 3) { K[kd].ok = 0; continue; }
K[kd].ok = 1;
makeUnique(n[kd]);
if(n[kd] < 3) {
K[kd].np = n[kd];
for(i = 0; i < n[kd]; i++) K[kd].P[i] = P[i];
continue;
}
convexHull(n[kd], c[kd]);
K[kd].np = c[kd];
for(i = 0; i < c[kd]; i++) K[kd].P[i] = C[i];
}
printf("Data set %d:\n", cs++);
for(i = 0; i < cit; i++) {
scanf("%d%d", &m.x, &m.y);
if(K[0].ok && inside(K[0].P, K[0].np, m)) printf(" Citizen at (%d,%d) is safe.\n", m.x, m.y);
else if(K[1].ok && inside(K[1].P, K[1].np, m)) printf(" Citizen at (%d,%d) is robbed.\n", m.x, m.y);
else printf(" Citizen at (%d,%d) is neither.\n", m.x, m.y);
}
printf("\n");
}
return 0;
}
If I have < 3 cops or robbers, I will ignore them. Then I find their convex hulls, which might not be strictly convex. Then I determine where the citizen falls... I think this is correct.
Please help me, thanks in advance
