1. 程式人生 > >C. Party Lemonade 很像揹包的貪心

C. Party Lemonade 很像揹包的貪心

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 ≤ 301 ≤ 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的i-1次方),每種商品有價格c[i],問至少買l體積的物品最少需要花費多少錢?

我們利用貪心的方法來解決。  我們優先選擇單位體積花費少的商品。

那麼我們的排序函式為

bool cmp(Node t1,Node t2)
{
    return t1.cost*t2.vol<t2.cost*t1.vol;
}

這是一個很常用的排序方法。

只是我們開始dfs,對於當前狀態,我們用最實惠的貨品一直買,買到買不下來了。還剩下零星的空間。

這個空間我們有兩個選擇,第一個就是再買這個商品,因為題目沒說不能超過給定的空間,只需要保證達到空間

耗費最少即可。所以我們可以這樣。第二個可能就是我們買第二實惠的商品,重複以上過程。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
struct Node
{
    long long vol;
    long long  cost;
}s[1100];
bool cmp(Node t1,Node t2)
{
    return t1.cost*t2.vol<t2.cost*t1.vol;
}
long long n,l;
long long dfs(long long left,long long cost,long long num)
{
    if(num>n)
        return 0;
    long long cout=left/s[num].vol; //判斷當前家需要購買幾瓶
    cost+=cout*s[num].cost;//加上花費
    left-=cout*s[num].vol;//計算剩餘體積
    if(left==0) return cost; //如果不剩餘了,則方案最優,直接返回
    return min(cost+s[num].cost,dfs(left,cost,num+1));//處理剩餘部分,再買一杯超出需要的量或者去下一家店,兩者去最少花費的那種。
}
int main(){
    s[1].vol=1;
    cin>>n>>l;
    cin>>s[1].cost;
    for(int i=2;i<=n;i++){
        cin>>s[i].cost;
        s[i].vol=2*s[i-1].vol;
    }
    sort(s+1,s+1+n,cmp);


//  ll tempm=0;
//  ll left=1;
//  ll temp;
    cout<<dfs(l,0,1)<<endl;
    return 0;
}

相關推薦

C. Party Lemonade 揹包貪心

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 al

C. Party Lemonade(DP,貪心,數位)

題目大意:有n種不同規格的飲料,容量分別為L,並給出每種飲料的價格。現在題目要求至少購買 l升的飲料,最少需要多少錢。思路:首先需要對價格進行一個預處理,如果val[i]*2<val[i+1],那麼是沒有意義的去購買第i+1種飲料,因為顯然購買兩瓶第i種的飲料更加的實惠

Hello 2018-C. Party Lemonade

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

Hello 2018 C. Party Lemonade

C. Party Lemonade time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output A N

Codeforces Hello 2018 C. Party Lemonade(思維)

Description A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lem

Codeforces-Hello 2018C. Party Lemonade(貪心

+= 最小 ont 最小花費 main std ces problem stream 傳送門 N個數,代表得到2^(i-1)次冪的花費,求構造L的最小花費 1 #include <cstdio> 2 #include <cstring>

博格巴:C羅進球就喝水 高興他能加盟尤文

南樂縣 阿里巴巴集團董事局主席馬雲發表致股東的公開信表示:生意難做之時,正是阿里巴巴兌現“讓天下沒有難做的生意”的使命之時。,阿里巴巴(NYSE:BABA)今日釋出了截至2018年9月30日的2019財年第二季度財報(注:阿里巴巴財年與自然年不同步,從每年的4月1日開始,至第二年的3月31日結束)。 財報中

codeforces 913C Party Lemonade貪心 + DFS)

題目大意: 有 n 種檸檬汁,第 i 種每瓶的容量為 2^(i-1) 升,價格為 Ci,問至少買 L 升檸檬汁最少花多少錢? 思路: 一看題,第一感覺是 DP,但是由於購買數可以超過 L 升,並

OpenCV學習C++接口 Mat素遍歷詳解

pos get ++ art details 學習c++ pan detail 接口 OpenCV學習C++接口 Mat像素遍歷詳解OpenCV學習C++接口 Mat像素遍歷詳解

高精度大數c++類模板 好用

