Thanks to your help with cropping her picture, Rebecca’s scenic photo is now featured on
the front cover of the newest issue of her magazine. However, it seems that some of her
readers still aren’t pleased with the picture. In particular, they seem to believe that the
mountain in the picture is fake!
For simplicity, we can describe the picture as a sequence of N columns of pixels. In the i-th
column, the first hi pixels from the bottom are of mountains. Her readers will only believe
that the picture contains a real mountain if it contains a single (possibly wide) peak. That
is, if there exists some index p with 1 ≤ p ≤ N such that h1 ≤ h2 ≤ · · · ≤ hp ≥ · · · ≥
hN−1 ≥ hN .
Luckily, Rebecca can still pay her editors to modify the picture and reprint the magazine.
Unfortunately for her though, the editors have a very peculiar pricing scheme for their work.
The only way Rebecca can edit the picture is by sending emails to her editors containing
the integers (i, j, k) such that 1 ≤ i < j < k ≤ N and hi > hj < hk. The editors will then
add an extra pixel of mountains in the j-th column (i.e. increment hj by 1) for a cost of
hi + hj + hk cents. Note that the change in hj may affect the costs of future edits.
To please her readers, Rebecca would like to edit the picture so that they believe it contains
a real mountain. Can you tell her the minimum cost required to do so?
3条回答 默认 最新
关注
引自免费微信小程序:皆我百晓生
To solve this problem, we can use dynamic programming. We will create a 2D array, dp, where dp[i][j] represents the minimum cost required to create a single peak up to column j with the last peak at column i.
Here are the steps to find the minimum cost:
-
Initialize dp[i][j] = infinity for all i and j.
-
For each column j, calculate the prefix sum of heights up to that column. Let's call it sum[j]. This can be done by sum[j] = sum[j-1] + h[j].
-
Iterate through each column j from left to right.
-
For each column j, iterate through each column i from 1 to j-1.
-
For each i, calculate the cost of adding a peak at j by checking if h[i] <= h[j] <= h[i+1]. If true, the cost would be sum[j-1] - sum[i]. Otherwise, the cost would be infinity.
-
If the cost is not infinity, update dp[i][j] to the minimum of dp[i][j] and dp[i][j-1] + cost.
-
Finally, the minimum cost required would be the minimum value in dp[N][j], where N is the total number of columns.
Here's the code that implements the above steps in C++:
#include <iostream> #include <vector> #include <climits> int main() { int N; std::cin >> N; std::vector<int> h(N+1); // 1-indexed array to store heights for(int i = 1; i <= N; i++) { std::cin >> h[i]; } std::vector<std::vector<int>> dp(N+1, std::vector<int>(N+1, INT_MAX)); std::vector<int> sum(N+1, 0); // prefix sum of heights for(int j = 1; j <= N; j++) { sum[j] = sum[j-1] + h[j]; } for(int j = 1; j <= N; j++) { dp[j][j] = 0; // Cost to create a single peak at column j with the last peak at column j is zero for(int i = 1; i < j; i++) { int cost = (h[i] <= h[j] && h[j] <= h[i+1]) ? (sum[j-1] - sum[i]) : INT_MAX; if(cost != INT_MAX) { dp[j][j] = std::min(dp[j][j], dp[i][j-1] + cost); } } } int minCost = INT_MAX; for(int j = 1; j <= N; j++) { minCost = std::min(minCost, dp[N][j]); } std::cout << minCost << std::endl; return 0; }
Please note that this code includes the necessary input reading code for N and the heights of the columns. Make sure to modify the code accordingly to read the input properly.
I hope this helps! Let me know if you have any further questions.
解决 1无用-
关注
【以下回答由 GPT 生成】
我可以帮助你解决这个问题。这个问题可以通过动态规划的方法来解决。
首先,我们可以定义一个二维数组dp,其中dp[i][j]表示从第1列到第i列中,在第i列底部的山的高度为j时,实现目标所需的最低成本。
接下来,我们可以遍历照片的每一列,计算出每一个dp[i][j]的值。
具体的计算方法如下: 1. 对于第i列底部的山的高度j,我们需要找到另外两列,可以分别表示为第i之前的列x和第i之后的列y,使得满足条件hi > hj < hk。 2. 对于每一对(x, y),我们可以计算出当前的成本cost = hi + hj + hk。 3. 对于每一个可能的(x, y)组合,我们可以更新dp[i][j]的值,取所有成本中的最小值。
最后,我们需要返回dp[N][j],其中N表示照片的总列数,j表示最后一列山的高度。
以下是使用Python编写的实现代码:
def minimum_cost(photo): N = len(photo) dp = [[float('inf')] * (N+1) for _ in range(N+1)] dp[0][0] = 0 for i in range(1, N+1): for j in range(i): for k in range(i+1, N+1): cost = photo[j-1] + photo[i-1] + photo[k-1] dp[i][photo[i]] = min(dp[i][photo[i]], dp[j][photo[j]] + cost) return min(dp[N]) # 测试代码 photo = [1, 3, 2, 4] cost = minimum_cost(photo) print(cost)
这段代码会输出最低成本的值。
注意,以上代码仅仅解决了这个问题的核心逻辑,你可以根据实际情况在此基础上进行扩展,例如添加输入输出的处理逻辑等。
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^解决 1无用- dxedxy 2024-01-30 07:03关注解决 无用评论 打赏 举报 编辑记录