题目链接:Click Here
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.
题意:n种杯子,每一种无穷多个,每一种容积不同,呈2的指数次幂上升,且价格不同。问买体积为L的柠檬水,需要最少多少钱。
分析:贪心 ,递归
第i种的容积是第i-1种的2倍,所以可根据此更新价格。若买两瓶第i-1种比买一瓶第i种便宜,那就用花费少的那个。价格数组c[i],c[i]=min(c[i],2*c[i-1]),从n到30的c[i]=c[i-1]*2.
柠檬水的容积都是2的指数次幂。c[i]每一步都是更优,容积指数越大越好,所以先用循环找到容积指数尽可能大的情况,即小于L的最大2^i,确定一个i,再依次递归往下找指数小的。
取完一个i的剩余的容积x,比x大的最小的容积2^j,在j到i之间,选出最小的一个就好了。
最后注意,可能一开始就取容量大于L的是更优的情况,所以循环从flag到30或许可以找出最小的结果哦,min更新cnt。
这里写代码片
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
LL c[35];
LL dfs(LL x) //递归
{
int pos=31;
for(int i=0;i<=30;i++)
{
if(x<(1<<i))
{
pos=i-1;//pos记录比L小的最大的容量指数
break;
}
}
if(x==(1<<pos)) //当前所需容量等于pos所指容量,返回其价格
return c[pos];
//不相等时,说明还有剩余哦,
LL res=x-(1<<pos); //所需容量比pos所指容量多的部分
LL sum=dfs(res); //对这一部分进行递归,
int pos1=31;
for(int i=0;i<=30;i++)
{
if(res<(1<<i))
{
pos1=i-1;//pos1记录比res小的最大容量指数
break;
}
}
for(int i=pos1+1;i<=pos;i++) //在pos1+1~pos之间c[i]和sum,找较小者,更新c[i]
sum=min(sum,c[i]);
return c[pos]+sum;
} //递归最后返回容量x在0~k中所需的最小的价格
int main()
{
int n;
LL L;
while(cin>>n>>L)
{
for(int i=0;i<n;i++)
cin>>c[i];
for(int i=1;i<n;i++)
c[i]=min(c[i],c[i-1]*2); //更新为更优
for(int i=n;i<=30;i++)
c[i]=c[i-1]*2;//更新,体积是2倍,价格也是呢
int flag=31;
for(int i=0;i<=30;i++)
{
if(L<(1<<i))
{
flag=i; //循环找到第一个大于L的容积对应的二次指数,用flag记录
break;
}
}
LL cnt=dfs(L); //递归找到0~flag最小的花费
for(int i=flag;i<=30;i++) //杯子容量比L大时,是否存在更小的花费呢
cnt=min(cnt,c[i]);
cout<<cnt<<endl;
}
return 0;
}