More than 2019 cattle off summer school camp (tenth field) J - Wood Processing (slope optimization DP)

> Portal <

The meaning of problems


$ $ A n-width $ w_ {i} $, height $ h_ {i} $ of wood, requires $ k $ into groups, each group for all the pieces of wood, have become the height of the lowest block group height, width remains constant, the change in minimum area required.

analysis


Depending $ $ DP writing different state definitions given here A Solution

Height relatively high wood to accommodate the relatively low height (for $ dp $ equations and optimization), the first plank in accordance with the height of the sort from high to low

Assuming that $ d [i] [k] $ is the maximum area of ​​the front $ I $ th divided $ k $ parts can be retained, then the answer is $ [n] tot-d [k] $ ($ tot $ initial total area )

Consider how transfer

               $d[i][k]=max(d[j][k-1]+(pre[i]-pre[j])\cdot h[i])$

Wherein the pre $ $ prefix and width, i.e. $ pre [i] = \ sum_ {1} ^ {i} w [i] $.

Violence transfer high complexity ($ O (n ^ {2}) $), consider how to optimize. (Is not that a slope optimization Well)

We set $ j_ {1} <j_ {2} <i $ and computing $ dp [i] [k] $ when decision $ j_ {2} $ better, that is to say

                $d[j_{1}][k−1]+(pre[i]−pre[j_{1}])\cdot h[i]<d[j_{2}][k−1]+(pre[i]−pre[j_{2}])\cdot h[i] $

In this case $ j_ {1} $ can be omitted from the centralized decision-making, as the latter $ j_ {2} $ than $ j_ {1} $ better.

The equation can be simplified to

              $\frac{d[j_{2}][k-1]-d[j_{1}][k-1]}{pre[j_{2}]-pre[j_{1}]}$>$h[i]$

Then we set a monotonically decreasing maintenance decision on it

Code

#include <bits/stdc++.h>
#define empty (head>=tail)
#define ll long long
using namespace std;
const int maxn = 5e3+10, maxk = 2e3+10;
int n, k, head, tail, j;
ll pre[maxn], d[maxn][maxk], q[maxn];
struct node{int w ,h;}a[maxn];
bool cmp(node a, node b){return a.h > b.h;}
long double slope(int x, int y, int p) {
    return (long double)(d[y][p-1]-d[x][p-1])/(pre[y]-pre[x]);
}
int main()
{
    scanf("%d%d", &n, &k);
    ll sum = 0;
    for (int i = 1; i<= n; i++) {
        scanf("%d%d", &a[i].w, &a[i].h);
        sum += a[i].h * a[i].w;
    }
    sort(a+1, a+1+n, cmp);
    for (int i = 1; i <= n; i++) pre[i] = pre[i-1] + a[i].w;
    for(int p = 1; p <= k; p++) {
        head = tail = 1;
        for (int i = 1; i <= n; i++) {
            while(!empty&&slope(q[head],q[head+1],p)>a[i].h) head++;
            j = q[head]; d[i][p] = d[j][p-1]+a[i].h*(pre[i]-pre[j]);
            while(!empty&&slope(q[tail],q[tail-1],p)<slope(q[tail],i,p)) tail--;
            q[++tail] = i;
        }
    }
    printf("%lld\n", sum-d[n][k]);
    return 0;
}
View Code

 

Think


There began sorting board height, can think should direct thought, did not think it should be written transfer equation, we found that after ordering the transfer equation is better written, and will need to be optimized to care about the sort of direction . The original blogger blog has been written very good, but I think this kind of thing still have to look at a variety of blog, this will give you a broad idea and some contrast. His topic about $ dp $ optimization slope, the writing is due $ A [i] $ is monotonically increasing / decreasing, so is the decision to maintain a set increment / decrement, but I am very good at writing related topics blog saw that bloggers are given to explain why a convex hull maintenance / female package, but did not say that because that is the reason above. Also this problem, because the convex hull seen maintenance, maintenance on the analogy of thought concave package principles are similar, but after going to write or concave package related topics ( legislation Flag ). Where there is confusion encountered, again turned back to see the big rice cakes blog there are other people blog, it will slowly feeling a little clearer, $ go \ on $ ~

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11409178.html