设f[i]表示在第i个点休息的话最少需要的体力值;
答案为f[n]
注意,如果每个点都休息的话;
总的花费是不会超过
所以,当x[i]-x[j]>60, 直接break
然后把x值相同的,直接去掉就好,(肯定不用停,因为停了又不会前进);
(以防所有坐标都一样,卡时间)
3
这是对动态规划的一种优化.思想要掌握!
一开始的时候,没往动态规划那块想。
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5;
int T,n,x[N+100];
LL t[100],f[N+100],a;
int main(){
//Open();
//Close();
t[0] = 1;
rep1(i,1,60)
t[i] = t[i-1]*2;
scanf("%d",&T);
while (T--){
scanf("%d%lld",&n,&a);
rep1(i,1,n) scanf("%d",&x[i]);
int nn = 1;
rep1(i,2,n)
if (x[i]!=x[i-1])
x[++nn] = x[i];
n = nn;
rep1(i,1,n) f[i] = -1;
f[1] = 0;
rep1(i,2,n){
rep2(j,i-1,1){
if (x[i]-x[j]>60) break;
if (f[i]==-1)
f[i] = f[j]+a+t[x[i]-x[j]];
else
f[i] = min(f[i],f[j]+a+t[x[i]-x[j]]);
}
}
printf("%lld
",f[n]);
}
return 0;
}