Interval DP-2017 ACM-ICPC Beijing Division - J - Pangu and Stones

2017 ACM-ICPC Beijing Division - J - Pangu and Stones

answer:

One so I am very excited about the title. As a DP range just started two days can sprout new push on this question and had questions.

Closer to home:

1, set: l, r answer merged into the stack section stone programs k.
2, Properties: minimum.
3, state: l, r, k (heap) (sometimes last point may also be a state)
4, last point: we enumerate each point as a time interval division of the answers are likely to differ, so last point is any point in the interval.
According to last point and then we began to write the state transition equation:
P for the allocation.

f [l] [r] [k] = min (f [l] [r] [k], b [l] [p] [k-1], b [p + 1] [s] [1])

This is why the k-1 and k it? We carefully think about, we think about our k-1 molecules can not set minimum heap piles merger is not up? Right! So in fact we k by k-1 stack and heap another bunch of merging, so after that we successfully communicated merged into K heap.
So if we meet in the title k [l, r] within this range, it is not something we can directly put k heap merge into a pile, and our k heap merge at the cost of some! He is the range of weights and so we need to begin with the prefix and what pre-treatment. Then we completed the merger k heap into a pile of tasks! Then find the answers directly on it.

Synthesis pile equation f [l] [r] [1] = min (..., f [l] [r] [k] + sum [r] -sum [l-1])

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e2+7;
const int inf=1e9+7;
int dp[N][N][N];
int a[N],sum[N];
int main(){
    int n,l,r;
    while(scanf("%d%d%d",&n,&l,&r)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++)
                    dp[i][j][k]=inf;
        for(int len=1;len<=n;len++){
            for(int i=1;i+len-1<=n;i++){
                int j=i+len-1;
                dp[i][j][len]=0;
                for(int k=2;k<=len;k++){
                    for(int p=i;p<=j;p++)
                        dp[i][j][k]=min(dp[i][j][k],dp[i][p][1]+dp[p+1][j][k-1]);
                    if(k>=l&&k<=r)
                        dp[i][j][1]=min(dp[i][j][1],dp[i][j][k]+sum[j]-sum[i-1]);
                }
            }
        }
        if(dp[1][n][1]>=inf) printf("0\n");
        else printf("%d\n",dp[1][n][1]);
    }
}
Published 92 original articles · won praise 6 · views 1178

Guess you like

Origin blog.csdn.net/weixin_42979819/article/details/103899960