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;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11625634.html