NEU1579(斜率优化)

2015辽宁省赛的一道题,题意是给定n个人的坐标,要炸死他们,每一个炸弹可以投在任意位置炸死半径范围为R(R自己决定)的所有人,代价为a*r*r+b*r+c,问炸死所有人的最小代价。

显然DP[i] = min (DP[j-1]+a*R*R+b*R+c) 其中R = (r[i]-r[j])/2.0, 这里j<i,但是j可能等于i,所以在斜率优化中增加一句dp[i] = min (dp[i], dp[i-1]+c)。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 111111
#define sum r

double sum[maxn];
double dp[maxn];
int n;
int que[maxn], head, tail;
double a, b, c;

double up (int i, int j) {
    return dp[i-1]+a/4.0*r[i]*r[i]-b/2.0*r[i] - (dp[j-1]+a/4.0*r[j]*r[j]-b/2.0*r[j]);
}

double down (int i, int j) {
    return a/2.0*(r[i]-r[j]);
}

int main () {
    while (scanf ("%d", &n) == 1) {
        scanf ("%lf%lf%lf", &a, &b, &c);
        for (int i = 1; i <= n; i++)
            scanf ("%lf", &r[i]);
        sort (r+1, r+n+1);
        head = tail = 0;
        que[tail++] = 0;
        memset (dp, 0, sizeof dp);
        for (int i = 1; i <= n; i++) {
            while (head+1<tail && up (que[head+1], que[head]) <= sum[i]*down (que[head+1], que[head]))
                head++;
            int j = que[head];
            double R = (r[i]-r[j])/2.0;
            dp[i] = dp[j-1] + a*R*R + b*R + c;
            dp[i] = min (dp[i], dp[i-1]+c);
            while (head+1<tail && up (i, que[tail-1])*down (que[tail-1], que[tail-2]) <= up (que[tail-1], que[tail-2])*down (i, que[tail-1]))
                tail--;
            que[tail++] = i;
        }
        printf ("%.5lf\n", dp[n]);
    }
    return 0;
}


你可能感兴趣的:(NEU1579(斜率优化))