FJUT - OJ优先队列专题题解
- 题目链接http://120.78.128.11/Contest.jsp?cid=18
- 题面不贴了
- 都是英文题,看的我心力憔悴 =7=
一、Ugly Numbers
- 题目说一个数的质因数只包含2、3或者5(一个或多个),就是丑陋数。拜托,为啥这些数就丑陋了。然后题目特别说明第一个丑陋数是1
- 题目多组数据输入到0为止,然后输出第n个丑陋数。
- 解题思路就是对于一个丑陋数k,那么一定有2*k、3*k和5*k也是丑陋数,所以按照这个思路入队模拟打表即可
- 值得注意的是,对于2*k1来说可能和 3*k2重复,例如2*12 = 3*8;所以在入队的时候不能让重复元素入队。
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; priority_queue<ll,vector<ll>,greater<ll> >q;
vector<ll>p(); void init(){
q.push();
int num = ;
for(int i = ; i <= ; i++){
ll sum = q.top();
q.pop();
p[num++] = sum;
ll a = sum*, b = sum*, c = sum*;
if((a % ) && (a % ))
q.push(a);
if(b % )
q.push(b);
q.push(c);
}
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n;
init();
while(cin>>n && n){
cout << p[n] << endl;
} return ;
}
二、The kth great number
- 题意就是第一行输入n,k,表示下面有n行操作,I a表示把a入队,Q表示查询并输出当前数字中第k大的数。
- 用优先队列存k个元素,当已经存了k个元素,新元素进入再出队就行了。
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; struct node{
int num;
friend bool operator < (node a,node b){
return a.num > b.num;
}
}temp; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n,k;
while(~scanf("%d%d",&n,&k)){
priority_queue<node>q;
for(int i = ; i < n; i++){
char tp;
int num;
scanf(" %c",&tp);
if(tp == 'I'){
scanf("%d",&num);
if(q.size() >= k){
if(num > q.top().num){
q.pop();
temp.num = num;
q.push(temp);
}
}
else{
temp.num = num;
q.push(temp);
}
}
else{
int res = q.top().num;
cout << res << endl;
}
}
} return ;
}
三、Fence Repair
- 题面坑死我了,好半天才搞懂题意,就是第一行n,然后下面n个数,表示要把原木头锯成这么n段。然后每一次锯木头就消耗木头长度的钱,问最少要多少钱。
- 思路是递归的思路,从最短的开始往回推,因为设锯开后的长度分别为a,b,肯定要保证a+b最小,才能消耗最少。
- 所以就是从最小和次小的开始,加起来入队,同时消耗的金钱也要加上这个和。
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n;
while(cin>>n){
priority_queue<int, vector<int>,greater<int> >q;
ll ans = ,num,a,b;
for(int i = ; i < n; i++){
cin>>num;
q.push(num);
}
while(q.size() > ){
a = q.top();
q.pop();
b = q.top();
q.pop();
q.push(a+b);
ans += a+b;
}
cout << ans << endl;
} return ;
}
四、看病要排队
- 另一篇文章有讲这个题,思路就是开三个优先队列模拟即可
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; struct node{
int id;
int cnt;
node(int id,int cnt):id(id),cnt(cnt){}
friend operator < (node a,node b){
if(a.cnt == b.cnt){
return a.id > b.id;
}
return a.cnt < b.cnt;
}
}; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n;
while(cin>>n){
priority_queue<node>j,k,l;
int tot = ;
for(int i = ; i <= n; i++){
string s;
int a,b;
cin>>s;
if(s[] == 'I'){
cin>>a>>b;
node tp(tot++,b);
if(a == ){
j.push(tp);
}
else if(a == ){
k.push(tp);
}
else if(a == ){
l.push(tp);
}
}
else if(s[] == 'O'){
cin>>a;
if(a == ){
if(j.empty()){
cout<<"EMPTY"<<endl;
}
else{
node tp = j.top();
j.pop();
cout<<tp.id<<endl;
}
}
else if(a == ){
if(k.empty()){
cout<<"EMPTY"<<endl;
}
else{
node tp = k.top();
k.pop();
cout<<tp.id<<endl;
}
}
else if(a == ){
if(l.empty()){
cout<<"EMPTY"<<endl;
}
else{
node tp = l.top();
l.pop();
cout<<tp.id<<endl;
}
} }
}
}
return ;
}
五、Windows Message Queue
- 就是叫你模拟信息队列
- 两种操作GET就是得到优先级最高的信息出队,PUT三个参数,序号,信息,优先级值。
- 题目说明优先级值越低的优先级越高,然后优先级值相同时,谁先进谁优先级高。
- 队列为空时输出EMPTY QUEUE!
- 开个结构体存信息,优先级值,序号,重载一下<号,模拟即可
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; struct node{
string msg;
int num,id;
node(string _msg,int _num,int _id):msg(_msg),num(_num),id(_id){}
friend operator < (node a,node b){
if(a.num == b.num){
return a.id > b.id;
}
return a.num > b.num;
}
}; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
priority_queue<node,vector<node>,less<node> >p;
int i = ;
while(++i){
string s;
if(!(cin>>s)){
break;
}
if(s[] == 'G'){
if(p.empty()){
cout << "EMPTY QUEUE!" << endl;
}
else{
node tp = p.top();
p.pop();
cout << tp.msg << endl;
}
}
else{
string msg,ct;
int num;
cin>>msg>>ct>>num;
//cout << msg << " " << ct << " " << num << endl;
msg = msg + ' ' + ct;
//cout << msg << endl;
p.push(node(msg,num,i)); }
} return ;
}
六、Black Box
- 也是欺负我英语不好,翻译老半天
- 第一行给你一个n和k
- 第二行n个数,第三行k个数
- 然后输出当第ki个数输入是,第i小的数是多少。
- 思路是开一个小顶堆和大顶堆,当查询第i个元素时,把前ki个元素先入小顶堆,然后把交换两个堆的元素,直到保证小顶堆所有元素都大于大顶堆里面的,这样小顶堆的首元素就是第i小的数。然后依次把小顶堆的数往大顶堆移即可。
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int a[]; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n,m,x;
priority_queue<int, vector<int>,greater<int> >p;
priority_queue<int, vector<int>,less<int> >q;
cin>>n>>m;
for(int i = ; i < n; i++){
cin>>a[i];
}
int c = ;
for(int i = ; i < m; i++){
cin>>x;
while(c < x){
p.push(a[c++]);
}
while(!q.empty() && p.top() < q.top()){
int t = p.top();
p.pop();
p.push(q.top());
q.pop();
q.push(t);
}
cout << p.top() << endl;
q.push(p.top());
p.pop();
}
return ;
}
七、Stones
- 另一篇博文也有些,就是开个结构体存位置和能抛多远就行,再重构一下排序操作。
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; struct node{
int pos,num;
bool operator < (const node b)const{
if(pos != b.pos)
return b.pos < pos;
return b.num < num;
}
}; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int t,n;
node tp;
cin>>t;
while(t--){
cin>>n;
priority_queue<node>q;
for(int i = ; i < n; i++){
cin>>tp.pos>>tp.num;
q.push(tp);
}
int flag = ;
while(!q.empty()){
tp = q.top();
q.pop();
if(flag){
flag = ;
tp.pos += tp.num;
q.push(tp);
}
else
flag = ;
}
cout << tp.pos << endl;;
}
return ;
}
八、求中位数
- 这道题开始我想偷懒vector处理直接tle =7=
- 和黑盒子那题一样,开两个优先队列维护即可。
- 不过这题有个坑点就是题目说明数据在int范围内,但你开int的堆就会wa,得开longlong(别问我为啥会知道=7=)
- 代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int n;
while(cin>>n){
priority_queue<ll>q;
priority_queue<ll,vector<ll>,greater<ll> >p;
for(int i = ; i < n; i++){
ll a,b;
cin>>a;
if(a == ){
cin>>b;
if(q.empty() && p.empty()){
q.push(b);
}
else{
if(b > q.top()){
p.push(b);
}
else{
q.push(b);
}
if(q.size() < p.size()){
q.push(p.top());
p.pop();
}
else if(q.size() > p.size() + ){
p.push(q.top());
q.pop();
}
}
}
else{
if((q.size() + p.size()) % == ){
ll res = q.top() + p.top();
if(res % == )
cout << res/ << endl;
else
cout << fixed << setprecision() << (db)res/2.0 << endl;
}
else{
ll res = q.top();
cout << res << endl;
}
}
}
} return ;
}
FJUT - OJ优先队列专题题解的更多相关文章
- 搜索专题题解(FJUT - OJ 17级搜索强化训练)
题目连接:http://120.78.128.11/Contest.jsp?cid=221#H 题目都比较难,每道题都很经典,我也做的很慢,这篇博文算是个收录.具体题目题解点击下面超链接吧. 一.Br ...
- 几道STL题目(FJUT - OJ STL训练1)
这个OJ一直在做,一些专题题目都很好,从易至难,阶梯上升,很适合像我这样的蒟蒻 =7= 这篇是关于其中一个专题训练的题解思路及代码 http://120.78.128.11/Contest.jsp ...
- Comet OJ - Contest #11 题解&赛后总结
Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...
- $vjudge-$基本算法专题题解
考完期末又双叒回来刷普及题辣$kk$ 然后放个链接趴还是$QwQ$ [X]$A$ 因为是嘤文($bushi$所以放个题意趴$QwQ$ 就汉诺塔问题,只是说有四个塔$A,B,C,D$,要求输出有1-12 ...
- $vjudge-dp$专题题解
因为感觉题解写不了多少,,,就懒得一道道题目慢慢写了,汇总了算了$QAQ$ 昂然后因为我估计以后还会有些什么$dp$专题啊$balabala$的,,,然后谢总肯定又会建一堆小组啥的,,,所以还是放个链 ...
- bzoj4458 GTY的OJ (优先队列+倍增)
把超级钢琴放到了树上. 这次不用主席树了..本来以为会好写一点没想到细节更多(其实是树上细节多) 为了方便,对每个点把他的那个L,R区间转化成两个深度a,b,表示从[a,b)选一个最小的前缀和(到根的 ...
- 2016 UESTC DP专题题解
题解下载地址:http://pan.baidu.com/s/1eSx27Jk 题解下载地址:http://pan.baidu.com/s/1qYDxlhi
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- Kuangbin 带你飞-基础计算几何专题 题解
专题基本全都是模版应用.贴一下模版 平面最近点对 const double INF = 1e16; ; struct Point { int x,y; int type; }; double dist ...
随机推荐
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- [GO语言的并发之道] Goroutine调度原理&Channel详解
并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环的富二代编程语言,它的并发(并行)编程肯定是值得开发者去探究的,而Go ...
- 【Node.js】 bodyparser实现原理解析
为什么我们需要body-parser 也许你第一次和bodyparser相遇是在使用Koa框架的时候.当我们尝试从一个浏览器发来的POST请求中取得请求报文实体的时候,这个时候,我们想,这个从Koa自 ...
- 【记忆化搜索】掷骰子 hpuoj
B. 掷骰子 单点时限: 2.0 sec 内存限制: 512 MB 骰子,中国传统民间娱乐用来投掷的博具,早在战国时期就已经被发明. 现在给你 n 个骰子,求 n 个骰子掷出点数之和为 a 的概率是多 ...
- 逆向破解之160个CrackMe —— 014
CrackMe —— 014 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- python代码规范整理
规范参考源: 1.pep8(python代码样式规范):中文文档 https://blog.csdn.net/ratsniper/article/details/78954852 2.pep ...
- 浏览器DOM渲染及阻塞问题
在准备面试,然后复习到了计网的知识点,紧接着又扯到了url从输入到浏览器渲染的那个问题,这里来顺便完善补充一下,本文的重点在渲染 上面的图就是浏览器从服务器请求来页面后渲染的全过程 这里我们分开来看: ...
- 一个接口多个实现类的Spring注入方式
1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...
- Oracle面对“数据倾斜列使用绑定变量”场景的解决方案
1.背景知识介绍 2.构造测试用例 3.场景测试 4.总结 1.背景知识介绍 我们知道,Oracle在传统的OLTP(在线事务处理)类系统中,强烈推荐使用绑定变量,这样可以有效的减少硬解析从而 ...
- python学习——高阶函数
递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.使用递归函数的优点是逻辑简单清晰,缺点就是过深的调用会导致栈溢出.但是针对尾递归优化的语言可以通过尾递归防 ...