dxedxy 2024-01-29 14:56 采纳率: 0%
浏览 81

千千蓝鲸5790 Real Mountains

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条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-01-29 15:04
    关注

    引自免费微信小程序:皆我百晓生

    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:

    1. Initialize dp[i][j] = infinity for all i and j.

    2. 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].

    3. Iterate through each column j from left to right.

    4. For each column j, iterate through each column i from 1 to j-1.

    5. 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.

    6. If the cost is not infinity, update dp[i][j] to the minimum of dp[i][j] and dp[i][j-1] + cost.

    7. 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月29日

悬赏问题

  • ¥20 如何再GIS用海岸线建立缓冲区
  • ¥15 codeblock遇到问题了,求帮助😭
  • ¥15 Qt6.8.0加载网页MSVC2022
  • ¥15 360浏览器m2的这个值
  • ¥15 国内有哪些厂商做automlops的?
  • ¥15 skynet pb mysql
  • ¥15 笔记本外接显示器分辨率太低各种方法都用过了调不高
  • ¥15 Redstone R0697-F00 D2020 交换机 OS
  • ¥50 H5+js 动态数字画廊怎么做?
  • ¥20 外向内全景图像拼接相关项目和论文咨询