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 大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可 ...
随机推荐
- Xcode常用代码块
Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便. 常用的: 1.strong:@property (nonatomic,stron ...
- NYOJ题目1049自增自减
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAN0CAIAAAA4f3koAAAgAElEQVR4nO3dO3LbyNoG4H8TyrUQx1
- php上传文件进度条
ps:本文转自脚本之家 Web应用中常需要提供文件上传的功能.典型的场景包括用户头像上传.相册图片上传等.当需要上传的文件比较大的时候,提供一个显示上传进度的进度条就很有必要了. 在PHP 5.4以前 ...
- 用with实现python的threading,新鲜啊
哈哈,2.5以后可用.自动加锁释放,如同操作文件打开关闭一样. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading impor ...
- 【openGL】画正弦函数图像
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- Win7下的内置FTP组件的设置详解
在局域网中共享文件,FTP是比较方便的方案之一.Win7内部集成了FTP,只是设置起来颇费一番功夫.着文以记之. 一.安装FTP组件 由于Win7默认没有安装FTP组件.故FTP的设置第一步就是安装F ...
- ASP.NET WebApi Document Helper
本项目实现了ASP.NET WebApi 接口文档的自动生成功能. 微软出的ASP.NET WebApi Help Page固然好用,但是我们项目基于Owin 平台的纯WebApi 项目,不想引入MV ...
- Win10 资源文件
ResourceLoader rl = new ResourceLoader(); DisOutText.Text = rl.GetString("Display"); Resou ...
- 消息队列通信,王明学learn
消息队列通信 消息队列就是一个消息(一个结构)的链表.而一条消息则可看作一个记录,具有特定的格式.进程可以从中按照一定的规则添加新消息:另一些进程则可以从消息队列中读走消息. 每一个消息都是一个结构体 ...
- 并发异步处理队列 .NET 4.5+
namespace Test { using System; using System.Threading; using System.Threading.Tasks; using Microshao ...