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 𝑥 (0≤𝑥≤ℓ), 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 𝑛 signs along the road, 𝑖-th of which sets a speed limit 𝑎𝑖. This limit means that the next kilometer must be passed in 𝑎𝑖 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 𝑘 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 𝑛, ℓ, 𝑘 (1≤𝑛≤500, 1≤ℓ≤105, 0≤𝑘≤𝑛−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 𝑛 integers 𝑑𝑖 (𝑑1=0, 𝑑𝑖<𝑑𝑖+1, 0≤𝑑𝑖≤ℓ−1) — coordinates of all signs.
The third line contains 𝑛 integers 𝑎𝑖 (1≤𝑎𝑖≤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 𝑘 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.
题意 :
- 长为l的直线距离,一开始有n个路牌,路牌上的值乘以下一段的长度就是下一段的时间,现在最多可以移走k个路牌,问从0走到l的最小时间
思路 :
- 范围500,首先不是图论,然后想到dp
- 一维状态转移型dp,dp[i][j]表示到达i点,用了最多j次操作,的最小时间
- 转移时,枚举从哪个点转移
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define endl '\n'
using namespace std;
const int N = 510;
int va[N], vb[N];
int dis[N][N];
int dp[N][N];
void solve()
{
int n, l, k; cin >> n >> l >> k;
for (int i = 1; i <= n && cin >> va[i]; i ++ );
for (int i = 1; i <= n && cin >> vb[i]; i ++ );
va[n + 1] = l;
for (int i = 1; i <= n; i ++ )
for (int j = i + 1; j <= n + 1; j ++ )
dis[i][j] = vb[i] * (va[j] - va[i]);
memset(dp, 0x3f, sizeof dp);
for (int i = 0; i <= k; i ++ ) dp[1][i] = 0;
for (int i = 2; i <= n + 1; i ++ )
for (int j = 0; j <= k; j ++ )
for (int s = 1; s < i; s ++ )
{
if (j >= i - s - 1) dp[i][j] = min(dp[i][j], dp[s][j - (i - s - 1)] + dis[s][i]);
}
cout << dp[n + 1][k] << endl;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int _; cin >> _;
// while (_ -- )
solve();
}