1. 程式人生 > >【BZOJ2149】拆遷隊(斜率優化DP+CDQ分治)

【BZOJ2149】拆遷隊(斜率優化DP+CDQ分治)

題目:

BZOJ2149

分析:

先吐槽一下題意:保留房子反而要給賠償金是什麼鬼哦……

第一問是一個經典問題。直接求原序列的最長上升子序列是錯誤的。比如\(\{1,2,2,3\}\),選擇\(\{1,2,3\}\)不改變後會發現無論如何修改都無法變成一個嚴格上升序列。只能選擇\(\{1,2\}\),把原序列改成\(\{1,2,3,4\}\)

考慮對於兩個數\(a_i\)\(a_j(j<i)\)\(a_i\)能接在\(a_j\)後面的充要條件是\(a_i-a_j\geq i-j\)(這樣中間才能塞下\(i-j-1\)個數形成上升序列)。移項得到\(a_i-i\geq a_j-j\)

,所以應該把每個數減去它的編號作為權值然後求最長非降子序列。下面的程式碼展示了\(O(nlog_2n)\)求法(其中\(c[i]=a[i]-i\)\(f[i]\)表示以\(i\)結尾的最長非降子序列的長度)。

int solve()
{
    int ans = 0;
    memset(tmp, INF, sizeof(int[n + 1]));
    for (int i = 1; i <= n; i++)
    {
        if (c[i] < 0)
            f[i] = 0;
        else
        {
            int pos = upper_bound(tmp + 1, tmp + ans + 1, c[i]) - tmp;
            tmp[pos] = c[i];
            ans = max(ans, pos);
            f[i] = pos;
        }
        v[f[i]].push_back(i);
    }
    v[0].push_back(0);
    return ans;
}

未完待續...

程式碼:

#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <vector>
using namespace std;

namespace zyt
{
    template<typename T>
    inline void read(T &x)
    {
        char c;
        bool f = false;
        x = 0;
        do
            c = getchar();
        while (c != '-' && !isdigit(c));
        if (c == '-')
            f = true, c = getchar();
        do
            x = x * 10 + c - '0', c = getchar();
        while (isdigit(c));
        if (f)
            x = -x;
    }
    template<typename T>
    inline void write(T x)
    {
        static char buf[20];
        char *pos = buf;
        if (x < 0)
            putchar('-'), x = -x;
        do
            *pos++ = x % 10 + '0';
        while (x /= 10);
        while (pos > buf)
            putchar(*--pos);
    }
    typedef long long ll;
    typedef long double ld;
    const int N = 1e5 + 10, INF = 0x3f3f3f3f;
    const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
    int n, a[N], b[N], c[N], f[N], tmp[N];
    ll dp[N];
    vector<int> v[N];
    int solve()
    {
        int ans = 0;
        memset(tmp, INF, sizeof(int[n + 1]));
        for (int i = 1; i <= n; i++)
        {
            if (c[i] < 0)
                f[i] = 0;
            else
            {
                int pos = upper_bound(tmp + 1, tmp + ans + 1, c[i]) - tmp;
                tmp[pos] = c[i];
                ans = max(ans, pos);
                f[i] = pos;
            }
            v[f[i]].push_back(i);
        }
        v[0].push_back(0);
        return ans;
    }
    inline ll x(const int i)
    {
        return i - a[i];
    }
    inline ll y(const int i)
    {
        return dp[i] - (ll)(i + 1) * a[i] + (ll)i * (i + 1) / 2;
    }
    inline ld ratio(const int i, const int j)
    {
        if (x(i) == x(j))
            return y(i) < y(j) ? -LINF : LINF;
        else
            return (ld)(y(i) - y(j)) / (x(i) - x(j));
    }
    struct node
    {
        int pos;
        bool type;
        bool operator < (const node &b) const
        {
            return pos < b.pos;
        }
    }arr[N];
    const int CHANGE = 0, QUERY = 1;
    void CDQ(const int l, const int r)
    {
        if (l == r)
            return;
        int mid = (l + r) >> 1, i = l, j = mid + 1, k = l;
        static node tmp[N];
        static int st[N];
        CDQ(l, mid), CDQ(mid + 1, r);
        int top = 0;
        while (i <= mid && j <= r)
        {
            if (x(arr[i].pos) >= x(arr[j].pos))
            {
                if (arr[i].type == CHANGE)
                {
                    while (top > 1 && ratio(st[top - 2], st[top - 1]) < ratio(st[top - 1], arr[i].pos))
                        --top;
                    st[top++] = arr[i].pos;
                }
                tmp[k++] = arr[i++];
            }
            else
            {
                if (arr[j].type == QUERY && top)
                {
                    int l = 0, r = top - 2, ans = top - 1;
                    while (l <= r)
                    {
                        int mid = (l + r) >> 1;
                        if (ratio(st[mid], st[mid + 1]) < arr[j].pos)
                            r = mid - 1, ans = mid;
                        else
                            l = mid + 1;
                    }
                    dp[arr[j].pos] = min(dp[arr[j].pos], 
                        dp[st[ans]] + 
                        (ll)((a[st[ans]] << 1) + arr[j].pos - st[ans]) * (arr[j].pos - st[ans] - 1) / 2
                            + a[arr[j].pos] + b[arr[j].pos]);
                }
                tmp[k++] = arr[j++];
            }
        }
        while (i <= mid)
            tmp[k++] = arr[i++];
        while (j <= r)
        {
            if (arr[j].type == QUERY && top)
            {
                int l = 0, r = top - 2, ans = top - 1;
                while (l <= r)
                {
                    int mid = (l + r) >> 1;
                    if (ratio(st[mid], st[mid + 1]) < arr[j].pos)
                        r = mid - 1, ans = mid;
                    else
                        l = mid + 1;
                }
                dp[arr[j].pos] = min(dp[arr[j].pos], 
                    dp[st[ans]] + 
                    (ll)((a[st[ans]] << 1) + arr[j].pos - st[ans]) * (arr[j].pos - st[ans] - 1) / 2
                        + a[arr[j].pos] + b[arr[j].pos]);
            }
            tmp[k++] = arr[j++];
        }
        memcpy(arr + l, tmp + l, sizeof(node[r - l + 1]));
    }
    int work()
    {
        read(n);
        for (int i = 1; i <= n; i++)
            read(a[i]), c[i] = a[i] - i;
        for (int i = 1; i <= n; i++)
            read(b[i]);
        a[++n] = INF;
        c[n] = INF;
        int ans = solve();
        write(ans - 1), putchar(' ');
        memset(dp, INF, sizeof(ll[n + 1]));
        dp[0] = 0;
        for (int i = 1; i <= ans; i++)
        {
            int cnt = 0;
            for (int j = 0; j < v[i - 1].size(); j++)
                if (dp[v[i - 1][j]] < LINF)
                    arr[++cnt] = (node){v[i - 1][j], CHANGE};
            for (int j = 0; j < v[i].size(); j++)
                arr[++cnt] = (node){v[i][j], QUERY};
            sort(arr + 1, arr + cnt + 1);
            CDQ(1, cnt);
        }
        write(dp[n] - a[n] - b[n]);
        return 0;
    }
}
int main()
{
    return zyt::work();
}