[USACO12OPEN]书架Bookshelf

线段树优化dp

第一眼看上去:这不是傻逼区间dp吗?(flag1)

code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
#define int long long 
using namespace std;
const int maxn=400006;
int n,l,f[maxn];
struct hzw
{
  int h;
  int w;
}bk[maxn];
int sum[maxn];
signed main()
{
    cin>>n>>l;
    for (int i=1;i<=n;++i)
    {
        scanf("%lld%lld",&bk[i].h,&bk[i].w);
        sum[i]=sum[i-1]+bk[i].w;
    }
    for (int i=1;i<=n;++i) f[i]=1e17;
    f[0]=0,f[1]=bk[1].h;
    for (int i=2;i<=n;++i)
    {
        for (int j=i-1;j>=0;--j)
        {
           if (sum[i]-sum[j]>l) break;
           int tmp=0;
           for (int k=j+1;k<=i;++k) tmp=max(tmp,bk[k].h);
           f[i]=min(f[i],f[j]+tmp);
        }
    }
    cout<<f[n];
    return 0;  
}

结果:

posted @ 2018-09-27 21:13  Splitor  阅读(246)  评论(0编辑  收藏  举报