3道经典的优先队列题

优先队列是一种十分强大的数据结构,它保持了一种动态的有序性,对于不断改变有入队的操作,而又需要某种最大或最小的操作的问题是再合适不过了,通常优先队列的实现是由最小堆或者最大堆完成的,并通过堆排序保持队列的有序性,模拟队列的结构,在实际比赛中要写一个堆排序还是要一定的时间的,但是stl中queue容器中已经可以实现优先队列,下面以三道基本的题目来演示priority_queue的作用。

聪明的木匠  
Time Limit: 1000ms, Special Time Limit: 2500ms, Memory Limit: 32768KB  
Total submit users: 23, Accepted users: 10  
Problem 10611 : No special judgement  
Problem description  
    最近,一位老木匠遇到了一件非常棘手的问题。他需要将一根非常长的木棒切成N 段。每段的长度分别为 L1 ,L2 ,…,LN ( 1≤L1 ,L2 ,…,LN ≤1000 ,且均为整数)个长度单位。 ∑Li (i=1,2,…,N) 恰好就是原木棒的长度。我们认为切割时仅在整数点处切且没有木材损失。 
  木匠发现,每一次切割花费的体力与该木棒的长度成正比,不妨设切割长度为 1 的木棒花费 1 单位体力。例如,若 N=3 , L1 =3,L2 =4,L3 =5 ,则木棒原长为 12 ,木匠可以有多种切法,如:先将 12 切成 3+9. ,花费 12 体力,再将 9 切成 4+5 ,花费 9 体力,一共花费 21 体力;还可以先将 12 切成 4+8 ,花费 12 体力,再将 8 切成 3+5 ,花费 8 体力,一共花费 20 体力。显然,后者比前者更省体力。 
  那么,木匠至少要花费多少体力才能完成切割任务呢? 

 
Input  
  输入数据的第一行为一个整数N(2≤N≤150,000) ; 
在接下来的 N 行中,每行为一个整数 Li (1≤Li ≤1000) 。 

 
Output  
  输出数据仅有一行,为一个整数,表示木匠最少要花费的体力。测试数据保证这个整数不大于231 -1 。 

 
Sample Input  
4
3
5
7
11
 
Sample Output  
49 
Problem Source  
  2010年河北大学程序设计竞赛 
 

 

最优的方法是每次切出最小的合并,显然要使用优先队列;

[cpp:firstline[1]] view plaincopyprint?#include    
#include    
struct cmp  
{  
    bool operator ()(const int &i,const int &j)  
    {  
        return i>j;  
    }  
};  
using namespace std;  
int main()  
{  
    priority_queue,cmp> s;  
    int n;  
    while(cin>>n)  
    {  
        for(int i=0;i=2)  
        {  
            int a,b;  
            a=s.top();  
            s.pop();  
            b=s.top();  
            s.pop();  
            s.push(a+b);  
            //cout<
#include 
struct cmp
{
    bool operator ()(const int &i,const int &j)
    {
        return i>j;
    }
};
using namespace std;
int main()
{
    priority_queue,cmp> s;
    int n;
    while(cin>>n)
    {
        for(int i=0;i=2)
        {
            int a,b;
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            s.push(a+b);
            //cout<   
#include    
struct cmp  
{  
    bool operator ()(const int &i,const int &j)  
    {  
        return i>j;  
    }  
};  
using namespace std;  
int main()  
{  
    priority_queue,cmp> s;  
    int n;  
    while(cin>>n)  
    {  
        for(int i=0;i=2)  
        {  
            int a,b;  
            a=s.top();  
            s.pop();  
            b=s.top();  
            s.pop();  
            s.push(a+b);  
            //cout<
#include 
struct cmp
{
    bool operator ()(const int &i,const int &j)
    {
        return i>j;
    }
};
using namespace std;
int main()
{
    priority_queue,cmp> s;
    int n;
    while(cin>>n)
    {
        for(int i=0;i=2)
        {
            int a,b;
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            s.push(a+b);
            //cout<   
#include    
struct cmp  
{  
    bool operator ()(const int &i,const int &j)  
    {  
        return i>j;  
    }  
};  
using namespace std;  
int main()  
{  
    int b[10003];  
    priority_queue,cmp> s;  
    int n;  
    while(cin>>n)  
    {  
        for(int i=0;i
#include 
struct cmp
{
    bool operator ()(const int &i,const int &j)
    {
        return i>j;
    }
};
using namespace std;
int main()
{
    int b[10003];
    priority_queue,cmp> s;
    int n;
    while(cin>>n)
    {
        for(int i=0;i

priority_queue  q;

这是按照从大到小的队列;

#include 

struct cmp

{

    bool operator ()(const int &i,const int &j)

    {

        return i>j;

    }

};

priority_queue  q;

这是从小到大的优先队列;

你可能感兴趣的:(队列)