BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间【DP】

1642: [Usaco2007 Nov]Milking Time 挤奶时间

【题目描述】
传送门

【解题报告】

我们可以发现时间最大是106,那么完全可以写一个以时间为下标的DP。

代码如下

#include<cstdio>
#include<algorithm>
using namespace std;
int Len,n,R,f[2000005],NowMax=0;
struct xcw{
    int L,R,w;
    bool operator <(const xcw b)const{return L<b.L;}
}a[1005];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d%d%d",&Len,&n,&R);
    for(int i=1;i<=n;i++) scanf("%d%d%d",&a[i].L,&a[i].R,&a[i].w);
    sort(a+1,a+1+n);
    for(int i=0,j=1;i<=Len;i++){
        NowMax=max(NowMax,f[i]);
        for(;a[j].L==i&&j<=n;j++) f[a[j].R+R]=max(NowMax+a[j].w,f[a[j].R+R]);
    }
    for(int i=Len+1;i<=Len+R;i++) NowMax=max(NowMax,f[i]);
    printf("%d\n",NowMax);
    return 0;
}
posted @ 2018-06-02 11:59  XSamsara  阅读(144)  评论(0编辑  收藏  举报