bzoj 1571: [Usaco2009 Open] Ski Lesson [dp]

Reference: https://blog.csdn.net/cgh_andy/article/details/52506738
I didn't get any important dp... It's inexplicable.
Note that a slope can be skied many times.
Let f[i][j] be the time The i ability is the maximum number of skiing times of j, the preprocessing l[i][j] is the latest start time to obtain the j ability at i, and w[i] is the shortest skiing time when the ability j is available.
Simulate the transfer.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=10005,M=105;
int l[N][M],n,t,m,f[N][M],w[M],g[N];  
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int main()  
{
    t=read(),m=read(),n=read();
    for(int i=0;i<=t;i++)
        for(int j=0;j<=100;j++)
            f[i][j]=-1e9;
    for(int i=0;i<=100;i++)
        w[i]=1e9;
    // memset(f,128,sizeof f);
    // memset(w,127/3,sizeof w);cerr<<f[0][0]<<" "<<f[0][1]<<" "<<w[0]<<" "<<w[1]<<endl;
    for(int i=1;i<=m;i++)  
    {
        int x=read(),y=read()+x,z=read();
        l[y][z]=max(l[y][z],x);  
    }  
    for(int i=1;i<=n;i++)  
    {  
        int x=read(),y=read();
        for(int j=x;j<=100;j++)
            w[j]=min(w[j],y);  
    }  
    f[0][1]=0;  
    for(int i=1;i<=t;i++)  
        for(int j=1;j<=100;j++)  
        {  
            f[i][j]=f[i-1][j];  
            if(l[i][j])
                f[i][j]=max(f[i][j],g[l[i][j]]);  
            if(i-w[j]>=0)
                f[i][j]=max(f[i][j],f[i-w[j]][j]+1);  
            g[i]=max(g[i],f[i][j]);  
        }  
    printf("%d\n",g[t]);  
    return 0;  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253103&siteId=291194637