zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    Linux RAID部署
    系统运维架构师体系
    Linux系统上文件的特殊权限及文件acl
    Linux磁盘使用及文件系统管理
    中小规模网站架构组成
    优化配置模板主机
    网络原理基础
    MySQL二进制安装
    网络通讯基础
    点击改变背景
  • 原文地址:https://www.cnblogs.com/cellular-automaton/p/7657304.html
Copyright © 2011-2022 走看看