题目大意:Framer John有一段木板,想用以建筑围墙,没有锯子的他仅仅好向Framer Don求助。FD提出要求,FJ每截开一段木板,就要给这段木板长度的钱。FJ想使花费最少,向你求助。
思路:绝对是合并果子的翻版!
把截木板当成合并木板即可了。
小心L、n的范围,终于的ans用int装是装不下的。要用long long。
代码例如以下:
#include#include #include using namespace std;int n;priority_queue q;void init(){ scanf("%d",&n); int tmp; for (int i=1;i<=n;++i) { scanf("%d",&tmp); q.push(-tmp); }}void work(){ int ans=0; while (!q.empty()) { int a=q.top(); q.pop(); if (q.empty()) { printf("%lld",-ans); return; } int b=q.top(); q.pop(); ans+=(a+b); q.push(a+b); }}int main(){ init(); work(); return 0;}