Tag: Cross Product
Link: poj 2318
There is a rectangular box with blocks and the coordinates of these lines are given in order,
There are N line segments, the box is divided into n + 1 areas, and then there are m toys. The coordinates of these m toys are known and asked how many toys are there in each area at last.
Analysis: from left to right, until you determine whether the online segment of the toy is in the clockwise direction, this requires the use of cross product, of course, you can use binary search optimization.
Cross Product: known vector A (x1, Y1), vector B (X2, Y2), AXB = x1 * y2-x2 * Y1,
If AXB is greater than 0, A is in the clockwise direction of B. If AXB is less than 0, A is in the clockwise direction of B.
Note: An empty row is required for each group of data.
# Include <stdio. h> # include <string. h> int chaji (INT X1, int Y1, int X2, int Y2) {return X1 * y2-x2 * Y1;} int main () {int U [5010], L [5010], X, Y, X1, Y1, X2, Y2, M, N, I, j, s [5010]; while (scanf ("% d ", & N )! = EOF) {If (n = 0) break; scanf ("% d", & M, & X1, & Y1, & X2, & Y2); for (I = 0; I <n; I ++) scanf ("% d", & U [I], & L [I]); U [N] = L [N] = x2; // Add the last line segment to memset (S, 0, sizeof (s); for (I = 0; I <m; I ++) {scanf ("% d", & X, & Y); For (j = 0; j <= N; j ++) if (chaji (U [J]-l [J], y1-y2, X-L [J], y-y2)> 0) {// Cross Product judgment s [J] ++; break ;}}for (j = 0; j <= N; j ++) printf ("% d: % d \ n ", J, s [J]); printf (" \ n ");} return 0 ;}
Link: poj 2398
The meaning is the same as that in the previous question, but the given line segments are out of order, so they need to be sorted,
The output is also a little different. You need to output the number of intervals with 1-m toys, which are output in order.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; struct point {int L, U;} p [1010]; int chaji (INT X1, int Y1, int X2, int Y2) {return X1 * y2-x2 * Y1;} int CMP (struct point A, struct point B) {if (. l! = B. l) return. L <B. l; return. U <B. u;} int main () {int X, Y, X1, Y1, X2, Y2, M, N, I, j, s [1010], a [1010]; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; scanf ("% d", & M, & X1, & Y1, & X2, & Y2); for (I = 0; I <n; I ++) scanf ("% d", & P [I]. u, & P [I]. l); P [N]. U = P [N]. L = x2; sort (p, p + n + 1, CMP); // sort memset (S, 0, sizeof (s); memset (A, 0, sizeof (a); for (I = 0; I <m; I ++) {scanf ("% d", & X, & Y ); for (j = 0; j <= N; j ++) if (chaji (P [J]. u-p [J]. l, y1-y2, X-P [J]. l, y-y2)> 0) {s [J] ++; break ;}}for (j = 0; j <= N; j ++) A [s [J] ++; printf ("Box \ n"); for (I = 1; I <= N; I ++) if (A [I]) printf ("% d: % d \ n", I, a [I]);} return 0 ;}