一、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优先队列专题题解的更多相关文章

  1. 搜索专题题解(FJUT - OJ 17级搜索强化训练)

    题目连接:http://120.78.128.11/Contest.jsp?cid=221#H 题目都比较难,每道题都很经典,我也做的很慢,这篇博文算是个收录.具体题目题解点击下面超链接吧. 一.Br ...

  2. 几道STL题目(FJUT - OJ STL训练1)

    这个OJ一直在做,一些专题题目都很好,从易至难,阶梯上升,很适合像我这样的蒟蒻 =7= 这篇是关于其中一个专题训练的题解思路及代码   http://120.78.128.11/Contest.jsp ...

  3. Comet OJ - Contest #11 题解&赛后总结

    Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...

  4. $vjudge-$基本算法专题题解

    考完期末又双叒回来刷普及题辣$kk$ 然后放个链接趴还是$QwQ$ [X]$A$ 因为是嘤文($bushi$所以放个题意趴$QwQ$ 就汉诺塔问题,只是说有四个塔$A,B,C,D$,要求输出有1-12 ...

  5. $vjudge-dp$专题题解

    因为感觉题解写不了多少,,,就懒得一道道题目慢慢写了,汇总了算了$QAQ$ 昂然后因为我估计以后还会有些什么$dp$专题啊$balabala$的,,,然后谢总肯定又会建一堆小组啥的,,,所以还是放个链 ...

  6. bzoj4458 GTY的OJ (优先队列+倍增)

    把超级钢琴放到了树上. 这次不用主席树了..本来以为会好写一点没想到细节更多(其实是树上细节多) 为了方便,对每个点把他的那个L,R区间转化成两个深度a,b,表示从[a,b)选一个最小的前缀和(到根的 ...

  7. 2016 UESTC DP专题题解

    题解下载地址:http://pan.baidu.com/s/1eSx27Jk 题解下载地址:http://pan.baidu.com/s/1qYDxlhi

  8. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  9. Kuangbin 带你飞-基础计算几何专题 题解

    专题基本全都是模版应用.贴一下模版 平面最近点对 const double INF = 1e16; ; struct Point { int x,y; int type; }; double dist ...

随机推荐

  1. 为何Spring MVC可获取到方法参数名,而MyBatis却不行?【享学Spring MVC】

    每篇一句 胡适:多谈些问题,少聊些主义 前言 Spring MVC和MyBatis作为当下最为流行的两个框架,大家平时开发中都在用.如果你往深了一步去思考,你应该会有这样的疑问: 在使用Spring ...

  2. TinyMCE使用

    1.文本&文本字体颜色 与word类似不赘述 2.字体加粗&划线 与word类似不赘述 选中后  ctrl + B  加粗快捷键 选中后  ctrl + I  斜体快捷键 选中后  c ...

  3. spring-boot-plus快速快发脚手架简介

    Everyone can develop projects independently, quickly and efficiently! Introduction spring-boot-plus是 ...

  4. 【JVM从小白学成大佬】3.深入解析强引用、软引用、弱引用、幻象引用

    关于强引用.软引用.弱引用.幻象引用的区别,在很多公司的面试题中经常出现,可能有些小伙伴觉得这个知识点比较冷门,但其实大家在开发中经常用到,如new一个对象的时候就是强引用的应用. 在java语言中, ...

  5. python之爬虫-必应壁纸

    python之爬虫-必应壁纸 import re import requests """ @author RansySun @create 2019-07-19-20:2 ...

  6. Spring自定义属性编辑器及原理解释.md

    bean的自动装配解释 手动解决方式 自动注入解决方式 bean的自动装配解释 之前有构造注入和设值注入,但是也是手动的 autowire ="byname" 这里要注意自动装配的 ...

  7. 梳理commons-lang工具包

    目录 概述 builder包 NumberUtils 转换 String 类型为原始类型 截取小数位数 创建包装类型 最大值 | 最小值 关于数字的检查 mutable包 relect包 Constr ...

  8. JVM(十三):后端编译优化

    JVM(十三):后端编译优化 在 JVM(一):源文件的转变 中我们介绍了 Java 中的前端优化,即将 Java 源代码转换为字节码文件.在本文中,我们将介绍字节码文件如何转换为本地机器码,并如何对 ...

  9. 自制微信小程序 提示插件 -- noticeUitis.js

    /* noticeMsg.js by: FEer_llx Modify 2016/08/24 */ function weNotice(obj) { this.fadeFlag = true; thi ...

  10. springBoot日志框架自动配置与原理

    1.日志框架 小张:开发一个大型系统: ​ 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息: ...