# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
946391 | rainboy | Tapestries (POI13_gob) | C11 | 2 ms | 604 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 1000
#define INF 1e12
#define eps 1e-12
int sgn(long long a) { return a == 0 ? 0 : (a > 0 ? 1 : -1); }
double min(double a, double b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
typedef long double ld;
long long cross2(int x1, int y1, int x2, int y2) {
return (long long) x1 * y2 - (long long) x2 * y1;
}
ld cross2_(ld x1, ld y1, ld x2, ld y2) {
return x1 * y2 - x2 * y1;
}
long long cross(int x0, int y0, int x1, int y1, int x2, int y2) {
return cross2(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}
ld cross_(ld x0, ld y0, ld x1, ld y1, ld x2, ld y2) {
return cross2_(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}
long long dot(int x1, int y1, int x2, int y2) {
return (long long) x1 * x2 + (long long) y1 * y2;
}
ld dot_(ld x1, ld y1, ld x2, ld y2) {
return x1 * x2 + y1 * y2;
}
int intersect_(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
return sgn(cross(x1, y1, x2, y2, x3, y3)) * sgn(cross(x1, y1, x2, y2, x4, y4)) < 0 && sgn(cross(x3, y3, x4, y4, x1, y1)) * sgn(cross(x3, y3, x4, y4, x2, y2)) < 0;
}
int xx[N], yy[N]; char tt[N + 1];
int aa[N], bb[N], cc[N];
int compare(int i, int j) {
int sgni, sgnj;
long long c;
sgni = aa[i] < 0 || aa[i] == 0 && bb[i] < 0 ? -1 : 1;
sgnj = aa[j] < 0 || aa[j] == 0 && bb[j] < 0 ? -1 : 1;
if (sgni != sgnj)
return sgni - sgnj;
c = cross2(aa[i], bb[i], aa[j], bb[j]);
return c == 0 ? 0 : (c < 0 ? -1 : 1);
}
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
void intersect(int i, int j, ld *x, ld *y) {
long long c;
c = cross2(aa[i], bb[i], aa[j], bb[j]);
*x = (ld) -cross2(cc[i], bb[i], cc[j], bb[j]) / c;
*y = (ld) -cross2(aa[i], cc[i], aa[j], cc[j]) / c;
}
int check(int i, int j, int k) {
long long cij, cjk;
ld x1, y1, x2, y2;
cij = cross2(aa[i], bb[i], aa[j], bb[j]);
cjk = cross2(aa[j], bb[j], aa[k], bb[k]);
if (cij == 0) {
if (dot(aa[i], bb[i], aa[j], bb[j]) > 0) {
if ((aa[i] != 0 ? cross2(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross2(cc[i], bb[i], cc[j], bb[j]) * sgn(bb[i])) > 0)
return 1;
} else {
if ((aa[i] != 0 ? cross2(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross2(cc[i], bb[i], cc[j], bb[j]) * sgn(bb[i])) < 0)
return -1;
}
}
if (cjk == 0) {
if (dot(aa[k], bb[k], aa[j], bb[j]) > 0) {
if ((aa[k] != 0 ? cross2(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross2(cc[k], bb[k], cc[j], bb[j]) * sgn(bb[k])) > 0)
return 1;
} else {
if ((aa[k] != 0 ? cross2(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross2(cc[k], bb[k], cc[j], bb[j]) * sgn(bb[k])) < 0)
return -1;
}
}
if (cij >= 0 || cjk >= 0)
return 0;
intersect(i, j, &x1, &y1), intersect(j, k, &x2, &y2);
if (cross2_(x2 - x1, y2 - y1, aa[j], bb[j]) <= 0)
return cross2(aa[k], bb[k], aa[i], bb[i]) > 0 ? 1 : -1;
return 0;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static int ii[N];
static char s[2];
int n, n_, head, cnt, h, i, i1, i2, k, tmp;
long long c;
ld t, t1, t2;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]);
for (i = 0; i < n; i++) {
scanf("%s", s);
tt[i] = s[0];
}
for (i = 0; i < n; i++) {
aa[i] = yy[i] - yy[(i + 1) % n];
bb[i] = xx[(i + 1) % n] - xx[i];
cc[i] = cross2(xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]);
}
k = 0;
for (i = 0; i < n; i++)
if (tt[(i - 1 + n) % n] == 'C' && tt[i] == 'S')
k++;
if (k == 0) {
n_ = 0;
for (i = 0; i < n; i++)
if (tt[i] == 'S')
ii[n_++] = i;
sort(ii, 0, n_);
head = cnt = 0;
for (h = 0; h < n_; h++) {
i = ii[h];
if (cnt && (c = check(ii[head + cnt - 1], i, ii[head]))) {
if (c == -1) {
printf("NIE\n");
goto end;
}
continue;
}
while (cnt >= 2 && (c = check(ii[head + cnt - 2], ii[head + cnt - 1], i))) {
if (c == -1) {
printf("NIE\n");
goto end;
}
cnt--;
}
while (cnt >= 2 && (c = check(i, ii[head], ii[head + 1]))) {
if (c == -1) {
printf("NIE\n");
goto end;
}
head++, cnt--;
}
ii[head + cnt++] = i;
}
printf(tt[i] == 'C' ? "NIE\n" : "TAK\n");
} else if (k == 1) {
i1 = i2 = -1;
for (i = 0; i < n; i++)
if (tt[(i - 1 + n) % n] == 'S' && tt[i] == 'C')
i1 = i;
else if (tt[(i - 1 + n) % n] == 'C' && tt[i] == 'S')
i2 = i;
if ((i1 - i2 + n) % n == 1) {
printf("NIE\n");
goto end;
}
if (sgn(cross(xx[(i1 - 1 + n) % n], yy[(i1 - 1 + n) % n], xx[i1], yy[i1], xx[i2], yy[i2])) * sgn(cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i2 + 1) % n], yy[(i2 + 1) % n])) > 0) {
printf("NIE\n");
goto end;
}
for (i = 0; i < n; i++)
if (tt[i] == 'C' && intersect_(xx[i1], yy[i1], xx[i2], yy[i2], xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n])) {
printf("NIE\n");
goto end;
}
if (cross(xx[(i1 - 1 + n) % n], yy[(i1 - 1 + n) % n], xx[i1], yy[i1], xx[i2], yy[i2]) > 0) {
if (cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i1 + 1) % n], yy[(i1 + 1) % n]) < 0) {
printf("NIE\n");
goto end;
}
} else {
if (cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i1 + 1) % n], yy[(i1 + 1) % n]) > 0) {
printf("NIE\n");
goto end;
}
tmp = i1, i1 = i2, i2 = tmp;
}
t1 = -INF, t2 = INF;
for (i = 0; i < n; i++)
if (tt[i] == 'S') {
c = cross2(xx[i2] - xx[i1], yy[i2] - yy[i1], xx[(i + 1) % n] - xx[i], yy[(i + 1) % n] - yy[i]);
if (c == 0) {
if (cross(xx[i1], yy[i1], xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]) >= 0) {
printf("NIE\n");
goto end;
}
} else {
t = (ld) cross2(xx[i] - xx[i1], yy[i] - yy[i1], xx[(i + 1) % n] - xx[i], yy[(i + 1) % n] - yy[i]) / c;
if (c > 0)
t1 = max(t1, t);
else
t2 = min(t2, t);
}
}
printf(t1 < t2 ? "TAK\n" : "NIE\n");
} else {
printf("?\n");
}
end:;
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |