Meaning of the questions:
Give you a C, to give you when n groups a, b, let you find what value of x, $ \ sum_ {i = 1} ^ n | a_i * x + b_i | = C $, required to obtain the solution number, and the most simple fraction from small to large that if there are infinitely many solutions, output -1.
answer:
In fact, these equations is a set of curves on a plane, are V-shaped, the lowest point on the x-axis, all determined zero to the zero left to right order.
Easy to see, and these functions are of a curve y = f (i), this curve has a maximum turning point n, then the turning point in the n separated intervals n + 1, and n points, and find the intersection y = C proportional to the formula. Case of an infinite number of solutions exist coinciding with the line segment C y =.
First, the value of all the pre-turning point on f (i), pay attention to the range of n is 1e5, it is impossible to let you $ O (n ^ 2) $ evaluated at each point, in fact, just to maintain the prefix of a and b and when and suffixes and require a certain point x_k $ $, takes a positive zero at the left of the dot this function, this function is zero at a point right inverted.
$(\sum_{i=1}^{k-1}a_i) *x_k+\sum_{i=1}^{k-1}b_i-(\sum_{i=k+1}^{n}a_i) *x_k-\sum_{i=k+1}^{n}b_i$
Note that the zero overlap state is determined.
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<stack> #include<algorithm> using namespace std; typedef long long LL; typedef long long ll; const int M = 1e5 + 10; const double eps = 1e-7; const LL mod = 998244353; const LL lINF = 0x3f3f3f3f3f3f3f3f; struct node { int a, b; }tr[M]; int t; int n, c; int fenzi[M], fenmu[M]; int ans; int gcd(int a, int b) { if (!b) return a; else return gcd(b, a % b); } double lst; bool cmp(node x, node y) { return x.a * y.b - x.b * y.a < 0; } bool cmp2(node x, node y) { return (double)x.b / -x.a < (double)y.b / -y.a; } bool cmp1(node x, node y) { return (double)x.b / -x.a <= (double)y.b / -y.a; } int suma[M], sumb[M]; int flag; double nw; double nx; int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &c); for (int i = 1; i <= n; i++) { scanf("%d%d", &tr[i].a, &tr[i].b); } sort(tr + 1, tr + n + 1, cmp); suma[0] = sumb[0] = 0; for (int i = 1; i <= n; i++) { suma[i] = suma[i - 1] + tr[i].a; sumb[i] = sumb[i - 1] + tr[i].b; } flag = ans = 0; lst = -10000.0; for (int i = 0; i <= n; i++) { int tmpa = -suma[n]; int tmpb = -sumb[n]; tmpa += 2 * suma[i]; tmpb += 2 * sumb[i]; nw = (double)sumb[i] / suma[i]; nx = (double)sumb[i + 1] / suma[i + 1]; if (fabs(nw - nx) < eps) continue; if (!tmpa && tmpb == c) { flag = 1; break; } if (!i) { node tmpc; tmpc.a = tmpa, tmpc.b = tmpb - c; if (cmp1(tmpc, tr[1])) { fenzi [years] = -tmpc.b; fenmu [years] = mtpa; int d = gcd (fenzi [years], fenmu [years]); fenzi [years] / = d; fenmu [years] / = d; if (fenmu [years] < 0 ) { fenzi [years] = -fenzi [years], fenmu [years] = - fenmu [years]; } Years ++ ; } } Else if (i == n) { Node TMCP; tmpc.a = TMPA, tmpc.b TMPB = - c; if (cmp2 (tr [n], TMCP)) { fenzi [years] = - tmpc.b; fenmu [years] = mtpa; int d = gcd (fenzi [years], fenmu [years]); fenzi [years] / = d; fenmu [years] / = d; if (fenmu [years] < 0 ) { fenzi [years] = -fenzi [years], fenmu [years] = - fenmu [years]; } Years ++ ; } } Else { node TMCP; tmpc.a = TMPA, tmpc.b TMPB = - c; if (cmp2 (tr [i], TMCP) && CMP1 (TMCP, tr [i + 1 ])) { fenzi [years] = - tmpc.b; fenmu [years] = mtpa; int d = gcd (fenzi [years], fenmu [years]); fenzi [years] / =d; fenmu [years] / = d; if (fenmu [years] < 0 ) { fenzi [years] = -fenzi [years], fenmu [years] = - fenmu [years]; } Years ++ ; } } Lst = ( dual ) (TMPB - c) / TMPA; } If (flag) { printf ( " -1 \ n " ); } Else { printf ( "%d", ans); for (int i = 0; i < ans; i++) { printf(" %d/%d", fenzi[i], fenmu[i]); } puts(""); } } }