codeforces Make The Fence Great Again(dp)
题目链接: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)的更多相关文章
- codeforces 659 G. Fence Divercity 组合数学 dp
http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- 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 ...
- 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 ...
- codeforces 659G G. Fence Divercity(dp)
题目链接: G. Fence Divercity time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
[题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
随机推荐
- Linux开机加载过程
2015-01-06 10:29:13 目录 1 开机加载简介 2 常规加载流程 2.1 加载BIOS 2.2 读取MBR 2.3 boot loader 2.4 加载内核 2.5 init依据i ...
- 小白月赛22 E : 方格涂色
E:方格涂色 考察点 : 思维,模拟 坑点 : long long 其他的好像没什么,读懂题意就可以 AC 不要被样例画的图所迷惑 Code: #include <vector> #inc ...
- PHP自定义函数+系统函数库
全局变量 $n = 5; //全局变量 function fun1(){ global $n; $n++; } fun1(); echo '<hr>'; $n = 6; function ...
- 【Unity|C#】基础篇(16)——文件读写(I/O类)
[笔记] 文件操作 File / FileInfo / FileStream https://www.runoob.com/csharp/csharp-file-io.html 文本读写 Stream ...
- 虚拟化技术xen的简介和安装
虚拟化技术的分类: 1,模拟:Emulation Qemu,PearPC,Bochs 2,完全虚拟化:Full Virtualization,Native Virtualization HVM ...
- Unable to open debugger port (127.0.0.1:13249): java.net.BindException "Address already in use: JVM_Bind"
这个问题比较简单一点,Tomcat的端口被占用了,我使用的是IDEA里的一个热部署插件JReble,更新了IDEA之后就发现端口被占用了,可能我电脑没有重启过吧, 一直被占用着,所以解决方法就是更换一 ...
- VS的使用技巧记录:
调试: F5:调试运行 会在编译前进行debug F10:单步步过 遇到函数不会进入函数内部执行 F11:单步步入 遇到函数会进入函数一步一步执行 ctrl+F5:直接运行不调试
- CSS-使用CSS样式的方式
1.HTML<!DOCTYPE>声明标签 (1)定义和用法 <!DOCTYPE>声明必须是HTML文档的第一行,位于<html>标签之前. <!DOCTYPE ...
- 一道CTF针对XXE漏洞的练习
题目链接:http://web.jarvisoj.com:9882/ 目的很明确获取/home/ctf/flag.txt的内容 一般读取目标机的本地文件都会用到file协议. 思路: 那么思路一:文件 ...
- 推荐7个GitHub项目
CS-Notes https://github.com/CyC2018/CS-Notes 技术面试必备基础知识.Leetcode.计算机操作系统.计算机网络.系统设计.Java.Python.C++. ...