Evelyn likes drawing very much. Today, she draws lots of rainbows on white paper of infinite size, each using a different color. Since there're too many rainbows now, she wonders, how many of them can be seen?
For simplicity, each rainbow Li is represented as a non-vertical line specified by the equation: y=aix+bi. A rainbow Li can be seen if there exists some x-coordinate x0at which, its y-coordinate is strictly greater than y-coordinates of any other rainbows: aix0+bi > ajx0+bj for all j != i.
Now, your task is, given the set of rainbows drawn, figure out the number of rainbows that can be seen.
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 60) which is the number of test cases. And it will be followed by T consecutive test cases.
There's a blank line before every case. In each test case, there will first be an integer n (1 <= n <= 5000), which is the number of rainbows. Then n consecutive real number pairs follow. Each pair contains two real numbers, ai and bi, representing rainbow Li: y=aix+bi. No two rainbows will be the same, that is to say, have the same a and b.
Results should be directed to standard output. The output of each test case should be a single integer, which is the number of rainbows that can be seen.
2 1 1 1 3 1 0 2 0 3 0
12
类似求凸包,给出的是线的表达式,对于同斜率的,取最高的,然后根据斜率排个序,从左到右,计算之后
线与其交点的位置,取最近的那条线即可。
#include<map> #include<ctime> #include<cmath> #include<queue> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ms(x,y) memset(x,y,sizeof(x)) #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define loop(i,j,k) for (int i=j;i!=-1;i=k[i]) #define inone(x) scanf("%d",&x) #define intwo(x,y) scanf("%lf%lf",&x,&y) #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z) #define ft first #define sd second typedef long long LL; const int low(int x) { return x&-x; } const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 1e5 + 10; const double eps = 1e-10; int T, n, cnt, tot; pair<double, double> b[N], a[N]; int main() { for (inone(T); T--;) { inone(n); rep(i, 1, n) intwo(b[i].ft, b[i].sd); sort(b + 1, b + n + 1); tot = 0; cnt = 1; for (int i = 1, j; i <= n; i = j) { for (j = i; j <= n && b[j].ft - b[i].ft < eps; j++); a[++tot] = b[j - 1]; } n = tot; for (int i = 1, j, k; i <= n; i = k) { double x = k = n + 1; for (j = i + 1; j <= n; j++) { if (k > n) k = j, x = (a[i].sd - a[j].sd) / (a[j].ft - a[i].ft); else if (x - (a[i].sd - a[j].sd) / (a[j].ft - a[i].ft) > -eps) { k = j, x = (a[i].sd - a[j].sd) / (a[j].ft - a[i].ft); } } cnt += k <= n; } printf("%d\n", cnt); } return 0; }