FZU1327 优先队列
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
Output
Sample Input
Sample Output
#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 优先队列的更多相关文章
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 数据结构作业——Sanji(优先队列)
山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
- Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
- 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)
ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...
随机推荐
- 1 Intellij IDEA 个人常用快捷方式
个人常用快捷方式 alt+enter 万能键 inn if(xxx!=null) iter foreach ct ...
- keil中查看内存数据
1.工具栏中 view->Memory Windows 然后 c:0 表示读取0地址开始的代码区数据 d:0 表示读取0地址开始的数据区数据 x:0表示读取0地址开始的外部数据区
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
- 三个重要的游标sp_cursoropen
請問這三個存諸過程的作用是什么﹖ sp_cursoropen, sp_cursorfetch, sp_cursorclose API 服务器游标实现 SQL Server OLE DB 提供程序. ...
- window.onload,<body onload="function()">, document.onreadystatechange, httpRequest.onreadystatechang
部分内容参考:http://www.aspbc.com/tech/showtech.asp?id=1256 在开发的过程中,经常使用window.onload和body onload两种,很少使用do ...
- ext4 grid edit 添加删除行
extjs--grid动态添加一行和删除一行: (1)选择rowEditing时,添加一行后的编辑方式为----startEdit(record,columnHeader) (2)选择cellEdit ...
- CSS实现背景透明/半透明效果的方法
全透明代码:{background:transparent} 半透明代码:{filter:alpha(opacity=80);-moz-opacity:0.8;width:auto !importan ...
- 【6】python核心编程 第九章-文件和输入输出
1.文件对象的内建方法列表 文件对象的方法 操作 file.close() 关闭文件 file.fileno() 返回文件的描述符(file descriptor ,FD, 整数值) file.flu ...
- 开心菜鸟系列----变量的解读(javascript入门篇)
console.info( console.info(window['weiwu']) console.info(window. ...
- VirtualBox修改虚拟盘路径
VirtualBox虚拟盘路径默认是存在C盘的,而当我们发现C盘不够用的时候,想转移就感觉有点麻烦了,现在给大家介绍一个简单又使用的方法. 第一步:到默认目录C:\Users\Administrato ...