CF#335 Freelancer's Dreams

时间:2023-03-09 19:09:41
CF#335 Freelancer's Dreams
Freelancer's Dreams
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 np 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 CF#335 Freelancer's Dreams.

Sample test(s)
input
3 20 20
6 2
1 3
2 6
output
5.000000000000000
input
4 1 1
2 3
3 2
2 3
3 2
output
0.400000000000000
Note

First sample corresponds to the example in the problem statement.

题意:给出n个二元组(ai,bi),给出(p,q),要求min(∑xi (1 <= i <= n) ),使得 ∑xi*ai >= p, 且∑xi*bi >= q。问min值是多少。

分析:考虑向量(ai,bi)

将其考虑为平面上的一个点。

观察一下它的凸包,显然凸包里面的所有点都可以是组成凸包的点的线性组合(在小于等于单位长度内)。

我们现在要做的是找一个最小的放大倍数x使得这个凸包包含(p,q)

如果是包含的话有点难搞,如果是恰好等于(恰好在边界上)的话就好搞了。

我们假设我们可以选择某些二元组只有一边有影响,即我们只取他的ai或者bi,这样的话,就相当于求恰好等于时的答案了。(因为如果是包含的话,一定可以使某些点的某一边没有影响,进而变为恰好等于)。

这时显然相当于加入两个二元组(max ai, 0)、(0, max bi),在求一次凸包。

求使(p, q)恰好在边界上的最小倍数。

求这个倍数的话。

从S(0,0)到G(p,q)拉一条线,设SG这条直线与凸包交与X点,那么倍数显然是SG/SX。

 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const DB EPS = 1e-, PI = acos(-1.0);
const int N = ;
class Point
{
private :
int x, y;
public :
Point() {}
Point(const int tx, const int ty)
{
x = tx, y = ty;
}
inline bool operator <(const Point &t) const
{
if(x != t.x) return x > t.x;
return y < t.y;
} inline bool operator ==(const Point &t) const
{
return x == t.x && y == t.y;
} inline void Read()
{
scanf("%d%d", &x, &y);
} inline int Get(const int t) const
{
return t ? y : x;
}
} arr[N];
int n, p, q;
DB ans; inline void Input()
{
scanf("%d%d%d", &n, &p, &q);
for(int i = ; i < n; i++) arr[i].Read();
} inline LL Multi(const Point &o, const Point &a, const Point &b)
{
LL d1[], d2[];
for(int i = ; i < ; i++)
d1[i] = a.Get(i) - o.Get(i), d2[i] = b.Get(i) - o.Get(i);
return d1[] * d2[] - d1[] * d2[];
} inline void GetHull(Point *arr, int &n)
{
static int index[N];
int len = ;
for(int i = ; i < n; i++)
{
while(len >= && Multi(arr[index[len - ]], arr[index[len - ]], arr[i]) <= ) len--;
index[len++] = i;
}
for(int i = ; i < len; i++) arr[i] = arr[index[i]];
n = len;
} inline bool Cross(const Point &a, const Point &b, const Point &c, const Point &d)
{
LL dir1 = Multi(a, b, c), dir2 = Multi(a, b, d);
if(!dir1 || !dir2) return ;
return (dir1 > ) ^ (dir2 > );
} inline DB Sqr(DB x)
{
return x * x;
} inline DB Dist(const Point &a, const Point &b)
{
DB ret = 0.0;
for(int i = ; i < ; i++)
ret += Sqr(a.Get(i) - b.Get(i));
return sqrt(ret);
} inline void Solve()
{
ans = 1.0 * INF;
for(int i = ; i < n; i++)
{
DB t = max((1.0 * p) / arr[i].Get(), (1.0 * q) / arr[i].Get());
ans = min(ans, t);
} int mx1 = , mx2 = ;
for(int i = ; i < n; i++)
mx1 = max(mx1, arr[i].Get()),
mx2 = max(mx2, arr[i].Get());
arr[n] = Point(mx1, ), arr[n + ] = Point(, mx2);
n += ;
sort(arr, arr + n);
n = unique(arr, arr + n) - arr; GetHull(arr, n); Point g = Point(p, q), s = Point(, );
for(int i = ; i < n - ; i ++)
{
if(!Cross(s, g, arr[i], arr[i + ])) continue;
Point b = arr[i], c = arr[i + ];
DB bc = Dist(b, c), gc = Dist(g, c),
sg = Dist(s, g), sb = Dist(s, b), sc = Dist(s, c);
/*DB scb = acos((Sqr(sc) + Sqr(bc) - Sqr(sb)) / (2.0 * sc * bc)), csg = acos((Sqr(sc) + Sqr(sg) - Sqr(gc)) / (2.0 * sc * sg));
DB sxc = PI - scb - csg;
DB sx = sin(scb) * (sc / sin(sxc));*/
DB cosscb = (Sqr(sc) + Sqr(bc) - Sqr(sb)) / (2.0 * sc * bc), coscsg = (Sqr(sc) + Sqr(sg) - Sqr(gc)) / (2.0 * sc * sg);
DB sinscb = sqrt( - Sqr(cosscb)), sincsg = sqrt( - Sqr(coscsg));
DB sinsxc = sinscb * coscsg + cosscb * sincsg;
DB sx = sinscb * (sc / sinsxc);
ans = min(ans, sg / sx);
} printf("%.15lf\n", ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

后记:

  CF上TOOSIMPLE大神提出:由于线性组合的对偶性,可以使用三分的手段做出这道题,非常简单。

  这是证明:

We want to minimize CF#335 Freelancer's Dreams given that CF#335 Freelancer's Dreams and CF#335 Freelancer's Dreams, and CF#335 Freelancer's Dreams.

Now, let's add a linear combination of the two constraints together. They will be weighted by 2 numbersCF#335 Freelancer's Dreams. So, we have CF#335 Freelancer's Dreams.

The left hand side can be rewritten as CF#335 Freelancer's Dreams.

Note that if we add the constraints CF#335 Freelancer's Dreams, then we'll have CF#335 Freelancer's Dreams.

So, to get a good lower bound, we can solve the following problem: CF#335 Freelancer's Dreams given that CF#335 Freelancer's Dreams for all i. Solving this new linear program will give us the best lower bound we can get for our original problem.

贴上TooSimple大神的代码。

 #include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
typedef long double LD;
const int N=;
int n,p,q,a[N],b[N];
LD ff(LD x) {
LD mv=;
rep(i,,n) mv=min(mv,(-b[i]*x)/a[i]);
return mv*p+x*q;
}
int main() {
scanf("%d%d%d",&n,&p,&q);
rep(i,,n) scanf("%d%d",a+i,b+i);
LD l=,r=; r/=*max_element(b,b+n);
rep(i,,) {
LD fl=(l+l+r)/,fr=(r+r+l)/;
if (ff(fl)>ff(fr)) r=fr; else l=fl;
}
printf("%.10f\n",(double)ff((l+r)/));
}