codeforces765-div2 C(dp一生之敌)

题目
题意: 在大小为0-m的一维数轴上,给定n个柱子xi,每个点均有其限速ai.即在x[i]到x[i+1]一段速度为ai.现在可以删除至多 k个柱子,求从0到m的最短时间。(0处必有柱子,且不能删除)
思路: n = 500.猜测是一个n^3的算法,dp.
dp[i][j]表示前i个柱子,删除j个,且不删除第i个的最小花费.
dp2[i][j]表示前i个柱子,删除j个,且删除第i个的最小花费.
此处的最小花费是前从0到i+1的最小花费,不然不好写,绕了半天才看懂.

dp2[i][j] = min(dp2[i-1][j-1],dp[i-1][j-1]) + a[i] * (x[i+1] - x[i])
dp[i][j] = min(dp[i][j],dp2[t][i-t] + a[t] * (a[x+1]-x[i-t+1]) *), 1 <= t <= j

时间复杂度: O(n^3)
代码:

// Problem: C. Road Optimization
// Contest: Codeforces - Codeforces Round #765 (Div. 2)
// URL: https://codeforces.com/contest/1625/problem/C
// Memory Limit: 128 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 502;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int x[N];
int a[N];
int dp[N][N]; //不删除第i个
int dp2[N][N]; //删除第i个
void solve()
{
   mem(dp,0x3f);
   mem(dp2,0x3f);
   int ans = 0x3f3f3f3f;
   cin>>n>>m>>k;
   for(int i=1;i<=n;++i) cin>>x[i];
   for(int i=1;i<=n;++i) cin>>a[i];
   x[n+1] = m;
   if(n == 1)
   {
   	 ans = (m - x[1]) * a[1];
   	 cout<<ans; return ;
   }
   dp[1][0] = dp2[1][0] = (x[2]-x[1]) * (a[1]);
   for(int i=2;i<=n;++i)
   {
   	  for(int j=0;j<=min(i-1,k);++j) //删除多少个
   	  {
   	  	//不删第i个
   	  	dp[i][j] = min(dp2[i-1][j],dp[i-1][j]) + (x[i+1] - x[i]) * a[i];
   	  	// if(i == 2 && j == 0) cout<<x[i+1]<<" "<<a[i]<<endl;
   	  	for(int t=1;t<=j;++t) //删第i个,枚举从哪个转移
   	  	{
   	  		// int de = i - t;
   	  		dp2[i][j] = min(dp2[i][j],dp[i-t][j-t] + a[i-t] * (x[i+1]-x[i-t+1]));
   	  	}
   	  	// cout<<i<<" "<<j<<":"<<dp2[i][j]<<endl;
   	  }
   }
   for(int j=0;j<=k;++j) ans = min({ans,dp[n][j],dp2[n][j]});
   cout<<ans;
}
signed main(void)
{  
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}
上一篇:【剑指 Offer II】 090. 环形房屋偷盗


下一篇:蓝桥杯3(DP最短路径覆盖问题:老爷机)