[codeforces1221D] Make The Fence Great Again dp

Posted conver

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[codeforces1221D] Make The Fence Great Again dp相关的知识,希望对你有一定的参考价值。

题目:

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.

题意:给一列数ai,使相邻的数不同,可以花费bi代价使ai增加1,求最小代价


题解:

参考的题解:https://www.cnblogs.com/bianjunting/p/11556876.html
比赛的时候没做出来,没想到一个栏杆最多只需要增加两次。
因为如果两个相邻的数a[i] 和 a[i-1]相同,选择其中一个+1,比如a[i],这时候如果a[i]==a[i+1],a[i]再+1就能和a[i+1]不同。
线性dp  f[i][k]表示把第i个数增加k次,使i和之前的数满足条件的最小代价。k=0,1,2。
if(a[i]+k!=a[i-1]+j)
f[i][k]=min(f[i-1][j]+k*b[i],f[i][k]);

ac代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int const maxn=300010;
 4 long long  a[maxn],n,b[maxn],f[maxn][4];
 5 inline long long  get_num()
 6     long long  num=0;
 7     char ch=getchar();
 8     while(ch<0||ch>9)ch=getchar();
 9     while(ch>=0&&ch<=9)num=(num<<3)+(num<<1)+ch-0;ch=getchar();
10     return num;
11 
12 int main()
13     int  q;
14     scanf("%d",&q);
15     while(q--)
16         n=get_num();
17         for(int i=1;i<=n;i++)
18             a[i]=get_num();b[i]=get_num();
19         
20         
21         
22         f[1][0]=0;
23         f[1][1]=b[1];f[1][2]=2*b[1];
24 
25         for(int i=2;i<=n;i++)
26             f[i][0]=f[i][1]=f[i][2]=1e18;
27             for(long long k=0;k<3;k++)
28                 for(long long  j=0;j<3;j++)
29                     if(a[i]+k!=a[i-1]+j)
30                         f[i][k]=min(f[i-1][j]+k*b[i],f[i][k]);
31                     
32                 
33         
34         long long ans=min(f[n][0],f[n][1]);
35         if(f[n][2]<ans)ans=f[n][2];
36         printf("%lld\\n",ans);
37     
38 

 

以上是关于[codeforces1221D] Make The Fence Great Again dp的主要内容,如果未能解决你的问题,请参考以下文章

CF1221D Make The Fence Great Again

[Codeforces 864D]Make a Permutation!

Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

Make It Connected CodeForces - 1095F (建图+最小生成树)

Codeforces731D 80-th Level Archeology

Make k Equal from Codeforces Round #629 (Div. 3)