make the fence great again(dp 二维)

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 ai1aiai−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 (1q31051≤q≤3⋅105) — the number of queries.

The first line of each query contains one integers nn (1n31051≤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 (1ai,bi1091≤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 31053⋅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 2b2=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 1b1+1b3=91⋅b1+1⋅b3=9.

In the third query the fence is great initially, so you don't need to spend rubles.

 

这题。。。比赛的时候居然没有做出来,嘤嘤嘤

 1 /*
 2     本题大意:给定一串数字,现在你要把这串数字变为相邻数字不相等
 3     的一串数字,变化方法为:对某个位置的数+1(可加无限多次),且有一定的花费,每个
 4     位置花费不同,问最小花费。
 5     本题思路:
 6         本题可以说是想到就很好写的线性dp了,因为不知道某个数字
 7         到底要加几次,也不知道某个数字加了之后会不会影响其它数字
 8         ,我们不可能依次枚举某个数字加的次数,但是我们很清晰的知道
 9         每个数字最多只能加两次,为什么呢?因为加入a[i] 和 a[i + 1]
10         相等,加入这次我们改变a[i],那么a[i] += 1;加了之后如果形成
11         了a[i - 1] == a[i],那么我们还可以选择其中较小的花费加1,也
12         就是有可能选择a[i]又加一,此时a[i] 肯定和两端的数字都不一样
13         所以说每个数字必定最多加两次就可以使得他和左右两边的数字都不一样。
14         也就是是说我们可以用记忆化搜索搞定这个题目。
15         我们用dp[i][j]表示第i个数字加了j次使得第i个数字之前的所有数字
16         不矛盾的最小花费,那么很明显。
17         我们用j表示第i-1个数加的次数,k表示第i个数加的次数
18         因此我们判断
19         if val[i] + k != val[i - 1] + j:
20             dp[i][k] = min(dp[i][k], dp[i - 1][j] + k * cost[i]) 
21 */
22 #include <cstdio>
23 #include <cstring>
24 using namespace std;
25 
26 typedef long long ll;
27 const int maxn = 300000 + 5;
28 ll inf =  1e18 + 5;
29 int n;
30 ll a[maxn], b[maxn];
31 ll dp[maxn][3];
32 
33 ll min(ll a, ll b) {
34     return a > b ? b : a;
35 }
36 
37 int main() {
38     int q;
39     scanf("%d", &q);
40     while(q --) {
41         scanf("%d", &n);
42         for(int i = 0; i <= n; i ++) {
43             for(int j = 0; j < 3; j ++) dp[i][j] = inf;
44         }
45         for(int i = 1; i <= n; i ++) {
46             scanf("%lld %lld", a + i, b + i);
47         }
48         dp[0][0] = 0ll;
49         for(int i = 1; i <= n; i ++) {
50             for(int j = 0; j < 3; j ++) {
51                 for(int k = 0; k < 3; k ++) {
52                     if(a[i] + j != a[i - 1] + k) 
53                         dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);
54                 }
55             }
56         }
57         printf("%lld\n", min(min(dp[n][0], dp[n][1]), dp[n][2]));
58     }
59     return 0;  
60 }

 

 

posted @ 2019-09-20 14:17  Cruel_King  阅读(763)  评论(0编辑  收藏  举报