题目链接:http://codeforces.com/contest/1221/problem/D

题目要求ai ! = ai-1,草纸上推理一下可以发现每一个栅栏可以升高的高度无非就是 +0,+1,+2

用dp【i】【j】表示到第 i 个栅栏升高 j 高度时,所需要的最小花费。

状态转移方程:dp[i][j] = min(dp[i-1][k]+j*b[i],dp[i][j]),其实每次循环共枚举了9次,分别是第 i 个栅栏升高 j 高度时候,对前一个也就是第i-1个栅栏分别升高+0,+1,+2高度的枚举,最终取一个min(枚举出第 i 个升高 j 高度满足ai!= ai-1的最小花费),一共是9次。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
int main(){
ios::sync_with_stdio(false);
int q;
scanf("%d",&q);
while(q--){
long long int dp[300001][3];
long long int a[300001],b[300001];
int n;
scanf("%d",&n);
for(int i = 0;i<n;i++){
scanf("%lld%lld",&a[i],&b[i]);
dp[i][0] = 1e18,dp[i][1] = 1e18,dp[i][2] = 1e18;
}
dp[0][0] = 0,dp[0][1] = b[0],dp[0][2] = 2*b[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)){
continue;//如果ai == ai-1跳出循环
}
dp[i][j] = min(dp[i-1][k]+j*b[i],dp[i][j]);
}
}
}
printf("%lld\n",min(dp[n-1][0],min(dp[n-1][1],dp[n-1][2])));
}
return 0;
}

codeforces Make The Fence Great Again(dp)的更多相关文章

  1. codeforces 659 G. Fence Divercity 组合数学 dp

    http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...

  2. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  3. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  4. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  5. 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 boar ...

  6. Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP

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

  7. codeforces 659G G. Fence Divercity(dp)

    题目链接: G. Fence Divercity time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)

    [题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...

  9. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

随机推荐

  1. Docker最全教程——从理论到实战(十七)

    前言 上一篇我们通过实战分享了使用Go推送钉钉消息,由于技痒,笔者现在也编写了一个.NET Core的Demo,作为简单的对照和说明. 最后,由于精力有限,笔者希望有兴趣的朋友可以分享下使用CoreR ...

  2. ASP.NET Core 2.1 中的 HttpClientFactory (Part 4) 整合Polly实现瞬时故障处理

    原文:https://www.stevejgordon.co.uk/httpclientfactory-using-polly-for-transient-fault-handling发表于:2018 ...

  3. 关闭Apache的目录浏览功能

    一.默认情况 默认情况下,Apache的配置文件C:\web\apache2.4\conf/httpd.conf中有如下参数: 引用 <Directory "/var/www/html ...

  4. Gauss消元模板

    ; //高斯消元模板 //----------------------------------------------------------------------------------- //把 ...

  5. 假期学习【八】首都之窗百姓信件爬虫(完整版)2020.2.6 Python

    时间:2020.2.6 今天把昨天做到一半的首都之窗百姓信件爬取完成了. 源码如下: import requests import io from bs4 import BeautifulSoup # ...

  6. 优化公式排版和Beamer相关知识

    做优化的同学可能会碰到排列形如 max    ******* s.t.   ***** = *        ***** > ***        ...    的格式 既要要求 max 和 s ...

  7. The view or its master was not found or no view engine supports the searched locations

    Error like:The view 'LoginRegister' or its master was not found or no view engine supports the searc ...

  8. VTK坐标系统及视图分割

    计算机图像学里广泛应用的坐标系统有四种,分别是:模型坐标系统(model),世界坐标系统(world),视图坐标系统(view)和显示坐标系统(display). 模型坐标系统就是定义模型时所用的坐标 ...

  9. RN开发-IDE和API

    一.开发工具 1.Visual Studio Code:微软IDE,轻量级,只有30+M大小 2.nuclide :仅支持Mac 3.WebStorm : JavaScript开发工具(IDE) 二. ...

  10. 2020牛客寒假算法基础集训营6 I.导航系统 (最小生成树)

    https://ac.nowcoder.com/acm/contest/3007/I 题中给定的图必定是一棵树 容易发现,如果将输入的N(N-1)个距离看做N(N-1)条无向边的话,那么如果数据合法, ...