链接:

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

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

  1. Educational Codeforces Round 73 (Rated for Div. 2)

    传送门 A. 2048 Game 乱搞即可. Code #include <bits/stdc++.h> #define MP make_pair #define fi first #de ...

  2. 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 ...

  3. Educational Codeforces Round 73 (Rated for Div. 2) C. Perfect Team

    链接: https://codeforces.com/contest/1221/problem/C 题意: You may have already known that a standard ICP ...

  4. 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. Init ...

  5. Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)

    这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用log的复杂度加快了更新速度,也用了区间查询(查询当前区间向右直至最右中以当前区间端点向右一段区间的和中最大的那一段的和),也用lo ...

  6. Educational Codeforces Round 73 (Rated for Div. 2)E(思维,博弈)

    //这道题博弈的核心就是不能让后手有一段只能放b而长度不够放a的段,并且先手要放最后一次#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h> ...

  7. Educational Codeforces Round 73 (Rated for Div. 2)D(DP,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[300007],b[3 ...

  8. Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化

    题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...

  9. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

    D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...

随机推荐

  1. [python] 一行命令搭建http服务内网传文件

    在Linux服务器上或者Windows服务器上,只要安装python,均可以使用此命令,建立一个内网可以快速访问的WEB服务. 在想要搭建WEB服务的目录下,使用Python3.x内置方法: pyth ...

  2. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  3. Win7 Eclipse 搭建spark java1.8(lambda)环境:WordCount helloworld例子

    [学习笔记] Win7 Eclipse 搭建spark java1.8(lambda)环境:WordCount helloworld例子 lambda表达式是java8给我们带来的一个重量的新特性,借 ...

  4. 【Trie】The XOR Largest Pair

    [题目链接] https://loj.ac/problem/10050 [题意] 给出n个数,其中取出两个数来,让其异或值最大. [题解] 经典的01字典树问题. 首先需要把01字典树建出来. 然后对 ...

  5. k8s-prometheus 数据采集(node redis kubelet等)

    apiVersion: v1 kind: ConfigMap metadata: name: prometheus-config namespace: kube-ops data: prometheu ...

  6. C# DataTable根据字段排序

    DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Age");// ...

  7. where用法

    where 子句用于指定类型约束. 1.接口约束 public class MyGenericClass<T> where T:IComparable { }  2.基类约束: 指出某个类 ...

  8. thinkphp 4.8 漏洞测试

    首先要部署环境 这里利用docker的方便部署性,来直接找个现成的    git clone https://github.com/vulnspy/thinkphp-5.1.29.git 下载安装后, ...

  9. Centos7--从最小化系统发开发环境

    Centos7--从最小化系统发开发环境 ​程序员总是离不开"环境"的困扰,从进入新手村的那一天就开始手动搞各种环境.虽然阿里云学生服务很方便,但是想弄集群真的买不起.正好实验室有 ...

  10. Oracle学习笔记:rank、dense_rank、row_number、ntile等排序算法

    在 oracle 中有很多函数可以实现排序的功能,但是不尽相同.下面一一解说. row_number函数 功能:可实现分组排序,为数据行添加序号,多用于分页查询. 语法:row_number() ov ...