Codeforces 1221D. Make The Fence Great Again
容易想到 $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的更多相关文章
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- 【贪心】Codeforces 349B.Color the Fence题解
题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...
- Codeforces 1132C - Painting the Fence - [前缀和优化]
题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- Codeforces 448 C. Painting Fence
递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
随机推荐
- vue-loader+webpack配置项目流程
前提:安装了node.js与npm 1.建立一个npm项目 新建项目文件夹,打开终端,将路径移动至此文件夹,初始化输入 npm init 按照提示输入项目名称,描述,作者等信息(可回车跳过) 成功创建 ...
- springboot+dubbo+zookeeper+mybatis
参考地址:https://www.cnblogs.com/gaopengfirst/p/9555240.html 首先创建一个maven项目: 再在该父项目中创建3个module,分别是:provid ...
- 普通线程类获取service,controller等spring容器类
package com.zihexin.application.strategy; import org.springframework.beans.BeansException; import or ...
- mybatis延迟加载(assocation)
一.何为延迟加载? 延迟加载 我们并不需要总是在加载用户信息时就一定要加载他的账户信息.此时就是我们所说的延迟加载. 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. ...
- mysql使用命令行执行存储过程
编写存储过程sql 以给brand表添加phone字段为例: DROP PROCEDURE IF EXISTS UpdateColum; CREATE PROCEDURE UpdateColum() ...
- 调用远程linux服务器shell脚本
package com.haiyisoft.hyoaPc.ui; import java.io.BufferedReader;import java.io.IOException;import jav ...
- 打开新窗口(window.open)关闭窗口(window.close)
打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL: ...
- 微信小程序之条件判断
前文: 今天踩了一下午的坑,但是确实很简单的问题. 我说一下需求:扫描商品的二维码,从而判断,同一个二维码不可多次扫描: 点击扫一扫 会在灰色区域展示 扫描的商品信息,比如商品名称,商品码等,但是我们 ...
- 七十六:flask.Restful之flask-Restful插件的基本使用
安装:flask 0.8以上.python2.6或者3.3以上:pip install flask-restful 使用方法:1.从flask_restful中导入Api,来创建对象 2.写一个视图函 ...
- SQL学习(四)Where语句中的各种匹配方式
在where语句中,我们需要制定各种条件,条件的各种组合需要用到不同的关键字 一.单条件 如:select * from ticket where name='测试' 二.多条件 1.和(and) 如 ...