【刷题】【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
There are
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
To optimize the road traffic, the Government of Mars decided to remove no more than
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#include #include #include #include #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) 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; }
C. Road Optimization