優先隊列的使用

來源:互聯網
上載者:User

標籤:

windows live writer

終於安裝好了windows live writer.(不知道什麼問題一直出問題)。簡直完美!

試一下的確很爽,先來一道優先隊列壓壓驚!

題目描述

最近,Nova君遇到了一件非常棘手的問題。他需要整理非常多的解題報告。每份解題報告的題目數量是不定的。Nova君每次需要將兩份報告的題目解析合成到一份裡。假設兩份報告的題解數分別為a和b,那麼合成這兩份報告消耗Nova君a+b的hp值。現在有n份報告,題解數分別為a0,a1,a2,,,an-1,請問Nova最少消耗多少hp?

輸入

多組測試資料。每組資料,第一行為一個正整數n(INT範圍內),代表報告份數,接下來一行,包含n個正整數(INT範圍內)a0,a1,a2,,,an-1,代表每份報告的題解數。

輸出

對於每組資料,輸出一行,代表Nova需要付出的最少的hp值

輸入範例
43 5 7 11
輸出範例
49
Hint
陳題,然而請用優先隊列實現

 

來源: <http://biancheng.love/contest/10/problem/A/index>

 代碼實現:

 1 #include <bits/stdc++.h> 2 #define max_size 1000010 3  4 using namespace std; 5 int a[max_size]; 6  7 struct cmp{ 8     bool operator()(int &a,int &b){ 9     return a>b;10     }11 };12 13 int main(){14     priority_queue<int,vector<int>,cmp>q;15     int n;16     while(~scanf("%d",&n)){17         long long ans=0;18         int num;19         for(int i=1;i<=n;i++){20             scanf("%d",&num);21             q.push(num);22         }23         int key,a,b;24         while(!q.empty()){25         a=q.top();26         q.pop();27         if(q.empty())break;28         b=q.top();29         q.pop();30         ans+=a+b;31         key=a+b;32         q.push(key);33         }34         cout<<ans<<endl;35     }36 }

優先隊列的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.