Problem 1327 Blocks of Stones II

Accept: 318    Submit: 881
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

There are n blocks of stones in a line laying on the ground. Now you are to merge these blocks of stones together with the restriction that you can only merge the neighbouring blocks each time. The score you get for each merging is the number of stones the new blocks has.

Before each merging, you are allowed to swap any neighbouring blocks for any times. You are to calculate the minimal score you will get during the whole process.

Input

There are multiple test cases. For each case, the first line contains a integer n(n<=10000), representing the number of blocks. The second line contains n integers, representing number of stones in each blocks.

Output

For each case, output one line containing a single integer, representing the minimal score. The results are within 32bit integers.

Sample Input

3
2 5 1

Sample Output

11
题目大意:给你很多堆石头,让你合并这些石头,每次只能合并两堆,且你会得到相当于两堆石头总量的分数,让你将石头合并为一堆,求在这个过程中你得到的最小的分数。
思路分析:典型贪心题目,每次合并两堆最小的石头,得到的分数是最少的,思路很简单,但是在实现的时候如果是合并一次排序一次,那么时间复杂度将是n^2log2n,会TLE,
对于本题,我们要充分运用优先队列的性质,内部有序,建立一个最小堆,然后用这个最小堆来模拟石头的合并,效率将大大提高,做这道题的过程也就相当于我优先队列的学习
过程了,新知识get.
代码:#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;//建立最小堆
const int maxn=10000+5;
int a[maxn];
int main()
{
    int n,i;
    while(cin>>n)
    {
        int sum=0;
        for(i=0;i<n;i++)
cin>>a[i];
        for(i=0;i<n;i++)
            q.push(a[i]);
        int s,k;
        while(q.size()!=1)
        {
            s=q.top();
            q.pop();
            s+=q.top();
            q.pop();
            sum+=s;
            q.push(s);
        }
        cout<<sum<<endl;
            q.pop();
    }
    return 0;
}

FZU1327 优先队列的更多相关文章

  1. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  2. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  3. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  4. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  5. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  6. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  7. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  8. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...

随机推荐

  1. AfxOleInit()和::CoInitialize(NULL)区别

    From: http://blog.csdn.net/zhoubl668/archive/2009/04/30/4139933.aspx OLE是建立在COM之上的技术,层次比COM要高.AfxOle ...

  2. 1169 二叉树遍历(XCOJ DFS)

    给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 样例输入 BADC BDCA 样例输出 ABCD #include <iostream> ...

  3. DataTables选择多行

    $(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...

  4. head直接复制的

    <script type="application/x-javascript"> addEventListener("load", function ...

  5. Shell脚本调试技术

    http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ 一. 前言 shell编程在unix/linux世界中使用得非常广泛,熟练掌握 ...

  6. C#代码计时

    using System.Diagnostics; Stopwatch sw = new Stopwatch(); sw.Start(); //todo code ....... sw.Stop(); ...

  7. Qt判断和打开进程(windows端),运行,检测,中止

    windows端的Qt程序往往需要打开外部程序(如:prog.exe),并且需要确定这个外部程序是唯一打开的. 1.判断系统中是否存在prog.exe void judge() { QProcess ...

  8. Delphi 实现任务栏多窗口图标显示(使用WS_EX_APPWINDOW风格)

    uses Windows;type TfrmLogin = class(TForm) end; implementation {$R *.dfm} procedure TfrmLogin.FormCr ...

  9. MVC 自定义错误处理

    1. Application_Error namespace Libaray.Web{ public class MvcApplication : System.Web.HttpApplication ...

  10. Jquery局部打印插件

    局部打印插件 jquery.PrintArea.js js代码 (function ($) {     var printAreaCount = 0;     $.fn.printArea = fun ...