题意:
种套餐,第 种价格 ,得 桶,可买任意次。每次询问 ,求买至少 桶的最小总价。
思路:
补足:()。
反向优化:依次对 ,更新 。此时 表示至少需要 桶的最低价。
正向优化:依次对 ,更新 。保证 且序列 单调不降。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXB = 1e7+10;
const ll INF = 1e18;
ll a[MAXB];
ll solve(ll x) {
if (x == 0) return 0;
int p = 1;
while ((1LL << (p-1)) <= x) p++;
p--;
return min(a[p] + solve(x - (1LL << (p-1))), a[p+1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = N+1; i < MAXB; i++) a[i] = INF;
for (int i = MAXB-1; i >= 2; i--) a[i-1] = min(a[i-1], a[i]);
for (int i = 2; i < MAXB; i++) a[i] = min(a[i], 2 * a[i-1]);
while (Q--) {
ll x; cin >> x;
cout << solve(x) << '\n';
}
return 0;
}