BZOJ 1597: [Usaco2008 Mar]土地购买

次元传送门

题意:
中文不解释

分析:
DP -> 60%
DP+斜率优化 -> AC

代码实现

/************************************************************** Problem: 1597 User: YOUSIKI Language: C++ Result: Accepted Time:148 ms Memory:3424 kb ****************************************************************/

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

const int N = 50005;

inline long long nextInt(void) {
    long long ans;
    scanf("%lld", &ans);
    return ans;
}

struct Data {
    long long x, y;

    void readData(void) {
        x = nextInt();
        y = nextInt();
    }

}a[N], d[N];

inline bool cmpData(Data a, Data b) {
    return a.x < b.x;
}

long long f[N];

inline double slop(int i, int j) {
    return (double)(f[j] - f[i]) / (a[i + 1].y - a[j + 1].y);
}

int que[N], l = 0, r = 0;

signed main(void) {
    long long n = nextInt(), tot = 0;
    for (int i = 1; i <= n; i++)
        d[i].readData();
    sort(d + 1, d + n + 1, cmpData);
    for (int i = 1; i <= n; i++) {
        while (tot&&d[i].y >= a[tot].y)
            tot--;
        a[++tot] = d[i];
    }
    for (int i = 1, top; i <= tot; i++) {
        while (l < r && slop(que[l], que[l + 1]) < a[i].x)
            l++;
        top = que[l];
        f[i] = f[top] + a[top + 1].y*a[i].x;
        while (l < r && slop(que[r], i) < slop(que[r - 1], que[r]))
            r--;
        que[++r] = i;
    }
    printf("%lld", f[tot]);
}


By YOUSIKI

你可能感兴趣的:(BZOJ 1597: [Usaco2008 Mar]土地购买)