Submission #945464

# Submission time Handle Problem Language Result Execution time Memory
945464 2024-03-13T20:34:32 Z rainboy Tapestries (POI13_gob) C
10 / 100
2 ms 348 KB
#include <stdio.h>

#define N	1000
#define eps	1e-12

int sgn(long long a) {
	return a == 0 ? 0 : (a > 0 ? 1 : -1);
}

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

typedef long double ld;

long long cross(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}

double cross_(double x1, double y1, double x2, double y2) {
	return x1 * y2 - x2 * y1;
}

long long dot(int x1, int y1, int x2, int y2) {
	return (long long) x1 * x2 + (long long) y1 * y2;
}

double dot_(double x1, double y1, double x2, double y2) {
	return x1 * x2 + y1 * y2;
}

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 = cross(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, double *x, double *y) {
	long long c;

	c = cross(aa[i], bb[i], aa[j], bb[j]);
	*x = (double) -cross(cc[i], bb[i], cc[j], bb[j]) / c;
	*y = (double) -cross(aa[i], cc[i], aa[j], cc[j]) / c;
}

int check(int i, int j, int k) {
	long long cij, cjk;
	double x1, y1, x2, y2;

	cij = cross(aa[i], bb[i], aa[j], bb[j]);
	cjk = cross(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 ? cross(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross(cc[i], bb[i], cc[j], bb[j]) * sgn(bb[i])) > 0)
				return 1;
		} else {
			if ((aa[i] != 0 ? cross(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross(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 ? cross(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross(cc[k], bb[k], cc[j], bb[j]) * sgn(bb[k])) > 0)
				return 1;
		} else {
			if ((aa[k] != 0 ? cross(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross(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 (cross_(x2 - x1, y2 - y1, aa[j], bb[j]) <= 0)
		return cross(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, c;

		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] = cross(xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]);
		}
		i1 = -1, i2 = -1;
		for (i = 0; i < n; i++)
			if (tt[i] == 'C' && tt[(i - 1 + n) % n] == 'S') {
				if (i1 == -1)
					i1 = i;
				else
					i2 = i;
			}
		if (i2 == -1) {
			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;
			}
			if (i1 == -1)
				printf(tt[i] == 'C' ? "NIE\n" : "TAK\n");
			else
				printf("?\n");
		} else {
			printf("?\n");
		}
end:;
	}
	return 0;
}

Compilation message

gob.c: In function 'compare':
gob.c:41:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   41 |  sgni = aa[i] < 0 || aa[i] == 0 && bb[i] < 0 ? -1 : 1;
      |                      ~~~~~~~~~~~^~~~~~~~~~~~
gob.c:42:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   42 |  sgnj = aa[j] < 0 || aa[j] == 0 && bb[j] < 0 ? -1 : 1;
      |                      ~~~~~~~~~~~^~~~~~~~~~~~
gob.c: In function 'main':
gob.c:114:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
gob.c:120:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   scanf("%d", &n);
      |   ^~~~~~~~~~~~~~~
gob.c:122:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |    scanf("%d%d", &xx[i], &yy[i]);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gob.c:124:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |    scanf("%s", s);
      |    ^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Incorrect 1 ms 348 KB Output isn't correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Incorrect 1 ms 348 KB Output isn't correct
9 Incorrect 2 ms 348 KB Output isn't correct
10 Correct 1 ms 348 KB Output is correct