CF#335 Freelancer's Dreams
2 seconds
256 megabytes
standard input
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.
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.
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
.
3 20 20
6 2
1 3
2 6
5.000000000000000
4 1 1
2 3
3 2
2 3
3 2
0.400000000000000
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
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大神的代码。
#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)/));
}
CF#335 Freelancer's Dreams的更多相关文章
- 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/contes ...
- Codeforces 605C Freelancer's Dreams 凸包 (看题解)
Freelancer's Dreams 我们把每个二元组看成是平面上的一个点, 那么两个点的线性组合是两点之间的连线, 即x * (a1, b1) + y * (a1, b1) && ...
- Codeforces Round #335 (Div. 1)--C. Freelancer's Dreams 线性规划对偶问题+三分
题意:p, q,都是整数. sigma(Ai * ki)>= p, sigma(Bi * ki) >= q; ans = sigma(ki).输出ans的最小值 约束条件2个,但是变量k有 ...
- CF#335 Intergalaxy Trips
Intergalaxy Trips time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF#335 Board Game
Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
- CF#335 Lazy Student
Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codeforce 605BE. Freelancer's Dreams
题意:给你n个工程,做了每个工程相应增长x经验和y钱.问你最少需要多少天到达制定目标.时间可以是浮点数. 思路:杜教思路,用对偶原理很简易.个人建议还是标准解题法,凸包+线性组合. #include& ...
- CF #335 div1 A. Sorting Railway Cars
题目链接:http://codeforces.com/contest/605/problem/A 大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可 ...
随机推荐
- Qt5_简易画板_详细注释
代码下载链接: http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg 显示效果如下: 代码附有详细注释(代码如下) /*** * 先新建QMainWindow, 项目名称 ...
- NMON中的各项参数指标
一.NMON中的各项参数指标: SYS_SUMM:显示当前服务器的总体性能情况 Total System I/OStatistics:Avg tps during an interval:显示采集间隔 ...
- SQLServer操作结果集
union组合结果集 --相同合并 union --全部显示 union all 公用表表达式 with CET( wName,dName) as ( select wName,dName from ...
- Ubuntu下调整swap分区的大小
转自:http://blog.chinaunix.net/uid-7573623-id-2048964.html 由于安装oracle 的时候,swap太小不能继续安装,于是想有什么方法在不不用安装o ...
- Android屏幕旋转总结
转自:http://www.myexception.cn/operating-system/1452058.html 1. ProjectConifg.mk中定义宏MTK_LCM_PHYSICAL_R ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- Code Review for SSIS package
以下是我对SSIS包进行code review的一些建议,如果有其他更好的方案欢迎拍砖. A. 查看是否使用了最优的解决方案 1. 最优的结构视图 2. 解决方案,包,任务,组建,参数的命名使用了易读 ...
- Matlab中如何将(自定义)函数作为参数传递给另一个函数
假如我们编写了一个积分通用程序,想使它更具有通用性,那么可以把被积函数也作为一个参数.在c/c++中,可以使用函数指针来实现上边的功能,在matlab中如何实现呢?使用函数句柄--这时类似于函数指针的 ...
- Mesa 3D
Mesa 3D是一个在MIT许可证下开放源代码的三维计算机图形库,以开源形式实现了OpenGL的应用程序接口. OpenGL的高效实现一般依赖于显示设备厂商提供的硬件,而Mesa 3D是一个纯基于软件 ...
- Java关键字native、volatile、transient
native native是方法修饰符.Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法.一般用于JNI中. native关键字说明其修饰的方法是一个原生态方法,方 ...