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个愿望中挑选总时间和总金钱 ...
随机推荐
- CPC/CPM/CPA/CPS定义
CPC 每点击次数计费 CPM 每千人次展现计费 CPA 每行动成果计费(比如推广成功一个用户) CPS 淘宝客类型,按照商品佣金,推广成功计费
- 美团点评SQL优化工具SQLAdvisor开源快捷部署
美团点评SQL优化工具SQLAdvisor开源快捷部署 git clone https://github.com/Meituan-Dianping/SQLAdvisor.gityum install ...
- 【NOIP2016提高A组五校联考1】道路规划
题目 分析 我们考虑,当现在有一个合法的集合时,如何往里面增加一个点,使这个集合仍然合法. 假设现在有一个合法的集合, 那么当我们加入一个点,它的道路穿过来整个集合,那么 然后搞一遍最长下降子序列就可 ...
- python 习题
文件内容为一个多层元组,遍历该元组,当全为数字时输出数字之和,全为字母输出字符串,有数字有字母输出False,并将该内容写入到该文件的下一行中 # 方法一: t1= ((1,2,3),("a ...
- 页面渲染机制(一、DOM和CSSOM树的构建)
1.HTML的加载 HTML是一个网页的基础,下载完成后解析 2.其他静态资源加载 解析HTML时,发现其中有其他外部资源链接比如CSS.JS.图片等,会立即启用别的线程下载. 但当外部资源是JS时, ...
- python 线程模块
Python通过两个标准库thread和threading提供对线程的支持.thread提供了低级别的.原始的线程以及一个简单的锁. threading 模块提供的其他方法: threading.cu ...
- BZOJ 3786: 星系探索 ETT
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- POJ 1182 食物链 (带权并查集 && 向量偏移)
题意 : 中文题就不说题意了…… 分析 : 通过普通并查集的整理归类, 能够单纯地知道某些元素是否在同一个集合内.但是题目不仅只有种类之分, 还有种类之间的关系, 即同类以及吃与被吃, 而且重点是题目 ...
- 容器适配器————queue
只能访问 queue<T> 容器适配器的第一个和最后一个元素.只能在容器的末尾添加新元素,只能从头部移除元素. 操作 queue<int> q;//创建一个int型的空队列q ...
- mssql的sql注入拿后台
0x01判断数据 ①判断数据库类型 and exists (select * from sysobjects)--返回正常为mssql(也名sql server) and exists (select ...