【优先级队列】 Holedox Eating
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/M
【Accepted】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
int l,n;
int main()
{
int T;
scanf("%d",&T);
int cas=;
while(T--)
{
priority_queue<int,vector<int>,greater<int>> qr;
priority_queue<int> ql;
scanf("%d%d",&l,&n);
int pos=;
int dir=;
int ans=;
for(int i=;i<n;i++)
{
int op;
scanf("%d",&op);
if(op==)
{
int x;
scanf("%d",&x);
if(x<pos) ql.push(x);
else qr.push(x);
}
else
{
if(!ql.empty()&&!qr.empty())
{
int t1=ql.top();
int t2=qr.top();
if(pos-t1==t2-pos)
{
if(dir==)
{
ans+=t2-pos;
qr.pop();
pos=t2;
dir=;
}
else
{
ans+=pos-t1;
ql.pop();
pos=t1;
dir=;
}
}
else if(pos-t1<t2-pos)
{
ans+=pos-t1;
ql.pop();
pos=t1;
dir=; }
else if(pos-t1>t2-pos)
{
ans+=t2-pos;
qr.pop();
pos=t2;
dir=;
}
}
else if(!ql.empty())
{
int t1=ql.top();
ans+=pos-t1;
ql.pop();
pos=t1;
dir=;
}
else if(!qr.empty())
{
int t2=qr.top();
ans+=t2-pos;
qr.pop();
pos=t2;
dir=;
}
}
}
printf("Case %d: %d\n",++cas,ans);
}
return ;
}
【优先级队列】 Holedox Eating的更多相关文章
- 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- 如何基于RabbitMQ实现优先级队列
概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ...
- ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...
- java中PriorityQueue优先级队列使用方法
优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...
- stl的优先级队列
#include <iostream> #include <vector> #include <queue> using namespace std; class ...
- 【python cookbook】【数据结构与算法】5.实现优先级队列
问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...
- hdu------(4302)Holedox Eating(树状数组+二分)
Holedox Eating Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 《Java数据结构与算法》笔记-CH4-6优先级队列
/** * 优先级队列 * 效率:插入O(n),删除O(1).第12章介绍如何通过堆来改进insert时间 */ class PriorityQueue { private int maxSize; ...
随机推荐
- 洛谷P2742 【模板】二维凸包
题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...
- 【HEVC帧间预测论文】P1.3 Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information
基于图形信息的HEVC帧间预测快速算法/Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information <H ...
- UVALive 2238 Fixed Partition Memory Management 固定分区内存管理(KM算法,变形)
题意:目前有一部分可用内存,分为m个大小固定的区域.现有n个程序要执行,每个程序在不同大小的内存中运行所需的时间不同,要使运行完所有程序所耗时最少,问每个程序在哪块区域中从什么时间运行到什么时间,以及 ...
- 如何修改站点url
1.config目录下的config_global.php 文件,修改:$_config['cookie']['cookiedomain'] = '.xxxxx.com';2.config目录下的co ...
- 常用的-->查找算法与排序算法
顺序查找 从列表第一个元素开始,顺序进行搜索,直到找到为止. 二分查找 从有序列表的候选区data[0:n]开始,通过对待查找的值与候选区中间值的比较,可以使候选区减少一半. li = [1, 2, ...
- Ubuntu 16.04 LTS下matplotlib安装出错
使用命令sudo pip3 install matplotlib已知报错,用同样的命令安装numpy和opencv却没有,因此重装linux系统两次都没有解决(我是在Vmware中创建的).报错如下: ...
- 吸顶条 ---- jQ写法
<script> $(function () { var barTop = $('#bar').offset().top; //on方法相当于原生的绑定 $(window).on('scr ...
- Deep_into_iris
具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...
- 暑假集训 || LCA && RMQ
LCA定义为对于一颗树 树上两个点的最近公共祖先 一.Tarjan求LCA(离线方法 https://blog.csdn.net/lw277232240/article/details/7701751 ...
- grep取缩写|awk两个文件取交集
#!/bin/sh les species_latinName_abbr.txt|grep -E 'Aptenodytes|Gavia|Phoenicopterus|Chlamydotis|Phaet ...