程序设计竞赛数据结构知识—队列

2016-03-23

队列与STL

C++的STL里面已经内置了队列，使用时调用queue头文件。使用时像是操作基本类型一样，具体方法参考C++API，下面简单介绍一下基本的使用方法。

Queue

#include <iostream>
#include <queue>

using namespace std;

int main ()
{
queue<int> myqueue;
int myint;
cout << "Please enter some integers (enter 0 to end):\n";

do {
cin >> myint;
myqueue.push (myint);
} while (myint);

cout << "myqueue contains: ";

while (!myqueue.empty())
{
cout << " " << myqueue.front();
myqueue.pop();
}
return 0;
}

Priority Queue(优先队列)

priority_queue<int> qi;

priority_queue<int, vector<int>, greater<int> >qi2;

struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};

9          5
8          2
6          1
2          3
1          4

代码

#include<iostream>
#include<functional>
#include<queue>

using namespace std;

struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};

int main()
{
const int len = 5;
int i;
int a[len] = {3,5,9,6,2};

//示例1
priority_queue<int> qi;
for(i = 0; i < len; i++)
qi.push(a[i]);
for(i = 0; i < len; i++)
{
cout<<qi.top()<<" ";
qi.pop();
}
cout<<endl;

//示例2
priority_queue<int, vector<int>, greater<int> >qi2;
for(i = 0; i < len; i++)
qi2.push(a[i]);
for(i = 0; i < len; i++)
{
cout<<qi2.top()<<" ";
qi2.pop();
}

cout<<endl;

//示例3
priority_queue<node> qn;
node b[len];
b[0].priority = 6; b[0].value = 1;
b[1].priority = 9; b[1].value = 5;
b[2].priority = 2; b[2].value = 3;
b[3].priority = 8; b[3].value = 2;
b[4].priority = 1; b[4].value = 4;

for(i = 0; i < len; i++)
qn.push(b[i]);

cout<<"优先级"<<'\t'<<"值"<<endl;

for(i = 0; i < len; i++)
{
cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
qn.pop();
}
return 0;
}

例题1：hdu 1896 stones

Problem Description

Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first.

Input

In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.
For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.

Output

Just output one line for one test case, as described in the Description.

Sample Input

2

2

1 5

2 4

2

1 5

6 6

Sample Output

11

12

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct stone{int pos , dis;};
struct compare
{
bool operator() (const stone & s1 , const stone & s2)
{
if(s1.pos != s2.pos){return s1.pos > s2.pos;}
return s1.dis > s2.dis;
}
};
int main()
{
int t;
cin >> t;
while(t --)
{
int n;
cin >> n;
priority_queue<stone , vector<stone> , compare> record;
for(int i = 1; i <= n; ++i)
{
stone s;
cin >> s.pos >> s.dis;
record.push(s);
}
int cnt = 1;
while(!record.empty())
{
stone t = record.top();
record.pop();
if(cnt % 2)
{
t.pos += t.dis;
record.push(t);
}
if(record.empty()){cout << t.pos << endl;}
++cnt;
}
}
return 0;
}

例题2

Hdu 1242 rescue

Problem Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input

7 8

#.#####.

#.a#..r.

#..#x...

..#..#.#

#...##..

.#......

........

Sample Output

13

#include <stdio.h>
#include <string.h>
#include <queue>

#define maxn 202
using std::priority_queue;

int n, m, ans;
const int mov[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
char map[maxn][maxn];

struct Node{
int x, y, time;
friend bool operator<(Node a, Node b){
return a.time > b.time;
}
};

bool check(int x, int y){
return x >= 0 && x < n && y >= 0
&& y < m && map[x][y] != '#';
}

bool BFS(int x, int y)
{
Node now, tmp;
now.x = x; now.y = y;
map[x][y] = '#';
now.time = 0;
priority_queue<Node> Q;
Q.push(now);
while(!Q.empty()){
now = Q.top(); Q.pop();
for(int i = 0; i < 4; ++i){
tmp = now;
tmp.x += mov[i][0];
tmp.y += mov[i][1];
if(check(tmp.x, tmp.y)){
++tmp.time;
if(map[tmp.x][tmp.y] == 'x')
++tmp.time;
else if(map[tmp.x][tmp.y] == 'r'){
ans = tmp.time; return true;
}
map[tmp.x][tmp.y] = '#';
Q.push(tmp);
}
}
}
return false;
}

int main()
{
int i, j, x, y;
while(scanf("%d%d", &n, &m) == 2){
for(i = 0; i < n; ++i){
getchar();
for(j = 0; j < m; ++j){
map[i][j] = getchar();
if(map[i][j] == 'a'){
x = i; y = j;
}
}
}
if(BFS(x, y)) printf("%d\n", ans);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}