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 ...
随机推荐
- WeUI基础样式库——写一个移动端界面
WeUI是一套基础样式库,同微信原生视觉体验一致,由微信官方设计团队为微信内网页和微信小程序量身设计的.我们来看看这个基础库样式到底长什么样. 这些密密麻麻的就是压缩后的样式库.密密麻麻地看起来简直要 ...
- react 事件传参数
class Demo extends React.Component { click = (a) => (e) => { console.log('额鹅鹅鹅', a, e); } rend ...
- 2_abstractions
2. Up and down the level of abstraction In this chapter, we'll travel up and down the level of abstr ...
- c++ STL queue:deque+优先队列
/* queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque队列 类型.一:定义queue(要有头文件#include <qu ...
- [HNOI2013] 消毒 - 二分图匹配
容易发现 \(a,b,c\) 中至少有一个 \(\leq 17\) 不妨将其调剂为 \(a\),那么我们可以暴力枚举哪些 \(x\) 片片要被直接削掉,剩下的拍扁成二维情况 二维时,如果有一个格子是 ...
- 项目转移时发生的错误<springboot+mybatis(xml逆向工程自动生成)>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'app ...
- AtCoder arc078_d Mole and Abandoned Mine
洛谷题目页面传送门 & AtCoder题目页面传送门 给定一个无向连通带权图\(G=(V,E),|V|=n,|E|=m\)(节点从\(0\)开始编号),要删掉一些边使得节点\(0\)到\(n- ...
- PyCharm专业版2019.3.2激活码到期2089年!!!
Pycharm是一款很好用的python开发工具,开发Python爬虫和Python web方面都很不错 这里我为大家提供了pycharm激活方式2089年(都支持PyCharm20 激活步骤如下: ...
- 查看杀死django进程
#命令:#用于显示tcp,udp的端口和进程等相关情况netstat -tunlp"""ps:-t (tcp)仅显示tcp相关选项-u (udp)仅显示udp相关选项-n ...
- DeepLearningDTU: Building a RNN step by step
exercise 5: Week 5 - Recurrent Neural Networks Building your Recurrent Neural Network - Step by Step