题意:
思路:
首先证明一个结论:一个数最多被加两次。
首先假设
a
[
i
]
=
a
[
i
−
1
]
a[i]=a[i-1]
a[i]=a[i−1]或
a
[
i
]
=
a
[
i
+
1
]
a[i]=a[i+1]
a[i]=a[i+1],那么此时可以将
a
[
i
]
=
a
[
i
]
+
1
a[i]=a[i]+1
a[i]=a[i]+1,若此时
a
[
i
]
=
a
[
i
−
1
]
a[i]=a[i-1]
a[i]=a[i−1]或
a
[
i
]
=
a
[
i
+
1
]
a[i]=a[i+1]
a[i]=a[i+1],那么可以将其再加一,这个时候一定与两边的都不想等,此时再加就没有意义了。
既然只能加两次,那么定义
f
[
i
]
[
j
]
f[i][j]
f[i][j]表示到了第
i
i
i个位置,这个数加了
j
j
j次后满足条件的最小代价,转移方程:
f
[
i
]
[
j
]
=
m
i
n
(
f
[
i
]
[
j
]
,
f
[
i
−
1
]
[
k
]
+
j
∗
b
[
i
]
)
f[i][j]=min(f[i][j],f[i-1][k]+j*b[i])
f[i][j]=min(f[i][j],f[i−1][k]+j∗b[i])
注意初始化的时候要将其设为尽可能大的数。
代码中
10
10
10改成
3
3
3即可,当时是猜了个结论。
// Problem: D. Make The Fence Great Again
// Contest: Codeforces - Educational Codeforces Round 73 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1221/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=300010,mod=1e9+7,INF=0x3f3f3f3f;
const LL inf=0x3f3f3f3f3f3f3f3f;
const double eps=1e-6;
int n;
int a[N],b[N];
LL f[N][20];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--) {
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
for(int i=1;i<=n;i++) for(int j=0;j<=10;j++) f[i][j]=inf;
for(int i=0;i<=10;i++) f[1][i]=1ll*b[1]*i;
for(int i=2;i<=n;i++) {
for(int j=0;j<=10;j++) {
for(int k=0;k<=10;k++) {
if(a[i-1]+k!=a[i]+j) f[i][j]=min(f[i][j],f[i-1][k]+1ll*j*b[i]);
}
}
}
LL ans=inf;
for(int i=0;i<=10;i++) ans=min(ans,f[n][i]);
printf("%lld\n",ans);
}
return 0;
}
/*
*/