protagonist

Wood Processing

链接:https://ac.nowcoder.com/acm/contest/890/J
来源:牛客网

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

In the wood industry, it is very common to make big wood boards by combining planks. To combine several planks into boards, the carpenter may cut some of the planks horizontally and discard one of the two parts, such that the heights of all planks are equal. Then, the planks are joined together, forming a big wood board. The height of the board is the common height of the planks, and the width of the board is the sum of the widths of the planks.

However, cutting planks may result in huge wastes. The problem is, given n planks, determine the minimum total wasted area of planks to make k boards from these planks. You may freely reorder and combine these planks. Note that the mechanical properties of a plank are anisotropic, so you can't rotate the planks. Also, all planks must be used; you cannot discard any whole plank.

输入描述:

The first line of the input contains two integers n, k (1≤n≤5000,1≤k≤2000,k≤n)(1 \leq n \leq 5000, 1 \leq k \leq 2000, k \leq n)(1n5000,1k2000,kn), denoting the number of planks given and the number of boards to make.

Each of the remaining n lines contains two integers w, h (1≤w,h≤107)(1 \leq w, h \leq 10^7)(1w,h107), denote the width and the height of a given plank.

输出描述:

Output the minimum wasted area as an integer.
示例1

输入

复制
3 2
3 3
4 7
2 5

输出

复制
4
示例2

输入

复制
6 3
1 1
1 2
1 3
1 4
1 5
1 6

输出

复制
3
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn=1e5+10;
struct node{
    ll w,h;
}c[maxn];
ll n,k;
ll sum[maxn],e[maxn];
ll val[5006][5006];
ll dp[5006][5006];
bool cmp(node s,node t){
    return s.h<t.h;
}

void solve(ll cur,ll l,ll r,ll ql,ll qr){
    if(l>r)return;
    ll mid=l+r>>1;
    ll q=0;
    for(register ll i=ql;i<mid&&i<=qr;++i){
        ll to=dp[cur-1][i]+val[i+1][mid];
        if(to>dp[cur][mid]){
            q=i;
            dp[cur][mid]=to;
            //cout<<"debug "<<dp[cur][mid]<<endl;
        }
    }
    solve(cur,l,mid-1,ql,q);
    solve(cur,mid+1,r,q,qr);
}
int main()
{
    //freopen("1.txt","r",stdin);
    scanf("%lld%lld",&n,&k);
    for(register ll i=1;i<=n;++i){
        scanf("%lld%lld",&c[i].w,&c[i].h);
    }
    sort(c+1,c+1+n,cmp);
    for(register ll i=1;i<=n;++i){
        sum[i]=sum[i-1]+c[i].w*c[i].h;
        e[i]=e[i-1]+c[i].w;
        //cout<<"debug sum[i]="<<sum[i]<<endl;
    }
    for(register ll i=1;i<=n;++i){
        for(register ll j=i;j<=n;++j){
            val[i][j]=(e[j]-e[i-1])*c[i].h;
        }
    }
    for(register ll i=1;i<=n;++i){
        dp[1][i]=val[1][i];
    }
    for(register ll i=2;i<=k;++i){
        solve(i,i,n,i-1,n-1);
    }
    printf("%lld\n",sum[n]-dp[k][n]);
    return 0;
}

 

posted @ 2019-08-18 09:35  czy-power  阅读(276)  评论(0编辑  收藏  举报