game 游戏 (斜率优化dp)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w4149/article/details/78253749

game 游戏

10.16

思路:
f[i][j]表示前i个数分j段并全部获得的最小期望步数
f[i][j] = f[i-1][k] + cal(k+1, i) cal(k+1, i)表示k+1到i这一段分成一个部分并获得的最小期望步数
cal(k+1, i) = singma( (sum[x]-sum[k]) / t[x] ) ( k+1 <= x <= i )
= singma( sum[x] / t[x] ) - sum[k] * singma( 1.0 / t[x] ) ( k+1 <= x <= i )
sum[i] = singma(t[i])
rev[i] = singma(1.0 / t[i])
exc[i] = singma(sum[i] / t[i])
cal(k+1, i) = exc[i] - exc[k] - sum[k] * (rev[i] - rev[k])
f[a][j-1] + exc[i] - exc[a] - sum[a] * (rev[i] - rev[a])
< f[b][j-1] + exc[i] - exc[b] - sum[b] * (rev[i] - rev[b])
f[a][j-1] - f[b][j-1] + exc[b] - exc[a] + sum[a] * rev[a] - sum[b] * rev[b]
< sum[a] * rev[i] - sum[b] * rev[i]
f[a][j-1] - exc[a] + sum[a] * rev[a] - f[b][j-1] + exc[b] - sum[b] * rev[b]
< sum[a] * rev[i] - sum[b] * rev[i]
f[a][j-1] - exc[a] + sum[a] * rev[a] - f[b][j-1] + exc[b] - sum[b] * rev[b]) / (sum[a] - sum[b])
< rev[i]

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 200010
using namespace std;

int n, K;
int q[N], head, tail;
double exc[N], sum[N], rev[N];
double t[N];
double f[N][55];

inline double gety(int x, int p){
    return f[x][p] - exc[x] + sum[x] * rev[x];
}

inline double getk(int j, int k, int p){
    //return (gety(j, p) - gety(k, p)) / (sum[j] - sum[k]);
    return gety(j, p) - gety(k, p);
}

inline double cal(int k, int i){
    return exc[i] - exc[k] - sum[k] * (rev[i] - rev[k]);
}

inline void add(int i, int k){
    while(head < tail-1 && getk(i, q[tail-1], k) * (sum[q[tail-1]] - sum[q[tail-2]]) < getk(q[tail-1], q[tail-2], k) * (sum[i] - sum[q[tail-1]])) tail--;
    q[tail++] = i;
}

inline int getmax(int i, int k){
    while(head < tail-1 && getk(q[head+1], q[head], k) < rev[i] * (sum[q[head+1]] - sum[q[head]])) head++;
    return q[head];
}

int main(){
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    scanf("%d%d", &n, &K);
    for(register int i=1; i<=n; ++i) scanf("%lf", &t[i]);
    for(register int i=1; i<=n; ++i){
        sum[i] = sum[i-1] + t[i];
        rev[i] = rev[i-1] + 1.0 / t[i];
        exc[i] = exc[i-1] + sum[i] / t[i];
        f[i][1] = exc[i];
    }
    for(int j=2; j<=K; ++j){
        head = tail = 0;
        q[tail++] = 0;
        for(register int i=1; i<=n; ++i) add(i, j-1);
        for(register int i=1; i<=n; ++i){
            if(i >= j){
                int k = getmax(i, j-1);
                f[i][j] = f[k][j-1] + cal(k, i);
            }
            else f[i][j] = 1e50;
        }
    }
    printf("%.2f\n", f[n][K]);
    return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页