码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

时间:2019-10-05 22:51:27      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:codeforce   problem   rate   ati   pac   oar   pen   false   链接   

链接:

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

Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

标签:codeforce   problem   rate   ati   pac   oar   pen   false   链接   

原文地址:https://www.cnblogs.com/YDDDD/p/11625634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!