【BZOJ2876】[Noi2012]骑行川藏【二分】【拉格朗日乘数法】

【题目链接】

显然是一个约束条件下多元函数最值问题,那么就用拉格朗日乘数法就行了。然后二分lambda的值,解方程。

具体见POPOQQQ大爷的题解

【POPOQQQ的题解】

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long double LD;

const int maxn = 10005;
const LD eps = 1e-12;

int n;
LD E, s[maxn], k[maxn], v[maxn], u[maxn];

inline bool check(LD lambda) {
	LD res = 0.0;
	for(int i = 1; i <= n; i++) {
		LD l = max((LD)0.0, v[i]), r = 0x3f3f3f3f;
		while(r - l > eps) {
			LD mid = (l + r) / 2;
			if(2 * lambda * k[i] * mid * mid * (mid - v[i]) > 1.0) r = mid;
			else l = mid;
		}
		u[i] = r;
		res += k[i] * (u[i] - v[i]) * (u[i] - v[i]) * s[i];
	}
	return res > E;
}

int main() {
	scanf("%d%Lf", &n, &E);
	for(int i = 1; i <= n; i++) scanf("%Lf%Lf%Lf", &s[i], &k[i], &v[i]);

	LD l = 0.0, r = 0x3f3f3f3f;
	while(r - l > eps) {
		LD mid = (l + r) / 2;
		if(check(mid)) l = mid;
		else r = mid;
	}

	LD ans = 0.0;
	for(int i = 1; i <= n; i++) ans += s[i] / u[i];
	printf("%.10Lf\n", ans);
	return 0;
}

庆幸看过数分

你可能感兴趣的:(【BZOJ2876】[Noi2012]骑行川藏【二分】【拉格朗日乘数法】)