details lean 代碼 sprintf printf span tdi 我只 tar 首先聲明這是大佬寫的,我只是記錄下,拿來學習。附上大佬的鏈接 : https://blog.csdn.net/code4101/article/details/2302052

小米第二款區塊鏈產品WiFi鏈,跟網易星球

小米區塊鏈繼小米加密兔之後,小米昨天又上線了第二款區塊鏈產品“小米WiFi鏈”。“小米WiFi鏈”官網目前只有一個網頁,對“小米WiFi鏈”進行了簡單的介紹,還提供了APP的下載。從 加密兔到“小米WiFi鏈”,可以看出小米進軍區塊鏈的決心是十分堅定的。 一、認識“小米WiFi鏈” 小米唐沐在微博表示,“Wi

C#/WPF】圖數據格式轉換時,透明度丟失的問題

csdn pypi 數據類型 acc scan str 圖像 ber release 原文:【C#/WPF】圖像數據格式轉換時,透明度丟失的問題 問題:工作中涉及到圖像的數據類型轉換,經常轉著轉著

c語言數字圖處理(五):空間濾波

element tex play ali p s pla lte ret void 空間濾波原理 使用大小為m*n的濾波器對大小為M*N的圖像進行線性空間濾波,將濾波器模板乘以圖像中對應灰度值,相加得模板中心灰度值

UGUI揹包,使用MVC框架(c#語言),模擬揹包物品載入到自己的揹包

最近也就是了解了下MVC揹包模式,然後試著做了一下,先展示下效果圖吧: 左邊是我虛擬的商店物品,當然你也可以當成另一個倉庫。右邊是另一個倉庫。 左邊的揹包可以看出,是可以隨意換位置的,而且左邊的揹包換位置後放進自己的揹包是不受影響的。 ChessInto程式碼(這段程式碼

C/C++ 演算法分析與設計:貪心(整數配對)

題目描述 江鳥想到一個有趣的問題:給你N個正整數,你可以將這N個整數按兩個一組的方式成對劃分,當然其中的元素也可以不和其他元素配對劃分。現在的問題是,讓劃分為一對的元素的乘積與未配對的元素相加求和,並且讓和最大。比如:考慮這個集合{0,1,2,4,5,3},如果我們讓{0,3}、{2,5}分別成

C/C++ 演算法分析與設計:貪心(排隊接水)

題目描述 N個人同時提水到一個水龍頭前提水因為大家的水桶大小不一,所以水龍頭注滿第i(i=1,2,3......N)個人所需要的時間是T(i) 編寫一個程式,對這N個人使他們花費的時間總和最小,並求出這個時間。 例如有三個人a,b,c,用時分別是2,1,3 排隊順序為c,b,a的時候,c要等

C/C++ 演算法分析與設計:貪心(守望者的逃離)

題目描述 惡魔獵手尤迫安野心勃勃.他背叛了暗夜精靈,率深藏在海底的那加企圖叛變:守望者在與尤迪安的交鋒中遭遇了圍殺.被困在一個荒蕪的大島上。為了殺死守望者,尤迪安開始對這個荒島施咒,這座島很快就會沉下去,到那時,刀上的所有人都會遇難:守望者的跑步速度,為17m/s, 以這樣的速度是無法逃離荒島的

Codeforces Round #375 (Div. 2) C - Polycarp at the Radio 思路+貪心

本場詳細題解見:https://blog.csdn.net/xiang_6/article/details/83549528 題意&思路見上述連結   #include<bits/stdc++.h> using namespace std; #def

走進設計模式的世界7:我們但不是一個人好嗎?-介面卡模式和外觀模式

介面卡模式: 將一個類的介面,轉接成客戶期望的另一個介面。介面卡讓原本介面不相容的類可以合作無間。 外觀模式: 提供了一個統一的介面,用來訪問子系統中的一群介面。外觀定義了一個高層介面,讓子系統更容易使用。 設計原則:最少知識原則:只和你的密友談話。 解釋:當需要使用一個現有

C++知識整理 - 基礎的基礎

主要內容 基於過程的程式設計 C++的輸入和輸出 C++的函式 1. 內建函式 2. 過載函式 3. 函式模板 4. 帶有預設引數的函式 5. 內部函式 6. 外部函式   基於過程的程式設計 特點:程式必須告訴計算機具