HDU 4302 Holedox Eating(multiset)
http://acm.hdu.edu.cn/showproblem.php?pid=4302
题意:
在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的蛋糕去吃。一开始在0坐标处,如果两边都有距离相同的蛋糕,则不改变方向。求经过的总距离。
思路:
multiset维护,每次1命令时在multiset找距离最近的即可。
#include<iostream>
#include<cstdio>
#include<set>
using namespace std; int L,n;
multiset<int> s;
multiset<int>::iterator it; int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
scanf("%d",&T);
while(T--)
{
s.clear();
scanf("%d%d",&L,&n);
int ans = ;
int pos = , dir = ;
for(int i=;i<n;i++)
{
int op;
scanf("%d",&op);
if(op==)
{
int x; scanf("%d",&x);
s.insert(x);
}
else
{
if(s.size()==) continue;
it = s.lower_bound(pos);
int tmp1 = -, tmp2 = -;
if(it!=s.end())
{
tmp1 = *it - pos;
}
if(it!=s.begin())
{
tmp2 = pos - *(--it);
}
if(tmp1 == )
{
if(tmp2==-) s.erase(it);
else s.erase(++it);
continue;
}
if(tmp1 == -)
{
ans += tmp2;
dir = ;
pos = *it;
s.erase(it);
}
else if(tmp2 == -)
{
ans += tmp1;
dir = ;
pos = *it;
s.erase(it);
}
else if(tmp1 == tmp2)
{
if(dir == )
{
ans += tmp1;
pos = *(++it);
s.erase(it);
}
else
{
ans += tmp2;
pos = *it;
s.erase(it);
}
}
else
{
if(tmp1 < tmp2)
{
ans += tmp1;
dir = ;
pos = *(++it);
s.erase(it);
}
else
{
ans +=tmp2;
dir = ;
pos = *it;
s.erase(it);
}
}
}
}
printf("Case %d: ",++kase);
printf("%d\n",ans);
}
return ;
}
HDU 4302 Holedox Eating(multiset)的更多相关文章
- HDU 4302 Holedox Eating (STL + 模拟)
Holedox Eating Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4302 Holedox Eating(优先队列/线段树)
题意:一只蚂蚁位与原点,在x轴正半轴上会不时地出现一些蛋糕,蚂蚁每次想吃蛋糕时选取最近的去吃,如果前后距离相同,则吃眼前的那一块(即方向为蚂蚁的正前),求最后蚂蚁行进距离. 思路:优先队列q存储蚂蚁前 ...
- HDU 4302 Holedox Eating (线段树模拟)
题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动.开始时没有蛋糕,老鼠的初始位置是0. 有两个操作,0 x 代表在位置x添加一个蛋糕: 1 代表老鼠想吃蛋糕.老鼠每次都会选择离自己最 ...
- hdu 4302 Holedox Eating
http://acm.hdu.edu.cn/showproblem.php?pid=4302 #include <cstdio> #include <cstring> #inc ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
随机推荐
- 转:[你必须知道的异步编程]——异步编程模型(APM)
本专题概要: 引言 你知道APM吗? 你想知道如何使用异步编程模型编写代码吗? 使用委托也可以实现异步编程,你知道否? 小结 一.引言 在前面的C#基础知识系列中介绍了从C#1.0——C#4.0中一些 ...
- 大数据学习路线分享-Hbase shell的基本操作完整流程
HBase的命令行工具,最简单的接口,适合HBase管理使用,可以使用shell命令来查询HBase中数据的详细情况.安装完HBase之后,启动hadoop集群(利用hdfs存储),启动zookeep ...
- The Little Prince-12/02
The Little Prince-12/02 What moves me so deeply, about this little prince who is sleeping here, is h ...
- to refactor for refactor
v1.1 if all algorithm are in one function, it will expand. so each operate type should be separated. ...
- 2019/3/26 wen 数组
- IntelliJ IDEA. Debug模式
资料收集: https://www.bilibili.com/video/av6749471/?p=16 eclipse debug模式. 基础 Intellij Idea--Debug使用 Inte ...
- python的ws库功能,实时获取服务器ws协议返回的数据
# -*- coding:utf-8 -*- ''' 模块下载,帮助地址:https://github.com/liris/websocket-client#readme 模块:websocket-c ...
- 在static的function静态函数中访问成员变量
class A{ private $url; public function __construct(){ $this->url = $_SERVER['PATCH_INFO']; } publ ...
- Golang接口简单了解
在Golang中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口. package main import "fmt" type Animal interface ...
- Golang获取int数组里的最大值和下标
package main import ( "fmt" ) func main() { //获取一个数组里最大值,并且拿到下标 //声明一个数组5个元素 ], , , ,} //假 ...