• 【Luogu】P1868饥饿的奶牛(DP)


      题目链接

      话说我存一些只需要按照一个关键字排序的双元素结构体的时候老是喜欢使用链式前向星……

      DP。f[i]表示前i个位置奶牛最多能吃到的草。转移方程如下:  

    f[i]=f[i-1];
    f[i]=max(f[i],f[x[j]-1]+y[j]-x[j]+1);

      其中j满足y[j]=i。

      代码如下:

      

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    #include<algorithm>
    inline long long read(){
        long long num=0,f=1;
        char ch=getchar();
        while(!isdigit(ch)){
            if(ch=='-')    f=-1;
            ch=getchar();
        }
        while(isdigit(ch)){
            num=num*10+ch-'0';
            ch=getchar();
        }
        return num*f;
    }
    
    struct Edge{
        int next,to;
    }edge[1001010];
    int head[3000010],num;
    inline void add(int from,int to){
        edge[++num]=(Edge){head[from],to};
        head[from]=num;
    }
    
    int f[3000010];
    int m;
    
    int main(){
        int n=read();
        for(int i=1;i<=n;++i){
            int x=read(),y=read();
            add(y,x);
            m=std::max(m,y);
        }
        for(int i=1;i<=m;++i){
            f[i]=f[i-1];
            for(int s=head[i];s;s=edge[s].next){
                int x=edge[s].to;
                f[i]=std::max(f[i],f[x-1]+i-x+1);
            }
        }
        printf("%d",f[m]);
        return 0;
    }
  • 相关阅读:
    LeetCode
    数据流中的中位数
    二叉搜索树的第k个结点
    对称的二叉树
    按之字形顺序打印二叉树
    把二叉树打印成多行
    二叉树的下一个结点
    链表中环的入口结点
    删除链表中重复的结点
    不用加减乘除做加法
  • 原文地址:https://www.cnblogs.com/cellular-automaton/p/7657304.html
Copyright © 2020-2023  润新知