#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
//#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
//priority_queue<int> q;
class priority_queue
{
private:
vector<int> data;
public:
void push(int t)
{
data.push_back(t);
push_heap(data.begin(), data.end());
}
void pop()
{
pop_heap(data.begin(), data.end());
data.pop_back();
}
int top() {return data.front();}
int size() {return data.size();}
bool empty() {return data.empty();}
};
int main()
{
priority_queue test;
test.push(3);
test.push(5);
test.push(2);
test.push(4);
while(!test.empty()) {cout << test.top() << endl; test.pop();}
// 5 4 3 2
return 0;
}
</pre><pre code_snippet_id="462650" snippet_file_name="blog_20140902_3_9681810" name="code" class="cpp">
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
//priority_queue<Type, Container, Functional>
//三个参数分别是数据类型,容器,比较函数
//后面两个缺省的话,第二个为vector, 第三个为大元素优先,即为大顶堆
int main()
{
int i, n = 10;
/*大顶堆
priority_queue<int> q;
rep(i, 1, n) q.push(rand());
while(!q.empty())
{
cout << q.top() << " ";
q.pop();
}
*/
//小顶堆,要写三个参数,STL可以用greater<>
priority_queue<int, vector<int>, greater<int> > q;
rep(i, 1, n) q.push(rand());
while(!q.empty())
{
cout << q.top() << " ";
q.pop();
}
return 0;
}
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
struct Node
{
int x, y;
Node(int a = 0, int b = 0)
:x(a), y(b) {}
};
//对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
bool operator<(Node a, Node b)
{
return a.x > b.x || (a.x == b.x && a.y > b.y);
}
//x小的优先级高,x相等的时候y小的优先级高
int main()
{
int i, n = 10;
priority_queue<Node> q;
rep(i, 1, n) q.push(Node(rand(), rand()));
while(!q.empty())
{
cout << q.top().x << " " << q.top().y << endl;
q.pop();
}
return 0;
}
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
struct Node
{
int x, y;
Node(int a = 0, int b = 0)
:x(a), y(b) {}
};
struct cmp
{
bool operator() (Node a, Node b)
{
return a.x > b.x || (a.x == b.x && a.y > b.y);
}
};
int main()
{
int i, n = 10;
priority_queue<Node, vector<Node>, cmp> q;
rep(i, 1, n) q.push(Node(rand(), rand()));
while(!q.empty())
{
cout << q.top().x << " " << q.top().y << endl;
q.pop();
}
return 0;
}
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
// 使用友元函数
struct Node
{
int x, y;
Node(int a = 0, int b = 0)
:x(a), y(b) {}
friend bool operator<(Node a, Node b)
{
return a.x > b.x || (a.x == b.x && a.y > b.y);
}
};
//对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
//x小的优先级高,x相等的时候y小的优先级高
int main()
{
int i, n = 10;
priority_queue<Node> q;
rep(i, 1, n) q.push(Node(rand(), rand()));
while(!q.empty())
{
cout << q.top().x << " " << q.top().y << endl;
q.pop();
}
return 0;
}