D. Make The Fence Great Again(二维dp)

D. Make The Fence Great Again
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have a fence consisting of ? vertical boards. The width of each board is 1. The height of the ?-th board is ??. 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 ?, the condition ??−1≠?? holds.

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the ?-th board by 1, but you have to pay ?? 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 ? independent queries.

Input
The first line contains one integer ? (1≤?≤3⋅105) — the number of queries.

The first line of each query contains one integers ? (1≤?≤3⋅105) — the number of boards in the fence.

The following ? lines of each query contain the descriptions of the boards. The ?-th line contains two integers ?? and ?? (1≤??,??≤109) — the length of the ?-th board and the price for increasing it by 1, respectively.

It is guaranteed that sum of all ? over all queries not exceed 3⋅105.

It is guaranteed that answer to each query will not exceed 1018.

Output
For each query print one integer — the minimum number of rubles you have to spend to make the fence great.

Example
inputCopy
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
outputCopy
2
9
0
Note
In the first query you have to increase the length of second board by 2. So your total costs if 2⋅?2=2.

In the second query you have to increase the length of first board by 1 and the length of third board by 1. So your total costs if 1⋅?1+1⋅?3=9.

In the third query the fence is great initially, so you don’t need to spend rubles.

题意: 你可以提升格子1个单位的高度,花费b[i]代价。问最少多少代价使得相邻两个格子高度都不同。
思路: 很容易想到是二维dp(因为每个格子你可以翻也可以不翻),一开始推的方程式dp(i,0/1),0代表i不翻,1代表i翻。但是这样定义状态,使得你必须分很多情况讨论。但是题目有个特点——每个格子最多提高2点高度。这使得dp的第二维状态可以精确定义,dp(i,0/1/2)。这样以后就好推了。。。

#include 
#include 
#include 
#include 
 
using namespace std;
 
typedef long long ll;
const ll INF = 2e18;
const int maxn = 3e5 + 7;
ll h[maxn],b[maxn];
ll dp[maxn][3];
 
int main()
{
    int q;scanf("%d",&q);
    while(q--)
    {
        int n;scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%lld%lld",&h[i],&b[i]);
            dp[i][0] = dp[i][1] = dp[i][2] = INF;
        }
        
        dp[1][0] = 0;
        dp[1][1] = b[1];
        dp[1][2] = b[1] * 2;
        
        for(int i = 2;i <= n;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                for(int k = 0;k < 3;k++)
                {
                    if(h[i - 1] + j != h[i] + k)
                    {
                        dp[i][k] = min(dp[i][k],dp[i - 1][j] + k * b[i]);
                    }
                }
            }
        }
        printf("%lld\n",min(dp[n][0],min(dp[n][1],dp[n][2])));
    }
    return 0;
}

你可能感兴趣的:(#,codeforces,#,线性dp)