链接:

https://codeforces.com/contest/1221/problem/D

题意:

You have a fence consisting of n vertical boards. The width of each board is 1. The height of the i-th board is ai. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from 2 to n, the condition ai−1≠ai holds.

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the i-th board by 1, but you have to pay bi rubles for it. The length of each board can be increased any number of times (possibly, zero).

Calculate the minimum number of rubles you have to spend to make the fence great again!

You have to answer q independent queries.

思路:

考虑每个点加0,1, 2,种情况, 往后DP即可.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 3e5+10; LL Dp[MAXN][3];
int a[MAXN], b[MAXN];
int n; int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i] >> b[i];
Dp[1][0] = 0;
Dp[1][1] = b[1];
Dp[1][2] = 2*b[1];
for (int i = 2;i <= n;i++)
{
Dp[i][0] = Dp[i][1] = Dp[i][2] = 1e18;
for (int j = 0;j < 3;j++)
{
for (int k = 0;k < 3;k++)
{
if (a[i-1]+k != a[i]+j)
Dp[i][j] = min(Dp[i][j], Dp[i-1][k]+b[i]*j);
}
}
}
cout << min(Dp[n][0], min(Dp[n][1], Dp[n][2])) << endl;
} return 0;
}

最新文章

  1. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”
  2. VS联调多个解决方案的项目
  3. Design and Analysis of Algorithms_Introduction
  4. string.Format格式化
  5. sqltext的参数化处理
  6. 自定义android程序一段时间无操作后的功能
  7. PHP组合模式、策略模式
  8. Redis学习手册(开篇)
  9. php 数组操作类(整合 给意见)
  10. 数组去重Array
  11. C#、C++用GDAL读shp文件(转载)
  12. OpenSceneGraph几个重要功能节点练习
  13. 在线数据库表(sql语句)生成java实体类工具
  14. Python排序算法——快速排序
  15. django-media隐射
  16. array_column 低版本兼容
  17. 配置 cxf-rs spring bean 文件
  18. Java并发(二十):线程本地变量ThreadLocal
  19. Scanline Fill Algorithm
  20. 何在不联网的情况下ping通主机与虚拟机

热门文章

  1. vscode 安装一些快捷配置
  2. storm drpc分布式本地和远程调用模式讲解
  3. Oracle的查询-多行查询
  4. 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)
  5. k8s组件通信或者创建pod生命周期
  6. 彭博社:博通正在与赛门铁克洽谈收购事宜(博通能买得起 又能讲故事的 没几个了 为了刺激资本的兴趣 只能瞎搞 就和intel 收购 麦咖啡一样。就像杜蕾斯收购美赞臣一样,也许只是纯粹的商业行为,哪行赚钱干哪行)
  7. Entity的约束
  8. Django rest-framework框架-用户权限实例
  9. python matplotlib 折线图
  10. spark2.0 DataSet操作的一些问题记录