传送门

容易想到 $dp$,但是如果直接设 $f[i][j]$ 表示修正完前 $i$ 个位置,第 $i$ 个位置增加了 $j$ 高度显然是不行的

考虑有性质,发现每个位置只会被左右两个位置影响而改变,即如果一边等于它那么才要考虑增加它的位置,并且如果此时另一边恰好比它原本高度大 $1$,这个位置才要再考虑增加高度

所以容易发现,每个位置最多增加 $2$ 的高度,然后就可以直接 $dp$ 了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=6e5+;
const ll INF=1e18+;
int Q,n,a[N],b[N];
ll f[N][];
int main()
{
Q=read();
memset(f,0x3f,sizeof(f));
while(Q--)
{
for(int i=;i<=n;i++) f[i][]=f[i][]=f[i][]=INF;
n=read(); for(int i=;i<=n;i++) a[i]=read(),b[i]=read();
f[][]=; f[][]=f[][]=INF;
for(int i=;i<=n;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
if(a[i-]+k!=a[i]+j)
f[i][j]=min(f[i][j],f[i-][k]+1ll*b[i]*j);
ll ans=INF; for(int i=;i<;i++) ans=min(ans,f[n][i]);
printf("%lld\n",ans);
}
return ;
}

Codeforces 1221D. Make The Fence Great Again的更多相关文章

  1. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  2. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  3. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

  4. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  5. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  6. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  7. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  8. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

随机推荐

  1. JS 浏览器地址栏传递参数,参数加密/解密(编码/解码)

    我们有时候在JS里进行页面跳转,并且传递了参数(AppName),如下: window.location = "../../views/form/edit.html?AppName=新增&q ...

  2. DMA数据传输

    从字面意思上看,DMA即为“直接内存读取”的意思,换句话说DMA就是用来传输数据的,它也属于一个外设.只是在传输数据时,无需占用CPU. 高速IO设备可以在处理器安排下直接与主存储器成批交换数据,称为 ...

  3. Spring Boot 入门之消息中间件篇(转发)

    一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消息后,消息就被消息代理接管,消息代理保证消息传递到指定目的地. 我们常用的消息代理有 JMS 和 AMQP 规范.对应 ...

  4. omniplan

    汉化版安装包 下载链接:https://pan.baidu.com/s/104ZddPtNWTHyEMZx90agKw 密码:qizl 序列号    Name: Appked    Serial: I ...

  5. 淘宝TAE平台定时任务包的部署步骤

    淘宝TAE平台定时任务包的部署: 第一步:首先把自己的任务打包成一个jar包.使用maven打包的命令为:mvn clean install 第二步:把任务jar包依赖的jar包全部导出来.使用mav ...

  6. 使用谷歌提供的SwipeRefreshLayout下拉控件进行下拉刷新的实现数据的刷新

    package com.loaderman.swiperefreshdemo; import android.os.Bundle; import android.os.Handler; import ...

  7. pagehelper用法

    spring boot 和 mybatis 中使用 pagehelper: 1. 在pom.xml 中加入pagehelper的引用: <dependency> <groupId&g ...

  8. java源码-ConcurrentHashMap分析-1

    ConcurrentHashMap源码分析 版本jdk8 摈弃了jdk7之前的segement段锁: 首先分析一下put方法,大致的流程就是首先对key取hash函数 判断是否first节点是否存在 ...

  9. JavaScript 基础入门11 - 运动框架的封装

    目录 JavaScript 运动原理 运动基础 简单运动的封装 淡入淡出 不同属性的设置 多属性值同时运动 运动回调,链式运动 缓冲运动 加入缓冲的运动框架 案例1 多图片展开收缩 运动的留言本 Ja ...

  10. 7-1 shell编程基础之二

    shell编程基础之二 算数运算 bash中的算术运算:help let +, -, *, /, %取模(取余), **(乘方),乘法符号有些场景中需要转义 实现算术运算: (1) let var=算 ...