洛谷P4027 [NOI2007]貨幣兌換(dp 斜率優化 cdq 二分)

自為風月馬前卒發表於2019-01-01

題意

題目連結

Sol

解題的關鍵是看到題目裡的提示。。。

(f[i])表示到第(i)天所持有軟妹幣的最大數量,顯然答案為(max_{i = 1}^n f[i])

轉移為(f_i = max(f_{i – 1}, A_i frac{f_j R_j}{A_j R_j + B_j} + B_i frac{f_j}{A_j R_j + B_j}))

變形一下:

[frac{f_j}{B_i} – frac{f_j}{A_j R_j + B_j} = frac{A_i}{B_i} frac{f_j R_j}{A_j R_j + B_j}]

(y_i = frac{f_j}{A_j R_j + B_j}, x_i = frac{f_j R_j}{A_j R_j + B_j})

顯然可以斜率優化,也就是拿一條斜率為(-frac{A_i}{B_i})的直線從上往下切。

但是這裡的斜率和(x)都是不單調的。

按照老祖宗說的

(x)不單調cdq

斜率不單調二分凸包

然後xjb寫一寫就好了。我寫的cdq複雜度是(O(nlog^2n))的,每次暴力建左側的凸包,然後在右邊二分,雖然很好寫,但是在BZOJ上成功T飛。。

看了下SovietPower大佬的部落格發現有nlogn的做法Orz,就是先按斜率排序,然後轉移的時候把下標分為(<mid)(>= mid)的,直接用類似雙指標的東西掃就行了

#include<bits/stdc++.h> 
#define Pair pair<double, double>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define db  double
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << `
`;}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N, S;
struct Sta {
    int id;
    db A, B, R, x, y, f;
    void Get() {
        x = f * R / (A * R + B);
        y = f / (A * R + B);
    }
    bool operator < (const Sta &rhs) const {
        return f < rhs.f;
    }
}a[MAXN], st[MAXN];
vector<Pair> v;
double GetK(Pair a, Pair b) {
    if((b.fi - a.fi) < eps) return INF;
    return (b.se - a.se) / (b.fi - a.fi);
}
void GetConvexHull(int l, int r) {
    v.clear();
    for(int i = l; i <= r; i++) {
        double x = a[i].x, y = a[i].y;
        while(v.size() > 1 && ((GetK(v[v.size() - 1], MP(x, y)) > GetK(v[v.size() - 2], v[v.size() - 1])) )) v.pop_back();
        v.push_back(MP(x, y));
    }
}
int cnt = 0;
db Find(int id, db k) {
    int l = 0, r = v.size() - 1, ans = 0;
    while(l <= r) {
        int mid = l + r >> 1;
        if((mid == 0) || (GetK(v[mid - 1], v[mid]) > k)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    return a[id].A * v[ans].fi + a[id].B * v[ans].se;
}
db CDQ(int l, int r) {
    if(l == r) {
        int i = l;
        chmax(a[i].f, a[i - 1].f); 
        chmax(a[i].f, a[i].f * ((a[i].A * a[i].R + a[i].B) / (a[i].A * a[i].R  + a[i].B)));
        a[l].Get(); 
        return a[i].f;
    }
    int mid = l + r >> 1;
    db lmx = CDQ(l, mid); 
    GetConvexHull(l, mid);
    for(int i = mid + 1; i <= r; i++) chmax(a[i].f, max(lmx, Find(i, -a[i].A / a[i].B)));
    CDQ(mid + 1, r);
    int tl = l, tr = mid + 1, tot = tl - 1;
    while(tl <= mid || tr <= r) {
        if((tr > r) || (tl <= mid && a[tl].x < a[tr].x)) st[++tot] = a[tl++];//這裡要加上tl <= mid 
        else st[++tot] = a[tr++];
    }
    db rt = 0;
    for(int i = l; i <= r; i++) a[i] = st[i], chmax(rt, a[i].f);
    return rt;
}
signed main() {
//  freopen("a.in", "r", stdin);
    N = read(); S = read();
    for(int i = 1; i <= N; i++) scanf("%lf %lf %lf", &a[i].A, &a[i].B, &a[i].R), a[i].id = i;
    a[0].f = S;
    CDQ(1, N);
    db ans = 0;
    for(int i = 1; i <= N; i++) chmax(ans, a[i].f);
    printf("%.3lf", ans);
    return 0;
}

相關文章