小言_互联网的博客

CF605C. Freelancer's Dreams

388人阅读  评论(0)

CF605C. Freelancer’s Dreams

题目描述

Solution

实际上就是给定 a i , b i , A , B a_i,b_i,A,B ,求n维向量 ( x 1.. x n ) (x1..x_n) ,使得:
i = 1 n a i x i A i = 1 n b i x i B m i n z = i x i \sum_{i=1}^na_ix_i\geq A\\\sum_{i=1}^nb_ix_i\geq B\\min\;z=\sum_i{x_i}
答案就是 m i n z min\;z
这是一个简单线性规划问题,转化为对偶形式,可得:
i = 1.. n a i y 1 + b i y 2 < = 1 m a x z = A y 1 + B y 2 y 1 , y 2 0 \forall_{i=1..n}a_iy_1+b_iy_2<=1\\max\;z={Ay_1+By_2}\\y_1,y_2\geq 0
可以把上面的1式转化为:
i = 1.. n y 1 < = 1 b i y 2 a i \forall_{i=1..n}y_1<=\frac{1-b_iy_2}{a_i}

现在的变量是 y 1 , y 2 y_1,y_2 ,相当于求二维平面的半平面交,可以求一个凸包解决。

但显然有更简单的方法:我们确定了 y 2 y_2 之后可以唯一确定 y 1 y_1 ,并且该情况下答案显然是一个凸函数,所以可以直接三分 y 2 y_2 去求出极值点。

时间复杂度 O ( n l g n ) O(nlgn)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-12;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int n,A,B,a[MAXN],b[MAXN];
lod check(lod y2)
{
	lod y1=1e6;
	for (int i=1;i<=n;i++) upmin(y1,(1-y2*b[i])/a[i]);
	if (y1<eps) return -1;
	return y1*A+y2*B;
}
int main()
{
	n=read(),A=read(),B=read();
	for (int i=1;i<=n;i++) a[i]=read(),b[i]=read();
	lod l=0,r=1e6;
	while (r-l>eps)
	{
		lod midl=l+(r-l)/3,midr=r-(r-l)/3;
		if (check(midl)+eps<check(midr)) l=midl;
		else r=midr;
	}
	printf("%.11lf\n",(double)check(l));
	return 0;
}

转载:https://blog.csdn.net/xmr_pursue_dreams/article/details/104414855
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场