Argus Beijing 2004 LA3135

【题目讲解】

本题就是简单模拟优先队列的使用。

下面讲讲队列及优先队列的基础知识:

先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。(>表示优先级小的在前,<表示优先级大的在前)

优先队列有两种,一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素分别是优先级最大和优先级最小的元素。

1) 优先队列的定义

包含头文件:"queue.h", "functional.h"

可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。

2) 优先队列的常用操作

优先级队列支持的操作

q.empty()         如果队列为空,则返回true,否则返回false

q.size()            返回队列中元素的个数

q.pop()             删除队首元素,但不返回其值

q.top()             返回具有最高优先级的元素值,但不删除该元素

q.push(item)     在基于优先级的适当位置插入新元素

其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快

另外,在优先队列中,元素可以具有相同的优先权。

下面这个C例子,包含了几乎所有常见的优先队列用法。

  1 #include<iostream>
  2 #include<functional>
  3 #include<queue>
  4 #include<vector>
  5 using namespace std;
  6
  7 //定义比较结构
  8 struct cmp1{
  9     bool operator ()(int &a,int &b){
 10         return a>b;//最小值优先
 11     }
 12 };
 13
 14 struct cmp2{
 15     bool operator ()(int &a,int &b){
 16         return a<b;//最大值优先
 17     }
 18 };
 19
 20 //自定义数据结构
 21 struct number1{
 22     int x;
 23     bool operator < (const number1 &a) const {
 24         return x>a.x;//最小值优先
 25     }
 26 };
 27 struct number2{
 28     int x;
 29     bool operator < (const number2 &a) const {
 30         return x<a.x;//最大值优先
 31     }
 32 };
 33 int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
 34 number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
 35 number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};
 36
 37 int main()
 38 {
 39     priority_queue<int>que;//采用默认优先级构造队列
 40
 41     priority_queue<int,vector<int>,cmp1>que1;//最小值优先
 42     priority_queue<int,vector<int>,cmp2>que2;//最大值优先
 43
 44     priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
 45     priority_queue<int,vector<int>,less<int> >que4;////最大值优先
 46
 47     priority_queue<number1>que5; //最小优先级队列
 48     priority_queue<number2>que6;  //最大优先级队列
 49
 50     int i;
 51     for(i=0;a[i];i++){
 52         que.push(a[i]);
 53         que1.push(a[i]);
 54         que2.push(a[i]);
 55         que3.push(a[i]);
 56         que4.push(a[i]);
 57     }
 58     for(i=0;num1[i].x;i++)
 59         que5.push(num1[i]);
 60     for(i=0;num2[i].x;i++)
 61         que6.push(num2[i]);
 62
 63
 64     printf("采用默认优先关系:/n(priority_queue<int>que;)/n");
 65     printf("Queue 0:/n");
 66     while(!que.empty()){
 67         printf("%3d",que.top());
 68         que.pop();
 69     }
 70     puts("");
 71     puts("");
 72
 73     printf("采用结构体自定义优先级方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n");
 74     printf("Queue 1:/n");
 75     while(!que1.empty()){
 76         printf("%3d",que1.top());
 77         que1.pop();
 78     }
 79     puts("");
 80     printf("Queue 2:/n");
 81     while(!que2.empty()){
 82         printf("%3d",que2.top());
 83         que2.pop();
 84     }
 85     puts("");
 86     puts("");
 87     printf("采用头文件/"functional/"内定义优先级:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n");
 88     printf("Queue 3:/n");
 89     while(!que3.empty()){
 90         printf("%3d",que3.top());
 91         que3.pop();
 92     }
 93     puts("");
 94     printf("Queue 4:/n");
 95     while(!que4.empty()){
 96         printf("%3d",que4.top());
 97         que4.pop();
 98     }
 99     puts("");
100     puts("");
101     printf("采用结构体自定义优先级方式二:/n(priority_queue<number>que)/n");
102     printf("Queue 5:/n");
103     while(!que5.empty()){
104         printf("%3d",que5.top());
105         que5.pop();
106     }
107     puts("");
108     printf("Queue 6:/n");
109     while(!que6.empty()){
110         printf("%3d",que6.top());
111         que6.pop();
112     }
113     puts("");
114     return 0;
115 }
116 /*
117 运行结果 :
118 采用默认优先关系:
119 (priority_queue<int>que;)
120 Queue 0:
121 83 72 56 47 36 22 14 10  7  3
122
123 采用结构体自定义优先级方式一:
124 (priority_queue<int,vector<int>,cmp>que;)
125 Queue 1:
126  7 10 14 22 36 47 56 72 83 91
127 Queue 2:
128 83 72 56 47 36 22 14 10  7  3
129
130 采用头文件"functional"内定义优先级:
131 (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
132 Queue 3:
133  7 10 14 22 36 47 56 72 83 91
134 Queue 4:
135 83 72 56 47 36 22 14 10  7  3
136
137 采用结构体自定义优先级方式二:
138 (priority_queue<number>que)
139 Queue 5:
140  7 10 14 22 36 47 56 72 83 91
141 Queue 6:
142 83 72 56 47 36 22 14 10  7  3
143 */

本题目的代码:

#include<iostream>
#include<queue>
using namespace std;
struct  Item
{
  int Qnum,Period,Time;
  bool operator <(const Item& a) const
  {
      return Time >a.Time||(Time==a.Time&&Qnum>a.Qnum);
  }
} ;
int main()
{
    priority_queue <Item>pq;//设置有限队列
    char s[20];
    while (scanf("%s",s)&&s[0]!=‘#‘)
    {
        Item  item;
        scanf("%d%d",&item.Qnum,& item.Period);
        item.Time=item.Period;
        pq.push(item);
    }
    int k;
    scanf("%d",&k);
    while(k--)
    {
        Item r=pq.top();
        pq.pop();
        printf("%d\n",r.Qnum);
        r.Time+=r.Period;
        pq.push(r);//将r插入合适位置此时r的time已经变化
    }
    return 0;
}
时间: 2024-12-27 21:41:29

Argus Beijing 2004 LA3135的相关文章

poj2051&amp;&amp;UVALive 3135 水题

http://poj.org/problem?id=2051 Argus Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9716   Accepted: 4543 Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traff

POJ 2057 The Lost House

The Lost House Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 205764-bit integer IO format: %lld      Java class name: Main One day a snail climbed up to a big tree and finally came to the end of a branch. W

poj1922

Ride to School Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18704   Accepted: 7552 Description Many graduate students of Peking University are living in Wanliu Campus, which is 4.5 kilometers from the main campus – Yanyuan. Students i

《sed的流艺术之三》-linux命令五分钟系列之二十三

本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. === [正文开始] 上文接:<sed的流艺术之二>-linux命令五分钟系列之二十二 例五 给某些字符串后面插入些内容 [[email protected] programming]$ cat mysed.txt Beijing London[[email protected] programm

TJU Problem 1090 City hall

注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型. 原题: 1090.   City hall Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 4874   Accepted Runs: 2395 Because of its age, the City Hall has suffered damage to one of its walls. A matrix with M rows an

poj 1925

Spiderman Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6232   Accepted: 1221 Description Dr. Octopus kidnapped Spiderman's girlfriend M.J. and kept her in the West Tower. Now the hero, Spiderman, has to reach the tower as soon as he c

LA-3135 - Argus(优先队列)

3135 - Argus A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensordata, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logsand telephone call records. Likewise

UVALive - 3135 - Argus (优先队列!!)

UVALive - 3135 Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financi

Winter-2-STL-A Argus 解题报告及测试数据

Time Limit:2000MS     Memory Limit:65536KB Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usa