博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
队列的基本使用方法
阅读量:5141 次
发布时间:2019-06-13

本文共 2623 字,大约阅读时间需要 8 分钟。

#include <iostream>

#include <queue>

#include <assert.h>

/*

调用的时候要有头文件: #include<stdlib.h> #include<cstdlib> +

#include<queue>       #include<queue>

详细用法:

定义一个queue的变量     queue<Type> M

查看是否为空范例        M.empty()    是的话返回1,不是返回0;

从已有元素后面增加元素   M.push()

输出现有元素的个数      M.size()

显示第一个元素          M.front()

显示最后一个元素        M.back()

清除第一个元素          M.pop()

*/

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

queue <int> myQ;

 

cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl;

 

for(int i =0; i<10 ; i++)

{

myQ.push(i);

}

for(int i=0; i<myQ.size(); i++)

{

printf("myQ.size():%d\n",myQ.size());

cout << myQ.front()<<endl;

myQ.pop();

}

 

system("PAUSE");

 

return 0;

}

 

输出结果:

现在 queue 是否 empty? 1

myQ.size():10

0

myQ.size():9

1

myQ.size():8

2

myQ.size():7

3

myQ.size():6

4

请按任意键继续. . .

下面给出一个例子:ZOJ 3516

Tree of Three


Time Limit: 2 Seconds     
Memory Limit: 65536 KB


Now we have a tree and some queries to deal with. Every node in the tree has a value on it. For one node A, we want to know the largest three values in all the nodes of the subtree whose root is node A. Node 0 is root of the tree, except it, all other nodes have a parent node.

Input

There are several test cases. Each test case begins with a line contains one integer n(1 ≤ n ≤ 10000), which indicates the number of the node in the tree. The second line contains one integer v[0], the value on the root. Then for the following n - 1 lines(from the 3rd line to the (n + 1)th line), let i + 2 be the line number, then line i + 2 contains two integers parent and v[i], here parent is node i's parent node, v[i] is the value on node i. Here 0 ≤ v[i] ≤ 1000000. Then the next line contains an integer m(1 ≤ m ≤ 10000), which indicates the number of queries. Following m lines, each line contains one integer q, 0 ≤ q < n, it meas a query on node q.

Output

For each test case, output m lines, each line contains one or three integers. If the query asked for a node that has less than three nodes in the subtree, output a "-1"; otherwise, output the largest three values in the subtree, from larger to smaller.

Sample Input

510 100 52 72 8501234

Sample Output

10 8 7-18 7 5-1-1

解题思路:这个题目刚开始的时候没想到要用队列来做。。一直在想树。。到后来才想到。。

这个题是给你一颗树,然后问你这些节点上是否有大于3个子孙,如果有就输出最大的3个,

如果没有就输出-1;

下面是代码:

#include 
#include
#include
#include
#include
using namespace std; int cmp(int a,int b) {
 if(a>b)   return 1;  else   return  0; } vector
que[10005]; int n,a[10005],s[10005],x,y,j; void dfs(int node) {  int i;  s[j++]=a[node];  for(i=0;i

 

转载于:https://www.cnblogs.com/Mu-Tou/archive/2011/08/10/2133883.html

你可能感兴趣的文章
实验四2
查看>>
Android现学现用第十一天
查看>>
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
【luogu4185】 [USACO18JAN]MooTube [并查集]
查看>>
手机号脱敏处理
查看>>
CI控制器调用内部方法并载入相应模板的做法
查看>>
Hdu - 1002 - A + B Problem II
查看>>
HDU - 2609 - How many
查看>>
每天CookBook之Python-003
查看>>
每天CookBook之Python-004
查看>>
Android设置Gmail邮箱
查看>>
StringBuffer的用法
查看>>