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;
    }
    
  • 相关阅读:
    Java并发(5)- ReentrantLock与AQS
    Java并发(4)- synchronized与CAS
    Windows cmd 查看文件MD5 SHA1 SHA256
    进程、线程、协程概念理解
    Python学习--Python运算符
    Python学习--Python变量类型
    MySQL性能优化
    Docket学习--Docker入门
    Python学习--Python基础语法
    Python学习--Python 环境搭建
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11625634.html
Copyright © 2011-2022 走看看