make the fence great again(dp 二维)
D. Make The Fence Great Again
2 seconds
256 megabytes
standard input
standard output
You have a fence consisting of nn vertical boards. The width of each board is 11. The height of the ii-th board is aiai. 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 22 to nn, the condition ai−1≠aiai−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 ii-th board by 11, but you have to pay bibi 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 qq independent queries.
The first line contains one integer qq (1≤q≤3⋅1051≤q≤3⋅105) — the number of queries.
The first line of each query contains one integers nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of boards in the fence.
The following nn lines of each query contain the descriptions of the boards. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109) — the length of the ii-th board and the price for increasing it by 11, respectively.
It is guaranteed that sum of all nn over all queries not exceed 3⋅1053⋅105.
It is guaranteed that answer to each query will not exceed 10181018.
For each query print one integer — the minimum number of rubles you have to spend to make the fence great.
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
2
9
0
In the first query you have to increase the length of second board by 22. So your total costs if 2⋅b2=22⋅b2=2.
In the second query you have to increase the length of first board by 11 and the length of third board by 11. So your total costs if 1⋅b1+1⋅b3=91⋅b1+1⋅b3=9.
In the third query the fence is great initially, so you don't need to spend rubles.
这题。。。比赛的时候居然没有做出来,嘤嘤嘤
/*
本题大意:给定一串数字,现在你要把这串数字变为相邻数字不相等
的一串数字,变化方法为:对某个位置的数+1(可加无限多次),且有一定的花费,每个
位置花费不同,问最小花费。
本题思路:
本题可以说是想到就很好写的线性dp了,因为不知道某个数字
到底要加几次,也不知道某个数字加了之后会不会影响其它数字
,我们不可能依次枚举某个数字加的次数,但是我们很清晰的知道
每个数字最多只能加两次,为什么呢?因为加入a[i] 和 a[i + 1]
相等,加入这次我们改变a[i],那么a[i] += 1;加了之后如果形成
了a[i - 1] == a[i],那么我们还可以选择其中较小的花费加1,也
就是有可能选择a[i]又加一,此时a[i] 肯定和两端的数字都不一样
所以说每个数字必定最多加两次就可以使得他和左右两边的数字都不一样。
也就是是说我们可以用记忆化搜索搞定这个题目。
我们用dp[i][j]表示第i个数字加了j次使得第i个数字之前的所有数字
不矛盾的最小花费,那么很明显。
我们用j表示第i-1个数加的次数,k表示第i个数加的次数
因此我们判断
if val[i] + k != val[i - 1] + j:
dp[i][k] = min(dp[i][k], dp[i - 1][j] + k * cost[i])
*/
#include <cstdio>
#include <cstring>
using namespace std; typedef long long ll;
const int maxn = + ;
ll inf = 1e18 + ;
int n;
ll a[maxn], b[maxn];
ll dp[maxn][]; ll min(ll a, ll b) {
return a > b ? b : a;
} int main() {
int q;
scanf("%d", &q);
while(q --) {
scanf("%d", &n);
for(int i = ; i <= n; i ++) {
for(int j = ; j < ; j ++) dp[i][j] = inf;
}
for(int i = ; i <= n; i ++) {
scanf("%lld %lld", a + i, b + i);
}
dp[][] = 0ll;
for(int i = ; i <= n; i ++) {
for(int j = ; j < ; j ++) {
for(int k = ; k < ; k ++) {
if(a[i] + j != a[i - ] + k)
dp[i][j] = min(dp[i][j], dp[i - ][k] + j * b[i]);
}
}
}
printf("%lld\n", min(min(dp[n][], dp[n][]), dp[n][]));
}
return ;
}
make the fence great again(dp 二维)的更多相关文章
- 经典DP 二维换一维
HDU 1024 Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...
- HDU 2159 FATE (DP 二维费用背包)
题目链接 题意 : 中文题不详述. 思路 : 二维背包,dp[i][h]表示当前忍耐值为i的情况下,杀了h个怪得到的最大经验值,状态转移方程: dp[i][h] = max(dp[i][h],dp[i ...
- hdu6078 Wavel Sequence dp+二维树状数组
//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...
- dp --- 二维dp + 最大上升子序列
<传送门> 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74477 Accepted: 27574 ...
- codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)
题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...
- 洛谷P1719 最大加权矩形 (DP/二维前缀和)
题目描述也没啥好说的,就是给你个你n*n的矩形(带权),求其中最大权值的子矩阵. 首先比较好想的就是二维前缀和,n<=120,所以可以用暴力. 1 #include<bits/stdc++ ...
- bzoj 3594 [Scoi2014]方伯伯的玉米田(DP+二维BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3594 [题意] 给定一个n个数的序列,有K次将一个区间内的数加1的机会,问最长不下降子 ...
- POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...
- dp 二维乃至多维背包
洛谷P1855 榨取kkksc03 分析:套路是很明显的01背包,但是这时受约束的变量有两个了,这种情况下就该用多维背包了 分析方法一样的,用dp[i][j][k]表示从前i个愿望中挑选总时间和总金钱 ...
随机推荐
- R语言-六大数据结构
R语言有六种基本的数据结构(或者说数据类型吧).根据数据的维度和同质/异质可分为5种数据类型,最后再介绍一种特殊的类型“因子”. 同质 异质 1维 原子向量 列表 2维 矩阵 数据框 n维 数组 ...
- JAVA笔记25-IO流(3)-处理流举例
处理流类型: 1.缓冲流 例1: import java.io.*; public class TestBufferStream{ public static void main(String arg ...
- node.js入门学习(六)--express
1.官网:http://expressjs.com/ 中文:http://www.expressjs.com.cn/ 2.HelloWorld 1)mkdir node-express-demo 2) ...
- CDOJ 1059 秋实大哥与小朋友 STL(set)+离散化+BIT区间更新单点查询
链接: A - 秋实大哥与小朋友 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Subm ...
- 【gym102394B】Binary Numbers(DP)
题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...
- 论文阅读:Stateless Network Functions: Breaking the Tight Coupling of State and Processing
摘要: 无状态网络功能是一个新的网络功能虚拟化架构,解耦了现有的网络功能设计到无状态处理组件以及数据存储层,在打破紧密耦合的同时,实现了更具可伸缩性和可恢复性的网络功能基础设施.无状态NF处理实例是围 ...
- 南京网络赛C
分段打表大法好!!! 打表40min,A题1s https://nanti.jisuanke.com/t/41300 #include<bits/stdc++.h> #define int ...
- [design pattern](3) Dectorator
前言 很久没有写关于设计模式的博客了,实在是没有太多的精力去写.但个人觉得设计模式在我们的日常开发中还是挺重要的,它提高了软件的可维护性.因此还是有必要坚持学习设计模式,写博客主要是为了加深我对设计模 ...
- ElasticSearch的介绍
1.ELK 1.1 集中式日志系统 日志,对于任何系统来说都是及其重要的组成部分.在计算机系统里面,更是如此.但是由于现在的计算机系统大多比较复杂,很多系统都不是在一个地方,甚至都是跨国界的:即使是在 ...
- Hive函数介绍
一些函数不太会,查了些资料,分享一下 Hive已定义函数介绍: 1.字符串长度函数:length 语法: length(string A)返回值: int举例:[sql] view plain cop ...