zoukankan      html  css  js  c++  java
  • 【刷题】【cf】 C. Road Optimization

    The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet.

    One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i. e. the path from Olymp City to Kstolop).

    The road from Kstolop to Olymp City is  kilometers long. Each point of the road has a coordinate xx (0x0≤x≤ℓ), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate 00, and Olymp City is located in the point with coordinate .

    There are nn signs along the road, ii-th of which sets a speed limit aiai. This limit means that the next kilometer must be passed in aiai minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i. e. in the point with coordinate 00), which sets the initial speed limit.

    If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example:

    Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is 35+18+43+26=473⋅5+1⋅8+4⋅3+2⋅6=47 minutes.

    To optimize the road traffic, the Government of Mars decided to remove no more than kk road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible.

    The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above.

    .

    思路:显然dp

    有影响的变量三个,目前执行到第i个路标,这之前最后一个未删除的路标是j,总共删除了k个

    我思路比较曲折,用的与最初不删除相比,减少了多少时间作为dp的值,实际上直接用到第i+1个站所花的时间就行

    写的过程卡了很久,出现了以下问题:

    1.空间爆了,long long 类型500*500*500,MLE

    2.从不合法状态移动到合法状态,答案出错

    3.k-1不小心越界了

    4.文字注释,cf上无法提交...

    #include<cstdio>
    #include<cstdlib>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #define ll long long
    using namespace std;
    inline int read()
    {
        int x=0,f=1;char c=getchar();
        while(c<'0' || c>'9' ) 
        {
            if(c=='-' ) f=-1;
            c=getchar();
        }
        while(c>='0'&&c<='9' ) x=(x<<3)+(x<<1)+c-'0',c=getchar();
        return x*f;
    }
    
    int n,l,K;
    const int N=600,INF=0x3f3f3f3f;
    int d[N],a[N];
    int sum,dp[2][N][N];
    //压缩一个空间 
    int main()
    {
        n=read(),l=read(),K=read();
        for(int i=1;i<=n;i++) d[i]=read(),d[i-1]=d[i]-d[i-1];
        d[n]=l-d[n];
        for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i]*d[i];
        
        int nw=1,pre=0;
        for(int k=0;k<=K;k++)
            dp[1][0][k]=dp[1][1][k]-INF;
        dp[1][1][0]=0;//注意状态的合法性,只能从合法状态转移到合法状态
        //如果都设置0,dp数组会偏大 
        for(int i=2;i<=n;i++)
        {
            pre=nw;
            nw=(i%2) ;
            for(int j=1;j<=i;j++)
                for(int k=0;k<=K;k++)
                    dp[nw][j][k]=-INF;
            
            for(int j=1;j<i;j++)
                for(int k=0;k<=K;k++)
                {
                    dp[nw][i][k]=max(dp[nw][i][k] ,dp[pre][j][k] );
                    if(k>0)
                        dp[nw][j][k]=max(dp[nw][j][k] ,dp[pre][j][k-1]+(a[i]-a[j])*d[i] );
                }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            for(int k=0;k<=K;k++)
                ans=max(ans,dp[nw][i][k]);
        printf("%d\n",sum-ans);
        
        return 0;
    } 
    View Code

     

    C. Road Optimization

  • 相关阅读:
    每日日报
    每日日报
    java笔记
    每日日报
    每日日报
    每日日报
    查看当前mysql时区 并设置为北京时间
    springboot 指定配置文件启动, 区分开发和线上分支
    Js Contains方法
    vue $refs的基本用法
  • 原文地址:https://www.cnblogs.com/xwww666666/p/15805645.html
Copyright © 2011-2022 走看看