【模拟+线段树】 hdu4262 Juggler
Juggler
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4262
题意:有个数字圆环。三种操作:逆时针转一个,顺时针转一个,丢掉手中的那个的同时顺时针的数字到你的手里。每个操作都花费一秒。告诉你要求的出圈顺序,求最小花费时间。注:开始在手中的球为1.
题解:假设当前要移除的珠子为i,我们会发现无论左移还是右移都会得到同一个序列,即移去某个数字对后面的数字序列无影响,所以每次只需单独考虑左移和右移的长度,取最小累加即可。
代码:
#include<cstdio>
#include<cstring>
#include<list>
#include<algorithm>
using namespace std;
#define MAX 100005
#define ab(x) ((x)>0?(x):-(x))
int summ[MAX<<2];//区间内有几个数字
int pos[MAX];//询问顺序
void push_up(int idx)
{summ[idx]=summ[idx<<1]+summ[idx<<1|1];return;
}
void build(int l,int r,int idx)
{if(l==r){summ[idx]=1;return;}int mid=(l+r)>>1;build(l,mid,idx<<1);build(mid+1,r,idx<<1|1);push_up(idx);
}
void update(int p,int l,int r,int idx)
{if(l==r){summ[idx]=0;return;}int mid=(l+r)>>1;if(p<=mid) update(p,l,mid,idx<<1);else update(p,mid+1,r,idx<<1|1);push_up(idx);
}
long long query(int a,int b,int l,int r,int idx)
{if(a<=l&&r<=b){return summ[idx];}int mid=(l+r)>>1;long long ans=0;if(a<=mid) ans+=query(a,b,l,mid,idx<<1);if(mid<b) ans+=query(a,b,mid+1,r,idx<<1|1);return ans;
}
int main()
{int n,a,c,d;for(; scanf("%d",&n),n;){long long sumt=0,cnt;build(1,n,1);for(int i=1; i<=n; ++i){scanf("%d",&a);pos[a]=i;}a=1;for(int i=1; i<=n; ++i){sumt++;if(a!=pos[i]){if(a==1)c=0;elsec=query(1,a-1,1,n,1);if(pos[i]==1)d=0;elsed=query(1,pos[i]-1,1,n,1);cnt=ab(c-d);sumt+=min(cnt,n-i+1-cnt);}update(pos[i],1,n,1);a=pos[i];}printf("%I64d\n",sumt);}return 0;
}
来源: http://blog.csdn.net/acm_ted/article/details/7917889
【模拟+线段树】 hdu4262 Juggler相关推荐
- *【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)
题干: Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. O ...
- HDU 4262 Juggler (模拟+线段树优化)
转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526 by---cxlove http://acm.hdu.e ...
- HDU4262 Juggler 线段树
warm up的第六题,当时就看出来是线段树了,只是因为一些细节问题,没有A掉,做题目的时候心浮气躁.看来要走的路还很长. 这题目有三种动作让我们选择 1:顺时针移动一格 2:逆时针移动一格 3:把第 ...
- poj3468 线段树区间更新+区间查询
题目链接: http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limi ...
- Vijos P1103 校门外的树【线段树,模拟】
校门外的树 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,--, ...
- BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)
题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...
- BZOJ 5326 [JSOI2017]博弈 (模拟费用流、线段树)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5326 题解 终于成为第8个A掉这题的人--orz tzw神仙早我6小时 本以为这东西常数 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- CodeForces - 504B Misha and Permutations Summation(线段树模拟康托展开与逆展开)
题目链接:点击查看 题目大意:给出两个排列 ppp 和 qqq,现在要求输出 Perm((Ord(p)+Ord(q))modn!)Perm((Ord(p)+Ord(q)) \bmod n!) Perm ...
最新文章
- 苹果前置摄像头拍出来是反的怎么调_不用等了!屏下摄像头难攻克,明年还是挖孔屏...
- 元数据交换绑定的秘密
- 摇杆控制方向原理_医用无油空压机的送料作用及工作原理
- batch、epoch、iteration的区别
- SqlServer中char,varchar,nchar,nvarchar的区别
- python正则表达式 身份证_正则表达式实现身份证信息验证
- 计算机专业的学生可以考哪些证书
- Digispark(ATTINY85) 微型开发板驱动安装与开发环境配置教程
- C语言if( x)的意思,c语言 if(!x)中条件!x是什么意思
- Feature Enhancement Network: A Refined Scene Text Detector
- Oracle SQL开发考试试题
- 在PrimeFaces中更改主题
- TM4C123GXL_PWM
- matlab让legend横着排,echarts画一个饼图, orient: 'horizontal',图例横向排列,当图例比较多时,如何在第二排让图标一一对齐?...
- 数学建模(1)-matlab之fprintf函数用法
- Lesson 10
- win10下使用oflash时提示ftd2xx.dll缺失
- Prometheus 简介
- 车联网应用之“财产保全”
- 团体程序设计天梯赛-练习集 L2-026 小字辈 Python