codeforces D. Make The Fence Great Again

//思路:二维DP,因为木板的高度只需与相邻俩边不同,所以只需枚举0,1,2的状态即可(俺是SB)
#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
const int maxn = 3e5 + 32;
int a[maxn],b[maxn];
i64 dp[maxn][3];
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0),cout.tie(0);
    int T,n;  cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;++i)
        {
            cin>>a[i]>>b[i];
            dp[i][0] = dp[i][1] = dp[i][2] = 0x3f3f3f3f3f3f3f3f;
        }
        dp[1][0] = 0,dp[1][1] = b[1],dp[1][2] = 2*b[1];
        for(int i=2;i<=n;++i)
        {
            for(int j=0;j!=3;++j)
            {// i - 1的花费
                for(int k=0;k!=3;++k)
                {
                    if(a[i-1]+j != a[i] + k)
                    {
                        dp[i][k] = min(dp[i][k],dp[i-1][j] + k*b[i]);
                    }
                }
            }
        }
        cout<<min(min(dp[n][0],dp[n][1]),dp[n][2])<<'\n';
    }
}

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

猜你喜欢

转载自www.cnblogs.com/newstartCY/p/11624900.html

相关文章