ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[codeforces1221D] Make The Fence Great Again dp

2019-10-02 13:52:05  阅读:440  来源: 互联网

标签:Again Great Fence fence great length board each query


题目:

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.

题意:给一列数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 }

 

标签:Again,Great,Fence,fence,great,length,board,each,query
来源: https://www.cnblogs.com/conver/p/11617306.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有