https://codeforces.com/contest/1625/problem/C
C. Road Optimization
time limit per test3 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard output
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 x (0≤x≤ℓ), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate 0, and Olymp City is located in the point with coordinate ℓ.
There are n signs along the road, i-th of which sets a speed limit ai. This limit means that the next kilometer must be passed in ai 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 0), 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 3⋅5+1⋅8+4⋅3+2⋅6=47 minutes.
To optimize the road traffic, the Government of Mars decided to remove no more than k 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.
Input
The first line contains three integers n, ℓ, k (1≤n≤500, 1≤ℓ≤105, 0≤k≤n−1), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove.
The second line contains n integers di (d1=0, di<di+1, 0≤di≤ℓ−1) — coordinates of all signs.
The third line contains n integers ai (1≤ai≤104) — speed limits.
Output
Print a single integer — minimal possible time to drive from Kstolop to Olymp City in minutes, if you remove no more than k road signs.
Examples
inputCopy
4 10 0
0 3 4 8
5 8 3 6
outputCopy
47
inputCopy
4 10 2
0 3 4 8
5 8 3 6
outputCopy
38
Note
In the first example, you cannot remove the signs. So the answer is 47, as it’s said in the statements above.
In the second example, you may remove the second and the fourth sign. In this case, you need to drive four kilometers in 4⋅5=20 minutes, and then six kilometers in 6⋅3=18, so the total time is 4⋅5+6⋅3=38 minutes.
在一维坐标轴上给你n个牌子的位置和限速,去掉最多k块牌子,问最短从0到终点时间。
动态规划f[i][j],i表示在第i个位置,j表示前边已经选择了j个牌子,f为时间最小值。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 100;
int n, l, k;
int d[N];
int a[N];
int f[N][N];
int main()
{
scanf("%d%d%d", &n, &l, &k);
for (int i = 1; i <= n; i++)
scanf("%d", d + i);
d[n + 1] = l;
for (int i = 1; i <= n; i++)
scanf("%d", a + i);
memset(f, 0x3f, sizeof f);
f[1][0] = 0;
for (int i = 1; i <= n + 1; i++){
for (int j = 0; j < i; j++){
for (int x = 1; x < i; x++){
f[i][j] = min(f[i][j], f[x][j - 1] + a[x] * (d[i] - d[x]));
}
}
}
int res= 0x3f3f3f3f;
for(int i=n-k;i<=n;i++){
res=min(res,f[n+1][i]);
}
printf("%d\n",res);
return 0;
}