codeforces-913C Party Lemonade

C. Party Lemonade
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.


Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.


You want to buy at least L liters of lemonade. How many roubles do you have to spend?


Input
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.


The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.


Output
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.


Examples
input
4 12
20 30 70 90
output
150
input
4 3
10000 1000 100 10
output
10
input
4 3
10 100 1000 10000
output
30
input
5 787787787
123456789 234567890 345678901 456789012 987654321
output
44981600785557577
Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.

题意:第 i 种柠檬水可以装 2 的 i-1 次方升柠檬水花费 ci 卢布。商店里每种类型的瓶子数量都可以被认为是无限的。 你想买至少l升柠檬水。你最少要花多少卢布?

思路:比赛时没做出来,感觉是贪心,可是不会写。。。

题解:

Note that if ai ≤ ai + 1, then it doesn't make sense to buy any bottles of type i + 1 — it won't ever be worse to buy two bottles of type i instead. In this case let's assume that we actually have an option to buy a bottle of type i + 1 at the cost of ai and replaceai + 1 with min(ai + 1, 2·ai). Let's do this for all i from 1 to n - 1 in increasing order.

Now for all i it's true that ai ≥ ai + 1. Note that now it doesn't make sense to buy more than one bottle of type i if i < n. Indeed, in this case it won't ever be worse to buy a bottle of type i + 1 instead of two bottles of type i. From now on, we'll only search for options where we buy at most one bottle of every type except the last one.

Suppose that we had to buy exactly L liters of lemonade, as opposed to at least L. Note that in this case the last n - 1 bits of Luniquely determine which bottles of types less than n we have to buy. Indeed, if L is odd, then we have to buy a bottle of type 0, otherwise we can't do that. By the same line of thought, it's easy to see that bit j in the binary representation of L is responsible for whether we should buy a bottle of type j. Finally, we have to buy exactly ⌊ L / 2n - 1 bottles of type n.

But what to do with the fact that we're allowed to buy more than L liters? Suppose we buy M > L liters. Consider the highest bit j in which M and L differ. Since M > L, the j-th bit in M is 1, and the j-th bit in L is 0. But then all bits lower than the j-th in M are 0 in the optimal answer, since these bits are responsible for the "extra" bottles — those for which we spend money for some reason, but without which we would still have M > L.

Thus, here is the overall solution. Loop over the highest bit j in which M differs from L. Form the value of M, taking bits higher than the j-th from L, setting the j-th bit in M to 1, and bits lower than the j-th to 0. Calculate the amount of money we have to pay to buy exactly M liters of lemonade. Take the minimum over all j.

The complexity of the solution is O(n) or O(n2), depending on the implementation.


#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, L;
  scanf("%d %d", &n, &L);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &c[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    c[i + 1] = min(c[i + 1], 2 * c[i]);
  }
  long long ans = (long long) 4e18;
  long long sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    int need = L / (1 << i);
    sum += (long long) need * c[i];
    L -= need << i;
    ans = min(ans, sum + (L > 0) * c[i]);
  }
  cout << ans << endl;
  return 0;
}














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值