D. Make The Fence Great Again

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

For each query print one integer — the minimum number of rubles you have to spend to make the fence great.

Example
input

Copy
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
output

Copy
2
9
0
Note

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 二维)的更多相关文章

  1. 经典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] ...

  2. HDU 2159 FATE (DP 二维费用背包)

    题目链接 题意 : 中文题不详述. 思路 : 二维背包,dp[i][h]表示当前忍耐值为i的情况下,杀了h个怪得到的最大经验值,状态转移方程: dp[i][h] = max(dp[i][h],dp[i ...

  3. hdu6078 Wavel Sequence dp+二维树状数组

    //#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...

  4. dp --- 二维dp + 最大上升子序列

    <传送门> 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 74477   Accepted: 27574 ...

  5. codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)

    题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...

  6. 洛谷P1719 最大加权矩形 (DP/二维前缀和)

    题目描述也没啥好说的,就是给你个你n*n的矩形(带权),求其中最大权值的子矩阵. 首先比较好想的就是二维前缀和,n<=120,所以可以用暴力. 1 #include<bits/stdc++ ...

  7. bzoj 3594 [Scoi2014]方伯伯的玉米田(DP+二维BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3594 [题意] 给定一个n个数的序列,有K次将一个区间内的数加1的机会,问最长不下降子 ...

  8. POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)

    题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...

  9. dp 二维乃至多维背包

    洛谷P1855 榨取kkksc03 分析:套路是很明显的01背包,但是这时受约束的变量有两个了,这种情况下就该用多维背包了 分析方法一样的,用dp[i][j][k]表示从前i个愿望中挑选总时间和总金钱 ...

随机推荐

  1. 【NOIP2016提高组复赛day2】天天爱跑步

    题目 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏. <天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一棵 ...

  2. lazarus 连接mysql5.7 (deepin linux)

    在mysql下载站点下载驱动文件:libmysqlclient20_5.7.28-1debian9_amd64        64位 ,1.5M 安装后,lazarus IDE 就可以直接连MYSQL ...

  3. protocol buffer第一篇:语法介绍

    先理解一下protocol buffer是什么东西. protocol buffer是google发明的一种数据序列化方案,和json是同种类型的玩意,它非常适合在rpc场景下使用.同json一样,p ...

  4. POJ 2492 A Bug's Life (带权并查集 && 向量偏移)

    题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...

  5. 【gym102394L】LRU Algorithm(自然溢出哈希)

    题意:给定一个n个数的数字序列,第i个数为a[i],每次操作会将a[i]插入或移到最前端: 1.若a[i]已经在序列中出现过,则将其移到最前端,并删除原出现位置 2.若a[i]未出现过,则直接将其插入 ...

  6. Oracle --45 个非常有用的 Oracle 查询语句

    日期/时间 相关查询 1.获取当前月份的第一天运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期.SELECT TRUNC (SYSDATE, 'MO ...

  7. 大数据笔记(二十二)——大数据实时计算框架Storm

    一. 1.对比:离线计算和实时计算 离线计算:MapReduce,批量处理(Sqoop-->HDFS--> MR ---> HDFS) 实时计算:Storm和Spark Sparki ...

  8. 5-1 Django的路由层(urlconf)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...

  9. jquery 给input text元素赋值,js修改表单的值

    简单粗暴: (第一种) $('#checkUserName').attr("value",sessionUser.name); (第二种) $("#checkUserNa ...

  10. mysql 安装教程(详细说明)

    如果你装过,一定要先卸载干净,并且重启重新装.卸载教程(保证成功)https://www.cnblogs.com/qzhc/p/11354678.html 大家都知道MySQL是一款中.小型关系型数据 ...