zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

    链接:

    https://codeforces.com/contest/1221/problem/D

    题意:

    You have a fence consisting of n vertical boards. The width of each board is 1. The height of the i-th board is ai. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from 2 to n, the condition ai−1≠ai holds.

    Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the i-th board by 1, but you have to pay bi rubles for it. The length of each board can be increased any number of times (possibly, zero).

    Calculate the minimum number of rubles you have to spend to make the fence great again!

    You have to answer q independent queries.

    思路:

    考虑每个点加0,1, 2,种情况, 往后DP即可.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MAXN = 3e5+10;
    
    LL Dp[MAXN][3];
    int a[MAXN], b[MAXN];
    int n;
    
    int main()
    {
        ios::sync_with_stdio(false);
        int t;
        cin >> t;
        while (t--)
        {
            cin >> n;
            for (int i = 1;i <= n;i++)
                cin >> a[i] >> b[i];
            Dp[1][0] = 0;
            Dp[1][1] = b[1];
            Dp[1][2] = 2*b[1];
            for (int i = 2;i <= n;i++)
            {
                Dp[i][0] = Dp[i][1] = Dp[i][2] = 1e18;
                for (int j = 0;j < 3;j++)
                {
                    for (int k = 0;k < 3;k++)
                    {
                        if (a[i-1]+k != a[i]+j)
                            Dp[i][j] = min(Dp[i][j], Dp[i-1][k]+b[i]*j);
                    }
                }
            }
            cout << min(Dp[n][0], min(Dp[n][1], Dp[n][2])) << endl;
        }
    
        return 0;
    }
    
  • 相关阅读:
    小福bbs-冲刺日志(第三天)
    小福bbs-冲刺日志(第二天)
    小福bbs-冲刺日志(第一天)
    灯塔-冲刺集合
    团队作业第六次—事后诸葛亮
    灯塔-冲刺总结
    灯塔-测试总结
    灯塔-冲刺日志(第七天)
    灯塔-冲刺日志(第六天)
    灯塔-冲刺日志(第五天)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11625634.html
Copyright © 2011-2022 走看看