Description
Solution
容易列出方程:
t=max(dpjai+dpj/rtj×bi)
t
=
m
a
x
(
d
p
j
a
i
+
d
p
j
/
r
t
j
×
b
i
)
dpi=t×rti/(ai×rti+bi)
d
p
i
=
t
×
r
t
i
/
(
a
i
×
r
t
i
+
b
i
)
然后可以将其用斜率表示出来,发现并不具有单调性,然后CDQ分治即可。
第一次打用CDQ分治解决斜率优化DP的题目,感觉代码实现还是有很多细节的,参考抄了hzwer的代码。。
Source
有dalao知道为什么代码的NOTICE处没有加eps就会WA吗。。
我还是太菜了。。。
/************************************************
* Au: Hany01
* Date: Mar 16th, 2018
* Prob: [BZOJ1492][NOI2007] Cash
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("bzoj1492.in", "r", stdin);
freopen("bzoj1492.out", "w", stdout);
#endif
}
const int maxn = 100005;
const double eps = 1e-8;
int n, stk[maxn], top;
double f[maxn];
struct Data {
double a, b, sl, x, y, rt;
int id;
bool operator < (const Data& A) const { return sl > A.sl; }
}A[maxn], t[maxn];
inline double getdp(double t, int i) { return t * A[i].rt / (A[i].a * A[i].rt + A[i].b); }
inline double Slope(int x, int y) {
if (!y) return -1e20;
if (fabs(A[y].x - A[x].x) < eps) return 1e20;
return (A[y].y - A[x].y) / (A[y].x - A[x].x);
}
void CDQ(int l, int r)
{
//The bottom
if (l == r) {
chkmax(f[l], f[l - 1]);
A[l].x = getdp(f[l], l), A[l].y = A[l].x / A[l].rt;
return ;
}
//Divide them into 2 parts
int mid = (l + r) >> 1, x = l, y = mid + 1, cur = 1;
For(i, l, r)
if (A[i].id <= mid) t[x ++] = A[i]; else t[y ++] = A[i];
For(i, l, r) A[i] = t[i];
//Deal with the left part
CDQ(l, mid);
//Get the convex line of the left points
top = 0;
For(i, l, mid) {
//We can build the convex line directly because we have sort them before(At the last of this function)
while (top > 1 && Slope(stk[top - 1], stk[top]) < Slope(stk[top - 1], i) + eps) -- top;
//NOTICE: The '+ eps' is necessary or you will get only 80 points
stk[++ top] = i;
}
stk[++ top] = 0;//To deal with the dicision on the last element
//Update the right part with the convex line
For(i, mid + 1, r) {
while (cur < top && Slope(stk[cur], stk[cur + 1]) > A[i].sl) ++ cur;
chkmax(f[A[i].id], A[stk[cur]].x * A[i].a + A[stk[cur]].y * A[i].b);
}
//Deal with the right part
CDQ(mid + 1, r);
//Sort according to x
x = l, y = mid + 1;
For(i, l, r)
if (x == mid + 1) t[i] = A[y ++];
else if (y == r + 1) t[i] = A[x ++];
else if (A[x].x < A[y].x || (A[x].x - A[y].x < eps && A[x].y < A[y].y)) t[i] = A[x ++];
else t[i] = A[y ++];
For(i, l, r) A[i] = t[i];
}
int main()
{
File();
n = read(), f[0] = read();
For(i, 1, n)
scanf("%lf%lf%lf", &A[i].a, &A[i].b, &A[i].rt),
A[i].sl = -A[i].a / A[i].b, A[i].id = i;
sort(A + 1, A + 1 + n);
CDQ(1, n);
printf("%.3lf\n", f[n]);
return 0;
}
//山外青山楼外楼,西湖歌舞几时休?
// -- 林升《题临安邸》