HDU 4302 Holedox Eating (线段树模拟)
题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动。开始时没有蛋糕,老鼠的初始位置是0.
有两个操作,0 x 代表在位置x添加一个蛋糕; 1 代表老鼠想吃蛋糕。老鼠每次都会选择离自己最近的点,如果两边距离相同,老鼠优先选择与自己当前移动方向相同的点。
求最终移动的总距离。
题解:线段树单点修改+查询区间端点。
设当前位置为pos,每次查询区间[0, pos]的最右端和区间[pos, L]的最左端,比较选择哪个更近。
详细题解见代码注释。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath> #define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1 #define LEFT 0
#define RIGHT 1 using std::min;
using std::max;
using std::abs; const int MAXN = ;
const int INF = << ; int tr[MAXN << ]; //标记哪个点有蛋糕
int left[MAXN << ]; //标记区间[l, r]内有蛋糕的最左端点
int right[MAXN << ]; //标记区间[l, r]内有蛋糕的最右端点
int N, S;
int findL, findR; //标记查找到的任意区间[l, r]中有蛋糕的最左端和最右端 void PushUp( int rt )
{
int lc = rt << ;
int rc = rt << | ;
left[rt] = min( left[lc], left[rc] );
right[rt] = max( right[lc], right[rc] );
return;
} void build( int l, int r, int rt ) //建树
{
tr[rt] = ; //每个点初始蛋糕数目为0
left[rt] = INF;
right[rt] = -INF;
if ( l == r ) return;
int m = ( l + r ) >> ;
build( lson );
build( rson );
return;
} void Update( int pos, int v, int l, int r, int rt ) //更新
{
if ( l == pos && r == pos )
{
tr[rt] += v; //更新该点的蛋糕数目
if ( tr[rt] == ) //如果这一点没有蛋糕了
{
left[rt] = INF;
right[rt] = -INF;
}
else //如果这一点有蛋糕
{
left[rt] = l;
right[rt] = l;
}
return;
} int m = ( l + r ) >> ;
if ( pos <= m ) Update( pos, v, lson );
else Update( pos, v, rson );
PushUp( rt );
return;
} void Query( int L, int R, int l, int r, int rt ) //查询
{
if ( L <= l && r <= R )
{
findL = min( left[rt], findL );
findR = max( right[rt], findR );
return;
}
int m = ( l + r ) >> ;
if ( L <= m ) Query( L, R, lson );
if ( R > m ) Query( L, R, rson );
return;
} int main()
{
int T, cas = ;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d%d", &N, &S );
++N; //原题目中pipe长度为L,编号为0-L,实际上是有(L+1)个点,
//之前没注意,RE了一次,在这里我把每个点编号为1-(L+1) int curPos = ; //当前位置
int curDirection = RIGHT; //当前方向
int sum = ; //移动总路程 build( , N, ); //建树 while ( S-- )
{
//printf("**************curPos=%d curDir=%d\n", curPos, curDirection );
int op, pos;
scanf( "%d", &op );
if ( op == )
{
scanf( "%d", &pos );
Update( pos+, , , N, );
}
else
{
bool find = false;
int findpos; if ( curDirection == LEFT ) //如果当前方向向左
{
int LL, RR;
findL = INF;
findR = -INF;
Query( , curPos, , N, ), LL = findR; //向左走,找离当前点最近的右端点
findL = INF;
findR = -INF;
Query( curPos, N, , N, ), RR = findL; //向右走,找离当前点最近的左端点 if ( LL == -INF && RR == INF ) //两边都没有蛋糕
{
find = false;
}
else if ( LL == -INF && RR != INF ) //只有右边有蛋糕
{
find = true;
findpos = RR;
curDirection = RIGHT;
}
else if ( LL != -INF && RR == INF ) //只有左边有蛋糕
{
find = true;
findpos = LL;
curDirection = LEFT;
}
else //两边都有蛋糕
{
find = true;
if ( curPos - LL <= RR - curPos ) //注意等号,两边距离相等时优先选择左边
{
findpos = LL;
curDirection = LEFT;
}
else
{
findpos = RR;
curDirection = RIGHT;
}
} //printf("LEFT: ");
}
else
{
int LL, RR;
findL = INF;
findR = -INF;
Query( curPos, N, , N, ), RR = findL;
findL = INF;
findR = -INF;
Query( , curPos, , N, ), LL = findR; if ( LL == -INF && RR == INF )
{
find = false;
}
else if ( LL == -INF && RR != INF )
{
find = true;
findpos = RR;
curDirection = RIGHT;
}
else if ( LL != -INF && RR == INF )
{
find = true;
findpos = LL;
curDirection = LEFT;
}
else
{
find = true;
if ( curPos - LL < RR - curPos ) //注意没有等号,两边距离相等时优先选择右边
{
findpos = LL;
curDirection = LEFT;
}
else
{
findpos = RR;
curDirection = RIGHT;
}
} //printf("RIGHT: ");
}
//printf( "findL=%d findR=%d\n", findL, findR ); //printf( "findpos = %d\n", findpos );
if ( find )
{
sum += abs( findpos - curPos );
curPos = findpos;
Update( findpos, -, , N, );
}
}
} printf( "Case %d: %d\n", ++cas, sum );
}
return ;
}
HDU 4302 Holedox Eating (线段树模拟)的更多相关文章
- 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
http://acm.hdu.edu.cn/showproblem.php?pid=4302 #include <cstdio> #include <cstring> #inc ...
- HDU 4302 Holedox Eating(multiset)
http://acm.hdu.edu.cn/showproblem.php?pid=4302 题意: 在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的 ...
- hdu_5818_Joint Stacks(线段树模拟)
题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度 ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- 【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流
昨天考试被教育了一波.为了学习一下\(T3\)的科技,我就找到了这个远古时期的\(cf\)题(虽然最后\(T3\)还是不会写吧\(QAQ\)) 顾名思义,这个题目其实可以建成一个费用流的模型.我们用流 ...
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
随机推荐
- 初学pygame
#Author:cljimport pygamepygame.display.set_mode((640,480),0,32)#设置窗口大小 返回的也是一个surface对象,resolution可以 ...
- 复习宝典之Git分布式版本控制
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第三章:Git分布式版本控制 1)git文件状态 git中的文件有以下几种状态: 未跟踪(untrack):表示文件为新增加的. 已修改 ...
- 解决MacOS升级后出现xcrun: error: invalid active developer path, missing xcrun的问题
升级了系统 命令行不能用了 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), mis ...
- Django在windows系统下安装详情
1. Django 下载地址:https://www.djangoproject.com/download/ 2. 解压到跟python安装目录平级目录: 如下图: 3.通过cmd控制台安装djang ...
- 使用apt-get install时如何指定安装版本
命令语法如下: sudo apt-get install package=version 例如: sudo apt-get install samba=2:4.4.5+dfsg-2ubuntu6
- bootstrap-01-学习记录
1.bootstrap所有插件依赖JQ,必须在JQ之后引入. 2.bootstrap分预编译版(css,js,fonts)和源码版(less,js,fonts,dist->预编译版内容,docs ...
- shell编程基础-01
1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具Linux/UNIX系统的底层及基础应用软件的核心大都涉及Shell脚本的 ...
- wdcp v3 pureftpd 无法登录问题解决
wdcp v3 新建站点和ftp账号 单位无法登录ftp 在端口中也确实可以看到有进行在登录状态 错误原因: 防火墙端口没有开启该端口范围 20000-30000 这时候发现 改端口为20078 ...
- angular2配置使用ng2-bootstrap
第一步,安装.进入项目目录 npm install ng2-bootstrap bootstrap --save 第二步,angular-cli 配置 ng2-bootstrap src/.ang ...
- python3网络爬虫系统学习:第二讲 基本库requests(一)
之前,我们学习了基本库urllib的相关用法,但是在网页验证.Cookies处理等方面是比较繁琐的,需要用到Handler并且还需自己构建Opener.requests库的出现很好的解决了这个问题,下 ...