题目链接:http://www.ifrog.cc/acm/problem/1146?contest=1020&no=2
题解:显然知道这是一道dp而且
dp[i]=min(dp[j]+2^(x[j]-x[i])+a,dp[i])但是这题很显然2的次幂显然很容易比a大于是只要for一遍最多30次就行具体看一下代码,炒鸡简单的。
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> #define inf 10000000000000000 using namespace std; typedef long long ll; const int M = 1e5 + 10; int x[M] , t , n , a; ll dp[M] , Pow[65]; int main() { scanf("%d" , &t); Pow[0] = 1; for(int i = 1 ; i <= 63 ; i++) Pow[i] = Pow[i - 1] * (ll)2; while(t--) { scanf("%d%d" , &n , &a); for(int i = 0 ; i < n ; i++) scanf("%d" , &x[i]) , dp[i] = inf; dp[0] = 0; for(int i = 1 ; i < n ; i++) { int gg = i - 1 , count = 1; while((gg + 1)) { if(count >= 30 || x[i] - x[gg] > 30) break; dp[i] = min(dp[gg] + Pow[x[i] - x[gg]] + a , dp[i]); gg--; count++; } } printf("%lld " , dp[n - 1]); } return 0; }