CoderForces-913-C
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.
Example
4 12
20 30 70 90
150
4 3
10000 1000 100 10
10
4 3
10 100 1000 10000
30
5 787787787
123456789 234567890 345678901 456789012 987654321
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.
题解:
因为 2n−1×2=2n2n−1×2=2n ,所以我们可以想到小物品组成大物品是否可以带来更小的花费。
于是从小到大扫一遍计算出组成当前大小为 2i2i 所需要的最小花费,记为 aiai 。
然后针对大小 LL ,我们可以将其转换为二进制,从高位往低位开始枚举,
用 nownow 记录已访问的高位中所需要的花费,若当前位为 11 , now+=a[i]now+=a[i] ,因为我们不能通过这一位组合出大小大于 LL 的货物,
若当前位为 00 ,记录 now+a[i]now+a[i] ,因为此时我们只需要将该位填充为 11 即可组出大于 LL 的货物。
然后找最小值即可。
AC代码为:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
ll cost[50];
int main()
{
int n, l;
cin >> n >> l;
for (int i = 1; i <= n; ++i)
{
cin >> cost[i];
}
for (int i = 1; i <= 30; ++i)
{
if (i + 1 > n || cost[i] * 2 < cost[i + 1])
cost[i + 1] = cost[i] * 2;
}
for (int i = 30; i >= 1; --i)
{
if (cost[i + 1] < cost[i])
cost[i] = cost[i + 1];
}
ll ans = 0;
ll res = 1e18;
for (int i = 30; i >= 0; --i)
{
if (l & (1ll << i))
ans += cost[i + 1];
res = min(ans + cost[i + 1], res);
}
cout << min(res, ans) << endl;
}
CoderForces-913-C的更多相关文章
- Discuz!NT 3.9.913 Beta DIY过程
前提: 论坛的源码版本为dnt_3.9.913_sqlserver_beta.zip,以下例子都以这个版本为原型修改 dnt_3.9.913数据字典:下载 目前(2013年10月21日)官网的asp. ...
- [LeetCode] 913. Cat and Mouse 猫和老鼠
A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The grap ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- 【LeetCode】913. Cat and Mouse 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- coderforces 721b
题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- UESTC 913 握手 Havel定理+优先队列
给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化.进一步,若图为简单图,则称此序列可简单图化. 此题因为是无自环无重边,所以是简单图.用判定简单图可图化 ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
随机推荐
- 深入理解计算机系统 第八章 异常控制流 part1
本章主旨 第八章的目的是阐述清楚应用程序是如何与操作系统交互的(之前章节的学习是阐述应用程序是如何与硬件交互的) 异常控制流 异常控制流,即 ECF(exceptional contril flow) ...
- 了解Spring的基本概念
参考资料:https://www.jianshu.com/p/1c483bd8fd6d 在正式学习Spring框架之前,肯定有很多疑问,比如说: 1.Spring中经常出现的IOC.AOP.DI是什么 ...
- 安卓JNI精细化讲解,让你彻底了解JNI(一):环境搭建与HelloWord
目录 1.基础概念 ├──1.1.JNI ├──1.2.NDK ├──1.3.CMake与ndk-build 2.环境搭建 3.Native C++ 项目(HelloWord案例) ├── 3.1.项 ...
- python接口自动化--Excel
1.操作步骤: (1)安装python官方Excel库-->xlrd (2)获取Excel文件位置并读取 (3)读取sheet (4)读取指定rows和cols内容 2.示例代码 # -*- c ...
- 交叉编译sqlcipher
1. 小心预编译宏SQLITE_HAS_CODEC 2. openssl在不同License下,导出的符号不对等.(错了,1.1.0后api发生变化,小心选用openssl版本) 3, ac使用了li ...
- 使用ASP.NET Core 3.x 构建 RESTful API - 3.2 路由和HTTP方法
ASP.NET Core 3.x 的路由 路由机制会把一个请求的URI映射到一个Controller上面的Action,所以当你发送一个HTTP请求的时候,MVC框架会解析这个请求的URI,并尝试着把 ...
- jdbc-mysql测试例子和源码详解
目录 简介 什么是JDBC 几个重要的类 使用中的注意事项 使用例子 需求 工程环境 主要步骤 创建表 创建项目 引入依赖 编写jdbc.prperties 获得Connection对象 使用Conn ...
- 根据本地ip获取地理位置,再根据地理位置,获取天气
import json,requestsfrom urllib.request import urlopenfrom pyquery import PyQuery as pqfrom lxml imp ...
- HTML学习 day04
1.字体.文本声明 声明语句必须要包含在{}号之中: 属性和属性值之间用":"分隔: 当有多个属性时,用":"进行区分: 在书写属性时属性之间使用空格.换行等, ...
- html基础——a标签
a标签:超链接/锚点链接 实现页面跳转 只占据自己内容大小的位置 超链接: 使用 target="_self":表示在本页面跳转到 href 中的地址 target=" ...