Codeforces Round #335 (Div. 1) C. Freelancer's Dreams 计算几何
C. Freelancer's Dreams
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://www.codeforces.com/contest/605/problem/C
Description
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.
He has suggestions to work on n distinct projects. Mikhail has already evaluated that the participation in the i-th project will increase his experience by ai per day and bring bi dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time.
Find the real value, equal to the minimum number of days Mikhail needs to make his dream come true.
For example, suppose Mikhail is suggested to work on three projects and a1 = 6, b1 = 2, a2 = 1, b2 = 3, a3 = 2, b3 = 6. Also, p = 20and q = 20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed,a1·2.5 + a2·0 + a3·2.5 = 6·2.5 + 1·0 + 2·2.5 = 20 and b1·2.5 + b2·0 + b3·2.5 = 2·2.5 + 3·0 + 6·2.5 = 20.
Input
The first line of the input contains three integers n, p and q (1 ≤ n ≤ 100 000, 1 ≤ p, q ≤ 1 000 000) — the number of projects and the required number of experience and money.
Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 1 000 000) — the daily increase in experience and daily income for working on the i-th project.
Output
Print a real value — the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if
Sample Input
3 20 20
6 2
1 3
2 6
Sample Output
5.000000000000000
HINT
题意
给你一个2*n的矩阵A,然后让你找到一个n*2的矩阵B
要求使得矩阵中的所有数的和尽量小,使得A*B = [X,Y]
要求X>=P,Y>=Q
输出最小和
题解:
转化成计算几何问题,相当于给了你n个向量,然后你需要一个线性组合使得某个向量,超过P,Q这个点
我们可以将所有向量组成一个凸包。
这里需要一个证明,假设B矩阵的和是1的话,所有向量的线性组合最多达到这个凸包的边界上。
如果n=1的话,很显然成立,n=2也显然成立,n=3必然比n=2指的更加短,所以这个结论就成立了?
然后我们就二分答案就好了,然后再判一判(P,Q)这个点是否在这个凸包内部就好了咯。
代码:
#include<iostream> #include<math.h> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; #define maxn 100005 const double EP = 1e-7; const int MAXV = 300; const double PI = 3.14159265; /* 基本几何结构 */ struct POINT { double x; double y; POINT(double a=0, double b=0) { x=a; y=b;} //constructor }; POINT operator - (POINT A,POINT B){return POINT(A.x-B.x,A.y-B.y);} struct LINESEG { POINT s; POINT e; LINESEG(POINT a, POINT b) { s=a; e=b;} LINESEG() { } }; double multiply(POINT sp,POINT ep,POINT op) { return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y)); } bool InsideConvexPolygon(int vcount,POINT polygon[],POINT q) // 可用于三角形! { POINT p; LINESEG l; int i; p.x=0;p.y=0; for(i=0;i<vcount;i++) // 寻找一个肯定在多边形polygon内的点p:多边形顶点平均值 { p.x+=polygon[i].x; p.y+=polygon[i].y; } p.x /= vcount; p.y /= vcount; for(i=0;i<vcount;i++) { l.s=polygon[i];l.e=polygon[(i+1)%vcount]; if(multiply(p,l.e,l.s)*multiply(q,l.e,l.s)<0) /* 点p和点q在边l的两侧,说明点q肯定在多边形外 */ break; } return (i==vcount); } double Cross(POINT a,POINT b) { return a.x*b.y-a.y*b.x; } bool cmp1(POINT a,POINT b) { if(fabs(a.x-b.x)<EP) return a.y<b.y; return a.x<b.x; } int CH(POINT* p,int n,POINT* ch) { sort(p,p+n,cmp1); int m=0; for(int i=0;i<n;i++) { while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--; ch[m++]=p[i]; } if(n>1)m--; return m; } int N,tot; POINT fin,pp[maxn],t[maxn],T[maxn]; int check(double x) { for(int i=0;i<tot;i++) T[i].x = t[i].x*x,T[i].y = t[i].y*x; if(InsideConvexPolygon(tot,T,fin))return 1; return 0; } int main() { memset(pp,0,sizeof(pp)); memset(t,0,sizeof(t)); memset(T,0,sizeof(T)); double X=0,Y=0; scanf("%d",&N);cin>>fin.x>>fin.y; for(int i=0;i<N;i++) { double x,y; scanf("%lf%lf",&x,&y); X = max(X,x); Y = max(Y,y); pp[i].x = x,pp[i].y = y; } pp[N].x = 0,pp[N].y = 0; pp[N+1].x = X,pp[N+1].y = 0; pp[N+2].x = 0,pp[N+2].y = Y; N+=3; tot = CH(pp,N,t); double l = 0,r = 999990009.0; for(int i=1;i<=100;i++) { double mid = (l+r)/2.0; if(check(mid))r=mid; else l=mid; } printf("%.15f\n",l); }