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

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

时间: 2024-10-10 22:59:26

Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)的相关文章

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

题目链接:http://codeforces.com/contest/1221/problem/D 题意:给一个序列,要求修改某些位置的数字,使得这个序列的相邻的数不相等,每次修改,只能使得某个数字加一,每次修改的代价为b[i],求最小所需的代价. 解题思路:经过简单分析,我们可以知道,每个数字最多只需要修改两次,那么我们定义dp[i][j]使得前j个数字相邻数字不等的最小代价,且最后一个数字修改了i次.那么答案即为min{dp[0][n],dp[1][n],dp[2][n]}. #includ

Educational Codeforces Round 73 (Rated for Div. 2)

比赛链接:Educational Codeforces Round 73 (Rated for Div. 2) 官方题解:Educational Codeforces Round 73 Editorial A. 2048 Game 题意 如果一个只包含 \(2\) 的幂次的集合,问能否从中选择一些数使得和为 \(2048\). 思路 不断合并直到凑到 \(2048\). 代码 #include <bits/stdc++.h> using namespace std; int main() {

Educational Codeforces Round 73 (Rated for Div. 2) A. 2048 Game

链接: https://codeforces.com/contest/1221/problem/A 题意: You are playing a variation of game 2048. Initially you have a multiset s of n integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations wi

Educational Codeforces Round 73 (Rated for Div. 2) B. Knights(构造)

链接: https://codeforces.com/contest/1221/problem/B 题意: You are given a chess board with n rows and n columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board. A knight is a chess pi

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w