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. Arbitrage HDU

    Arbitrage                                              Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. ssh-agent自启动加key脚本

    公司使用到阿里云. 需要使用 ssh-agent forward 来跳转.为了方便自己就写了这个脚本 1 #!/bin/sh 2 # auto start ssh-agent and add key ...

  3. Freemarker日期函数处理【转】

    Freemarker日期函数处理[转] (2012-08-01 14:32:13) 转载▼ 标签: 杂谈   string(当和一个日期值一起使用) 这个内置标签用指定的格式把日期转换成字符串,(把默 ...

  4. Gora快速入门

    概述 Gora是apache的一个开源项目. The Apache Gora open source framework provides an in-memory data model and pe ...

  5. Yum中实现与apt-get install build-essential功能类似的命令

    在Ubuntu中安装完系统后,可以直接使用apt-get install build-essential命令安装常用的开发编译工具包.在诸如CentOS这样的使用Yum包管理的系统中,其实也有类似的实 ...

  6. 织梦dedecms自定义字段在首页列表页文章页的调用

      1.首页调用. {dede:arclist addfields='字段英文名' channelid='模型ID' row='条数' type='栏目ID'}       [field:字段英文名/ ...

  7. apache用户认证、默认主机、301跳转

    我更正论坛一个同学帖子(今天坑我一下午):原文http://www.apelearn.com/bbs/foru ... 3%BB%A7%C8%CF%D6%A4 apache用户认证.默认主机.301跳 ...

  8. 如何从uiview里面,获取其上层的uiviewcontroller

    id object = [self nextResponder]; while (![object isKindOfClass:[UIViewController class]] && ...

  9. android 登录界面

    http://blog.csdn.net/jiabinjlu/article/details/6920967 http://blog.csdn.net/myserverthepeople/articl ...

  10. cf C. Bombs

    http://codeforces.com/contest/350/problem/C 对n个点按曼哈顿距离排序. #include <cstdio> #include <cstri ...