Solution: Because there are too many solutions, random catch A, B, as long as there is compliance on the line; (first, the ax+by=0 must represent a straight line, i.e. A, B cannot be 0 at the same time, and notice that the line cannot cross the 2N point in the input, and that the detection point on which side of the line requires simple linear programming knowledge)
1#include <cstdio>2#include <cstdlib>3 4 intx[ -], y[ -];5 6 intTestintAintBintN)7 {8 Static intI, POS, neg, tmp;9pos =0, neg =0;Ten for(i =2*n-1; I >=0; i--) One { ATMP = A*x[i] + b*Y[i]; - if(tmp >0) Neg + +; - Else if(TMP <0) POS + +; the Else return 0; - } - returnpos = =neg; - } + - voidFindintN) + { A intA, B; at while(1) - { -A = rand ()%1001- -; -B = rand ()%1001- -; - if(Test (A, B, N)) - { inprintf"%d%d\n", A, B); - Break; to } + } - } the * intMain () $ {Panax Notoginseng intN, I; - while(SCANF ("%d", &n) = =1&&N) the { + for(i =2*n-1; I >=0; i--) Ascanf"%d%d", &x[i], &y[i]); the find (N); + } -}
Enumeration:
1#include <iostream>2#include <vector>3 using namespacestd;4 voidBruteforce (int&a,int&b, vector<int> &x, vector<int> &y,intN) {5 for(A =- -; A <= -; a++){6 for(B =- -; B <= -; b++){7 intD =0, U =0;8 for(inti =0; I <2N i++){9 if(A*x[i] + b*y[i] >0) u++;Ten if(A*x[i] + B*y[i] <0) d++; One } A if(U = = N&&d = = N)return; - } - } the } - intMain () - { - intN; + while(cin >> N&&n! =0){ -vector<int> x (2*n,0); +vector<int> Y (2*n,0); A for(inti =0; I <2*n; i++){ atCIN >> X[i] >>Y[i]; - } - intA, B; - Bruteforce (A, B, x, y, n); -cout << A <<' '<< B <<Endl; - } in return 0; -}
UVA 10167-birthday Cake