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 .

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。

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define MIT (2147483647)
 21 #define INF (1000000001)
 22 #define MLL (1000000000000000001LL)
 23 #define sz(x) ((int) (x).size())
 24 #define clr(x, y) memset(x, y, sizeof(x))
 25 #define puf push_front
 26 #define pub push_back
 27 #define pof pop_front
 28 #define pob pop_back
 29 #define mk make_pair
 30 
 31 inline int Getint()
 32 {
 33     int Ret = 0;
 34     char Ch = ' ';
 35     bool Flag = 0;
 36     while(!(Ch >= '0' && Ch <= '9'))
 37     {
 38         if(Ch == '-') Flag ^= 1;
 39         Ch = getchar();
 40     }
 41     while(Ch >= '0' && Ch <= '9')
 42     {
 43         Ret = Ret * 10 + Ch - '0';
 44         Ch = getchar();
 45     }
 46     return Flag ? -Ret : Ret;
 47 }
 48 
 49 const DB EPS = 1e-7, PI = acos(-1.0);
 50 const int N = 100010;
 51 class Point
 52 {
 53 private :
 54     int x, y;
 55 public :
 56     Point() {}
 57     Point(const int tx, const int ty)
 58     {
 59         x = tx, y = ty;
 60     }
 61     inline bool operator <(const Point &t) const
 62     {
 63         if(x != t.x) return x > t.x;
 64         return y < t.y;
 65     }
 66     
 67     inline bool operator ==(const Point &t) const
 68     {
 69         return x == t.x && y == t.y;
 70     }
 71     
 72     inline void Read()
 73     {
 74         scanf("%d%d", &x, &y);
 75     }
 76     
 77     inline int Get(const int t) const
 78     {
 79         return t ? y : x;
 80     }
 81 } arr[N];
 82 int n, p, q;
 83 DB ans;
 84 
 85 inline void Input()
 86 {
 87     scanf("%d%d%d", &n, &p, &q);
 88     for(int i = 0; i < n; i++) arr[i].Read();
 89 }
 90 
 91 inline LL Multi(const Point &o, const Point &a, const Point &b)
 92 {
 93     LL d1[2], d2[2];
 94     for(int i = 0; i < 2; i++)
 95         d1[i] = a.Get(i) - o.Get(i), d2[i] = b.Get(i) - o.Get(i);
 96     return d1[0] * d2[1] - d1[1] * d2[0];
 97 }
 98 
 99 inline void GetHull(Point *arr, int &n)
100 {
101     static int index[N];
102     int len = 0;
103     for(int i = 0; i < n; i++)
104     {
105         while(len >= 2 && Multi(arr[index[len - 2]], arr[index[len - 1]], arr[i]) <= 0) len--;
106         index[len++] = i;
107     }
108     for(int i = 0; i < len; i++) arr[i] = arr[index[i]];
109     n = len;
110 }
111 
112 inline bool Cross(const Point &a, const Point &b, const Point &c, const Point &d)
113 {
114     LL dir1 = Multi(a, b, c), dir2 = Multi(a, b, d);
115     if(!dir1 || !dir2) return 1;
116     return (dir1 > 0) ^ (dir2 > 0);
117 }
118 
119 inline DB Sqr(DB x)
120 {
121     return x * x;
122 }
123 
124 inline DB Dist(const Point &a, const Point &b)
125 {
126     DB ret = 0.0;
127     for(int i = 0; i < 2; i++)
128         ret += Sqr(a.Get(i) - b.Get(i));
129     return sqrt(ret);
130 }
131 
132 inline void Solve()
133 {
134     ans = 1.0 * INF;
135     for(int i = 0; i < n; i++)
136     {
137         DB t = max((1.0 * p) / arr[i].Get(0), (1.0 * q) / arr[i].Get(1));
138         ans = min(ans, t);
139     }
140     
141     int mx1 = 0, mx2 = 0;
142     for(int i = 0; i < n; i++)
143         mx1 = max(mx1, arr[i].Get(0)),
144         mx2 = max(mx2, arr[i].Get(1));
145     arr[n] = Point(mx1, 0), arr[n + 1] = Point(0, mx2);
146     n += 2;
147     sort(arr, arr + n);
148     n = unique(arr, arr + n) - arr;
149     
150     GetHull(arr, n);
151     
152     Point g = Point(p, q), s = Point(0, 0);
153     for(int i = 0; i < n - 1; i ++)
154     {
155         if(!Cross(s, g, arr[i], arr[i + 1])) continue;
156         Point b = arr[i], c = arr[i + 1];
157         DB  bc = Dist(b, c), gc = Dist(g, c), 
158             sg = Dist(s, g), sb = Dist(s, b), sc = Dist(s, c);
159         /*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));
160         DB sxc = PI - scb - csg;
161         DB sx = sin(scb) * (sc / sin(sxc));*/
162         DB cosscb = (Sqr(sc) + Sqr(bc) - Sqr(sb)) / (2.0 * sc * bc), coscsg = (Sqr(sc) + Sqr(sg) - Sqr(gc)) / (2.0 * sc * sg);
163         DB sinscb = sqrt(1 - Sqr(cosscb)), sincsg = sqrt(1 - Sqr(coscsg));
164         DB sinsxc = sinscb * coscsg + cosscb * sincsg;
165         DB sx = sinscb * (sc / sinsxc);
166         ans = min(ans, sg / sx);
167     }
168     
169     printf("%.15lf\n", ans);
170 }
171 
172 int main()
173 {
174     freopen("a.in", "r", stdin);
175     Input();
176     Solve();
177     return 0;
178 }
View Code

 

后记:

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

  这是证明:

We want to minimize  given that  and , and .

Now, let's add a linear combination of the two constraints together. They will be weighted by 2 numbers. So, we have .

The left hand side can be rewritten as .

Note that if we add the constraints , then we'll have .

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

 

贴上TooSimple大神的代码。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 #define rep(i,a,n) for (int i=a;i<n;i++)
 5 typedef long double LD;
 6 const int N=101000;
 7 int n,p,q,a[N],b[N];
 8 LD ff(LD x) {
 9     LD mv=1;
10     rep(i,0,n) mv=min(mv,(1-b[i]*x)/a[i]);
11     return mv*p+x*q;
12 }
13 int main() {
14     scanf("%d%d%d",&n,&p,&q);
15     rep(i,0,n) scanf("%d%d",a+i,b+i);
16     LD l=0,r=1; r/=*max_element(b,b+n);
17     rep(i,0,200) {
18         LD fl=(l+l+r)/3,fr=(r+r+l)/3;
19         if (ff(fl)>ff(fr)) r=fr; else l=fl;
20     }
21     printf("%.10f\n",(double)ff((l+r)/2));
22 }
View Code

 

转载于:https://www.cnblogs.com/StupidBoy/p/5062525.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值