Codeforces 1221D Make The Fence great again 动态规划DP题解

● 本题解会有详细的分析,适合初学者阅读
## 原题

Problem Description

You have a fence consisting of n vertical boards. The width of each board is 1. The height of the i − t h i-th ith board is a i ai 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 a i − 1 ≠ a i a_{i−1}≠ai ai1=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.

Input

The first line contains one integer q ( 1 ≤ q ≤ 3 ⋅ 1 0 5 ) q (1≤q≤3⋅10^5) q(1q3105) — the number of queries.

The first line of each query contains one integers n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 ) n (1≤n≤3⋅10^5) n(1n3105) — the number of boards in the fence.

The following n lines of each query contain the descriptions of the boards. The ii-th line contains two integers a i a n d b i ( 1 ≤ a i , b i ≤ 1 0 9 ) a_i and b_i(1≤ai,bi≤10^9) aiandbi(1ai,bi109) — the length of the i − t h i-th ith board and the price for increasing it by 1, respectively.

It is guaranteed that sum of all n over all queries not exceed 3 ⋅ 1 0 5 3⋅10^5 3105.

Output

扫描二维码关注公众号,回复: 12270920 查看本文章

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

题目翻译

Problem Description

你有一个由n块竖板组成的栅栏。每块板的宽度是1。第i个板的高度是ai。如果没有一对相邻的板具有相同的高度,你认为栅栏是完美的。更正式地说,当且仅当对于从2到n的所有指数,条件ai−1≠ai成立时,栅栏是最完美的的。

不幸的是,现在你的篱笆不完美。但你可以改变它。你可以将第i块板的长度增加1,但你必须为此支付 b i b_i biRubles。每个板的长度可以增加任意次数(可以为零)。

计算一下你最少花多少Rubles才能使栅栏再次变得完美!一共有q组询问,保证 ∑ i = 1 q n i ≤ 3 × 1 0 5 \sum_{i=1}^q n_i\le3\times10^5 i=1qni3×105,答案在 l o n g   l o n g long\ long long long范围内

Input

第一行包含一个整数 q ( 1 ≤ q ≤ 3 ⋅ 1 0 5 ) q(1≤q≤3⋅10^5) q1q3105—询问的组数。

每个询问的第一行包含一个整数 n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 ) n(1≤n≤3⋅10^5) n1n3105—围栏中的板数。

每个询问的下面n行包含电路板的描述。第二行包含两个整数 a i 和 b i ( 1 ≤ a i , b i ≤ 1 0 9 ) a_i和b_i(1≤ai,bi≤10^9) aibi1aibi109—分别是 i − t h i-th ith板的长度和增加1的价格。

保证所有n个查询的总和不超过 3 ⋅ 1 0 5 3⋅10^5 3105

Output

对于每个询问输出一个整数-你必须花费卢布的最小数目使围栏完美。

题目分析

这人的癖好有点奇怪首先,我们来分析分析,相邻栅栏一样高的时候,哦我们应该如何处理:

1

右侧7根柱子是合法的,左侧的3根是不合法的,我们拿123根来当示例:

我们首先从头开始向右扫描,第一根与第二根长度相同,因此我们花费1个代价,使一号柱子变高;

2

继续向右扫描,第二根柱子与第三根柱子等高,于是花费1个代价,使柱子增高;

3

我们发现,与左侧再次等高,因此又要花费1个代价,使二号柱子增高

4

此时,达到合法状态。

根据上述例子我们不难发现,对于最坏的情况三根柱子等高时,我们花费代价使柱子升高,那么只可能有两种情况:升高1单位、升高2单位;这两种情况一定能使相邻柱子两两互不等高。再加上一种理想状态:无需花费代价,柱子便是完美状态。

因此:高度变化量 △ H = 0 , 1 , 2 △H = 0,1,2 H=012这里的高度变化量实际上就是代价的个数。

接下来就是动态规划:我们设 d p [ i ] [ j ] dp[i][j] dp[i][j]表示第 i i i根柱子升高 j ( j = 0 , 1 , 2 ) j(j = 0,1,2) j(j=0,1,2)高度时的最小花费,我们可以推出状态转移方程:

d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i − 1 ] [ k ] + j ∗ b [ i ] ) dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]) dp[i][j]=min(dp[i][j],dp[i1][k]+jb[i]) ( b [ i ] 表 示 第 i 个 柱 子 升 高 1 单 位 的 代 价 ) (b[i]表示第i个柱子升高1单位的代价) (b[i]i1)

注意状态转移方程成立的条件–相邻柱子操作后的高度不相等: a [ i ] + j ! = a [ i − 1 ] + k a[i] + j != a[i - 1] + k a[i]+j!=a[i1]+k

分析完毕,写代码。

AC Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;

ll a[3 * N], b [3 * N], dp[3 * N][3];

int main(){
    
    
    ios::sync_with_stdio(0);
    int q = 0; cin >> q;
    while(q--){
    
    
        int n = 0; cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
        for(int i = 0; i <= n; i++)
            for(int j = 0; j < 3; j++) dp[i][j] = 1e18 + 5;
        dp[0][0] = 0;
        for(int i = 1; i <= n; i++){
    
    
            for(int j = 0; j < 3; j++){
    
    
                for(int k = 0; k < 3; k++){
    
    
                    if(a[i] + j != a[i - 1] + k) dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);
                }
            }
        }
        cout << min(min(dp[n][0], dp[n][1]), dp[n][2]) << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yanweiqi1754989931/article/details/113094278