HDU 4262 Juggler 树状数组
将每个球按输入顺序编号,建立 它第几个被扔掉->编号 的映射关系。
记录当前在手里的球的编号,按扔掉的顺序查找这个球的编号,看看这个球是逆时针转到手里更近还是顺时针转到手里更近,即当前扔掉球的编号与当前手里球的编号之间有几个球。
树状数组C[i]记录编号i的球是否还在。
球是环形排列的,特殊处理一下。
对于扔掉一个球之后下一个落在手里的球的编号,二分判定,找顺时针方向第一个有球的位置
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int using namespace std; const int MAXN = ; int C[MAXN];
int N;
int PosToID[MAXN];
int IDToPos[MAXN]; int lowbit( int x )
{
return x & ( -x );
} int query( int x )
{
if ( !x ) return ;
int res = ;
while ( x > )
{
res += C[x];
x -= lowbit(x);
}
return res;
} void update( int x, int val )
{
while ( x <= N )
{
C[x] += val;
x += lowbit(x);
}
return;
} int BiSearch( int l, int r )
{
int pre = l;
if ( query(r) - query(l) == )
{
r = l;
l = ;
pre = ;
} int ans = ;
while ( l <= r )
{
int mid = ( l + r ) >> ;
if ( query(mid) - query(pre) > )
{
ans = mid;
r = mid - ;
}
else l = mid + ;
}
return ans;
} int main()
{
while ( scanf( "%d", &N ), N )
{
memset( C, , sizeof(C) );
for ( int i = ; i <= N; ++i )
{
scanf("%d", &IDToPos[i] );
PosToID[ IDToPos[i] ] = i;
update( i, );
} int cur = ; //当前手里的
LL ans = ;
for ( int i = ; i <= N; ++i )
{
int id = PosToID[i]; if ( id < cur )
{
int tmp = query(cur) - query(id);
ans += min( tmp, query(N) - query(cur) + query(id) );
}
else if ( id > cur )
{
int tmp = query(id) - query(cur);
ans += min( tmp, query(N) - query(id) + query(cur) );
}
update( id, - );
++ans;
cur = BiSearch( id, N );
}
printf( "%I64d\n", ans );
}
return ;
}
HDU 4262 Juggler 树状数组的更多相关文章
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- hdu 1541 (基本树状数组) Stars
题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...
- hdu 4031(树状数组+辅助数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others) ...
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
随机推荐
- 如何在Linux中显示和设置主机名
原文链接 随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的 ...
- ibator自动代码生成
首先,强烈推荐一篇文章,介绍的特详细 http://www.iteye.com/topic/821983 1. 插件安装 http://blog.csdn.net/rchm8519/article/d ...
- System.Web.Caching.Cache
此类是利用缓存来保存信息的.可以把一些稳定的数据,不会随用户而改变的信息利用Cache保存起来,可以优化网站的速度. Cache辅助类已上传:GitHub Cache和Session,cookie的区 ...
- C# 操作符与表达式
C#保留了C++所有的操作符,其中指针操作符(*和->)与引用操作符(&)需要有unsafe的上下文.C#摈弃了范围辨析操作符(::),一律改为单点操作符(.).我们不再阐述那些保留的C ...
- 牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】
链接:https://www.nowcoder.com/acm/contest/85/I题目描述 あなたの蛙が帰っています! 蛙蛙完成了一趟旅行,回家啦!但它还是没有去它心中非常想去的几个地方.总共 ...
- 1801: [Ahoi2009]chess 中国象棋
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2520 Solved: 1524[Submit][Status][Discuss] Descripti ...
- 【学时总结】◆学时·VII◆ 高维DP
◆学时·VII◆ 高维DP 自学之余,偶遇DP…… ◇ 算法概述 顾名思义——一种处理多方面状态的DP,这种DP特点是……每一维的大小都不算太大(不然用dp数组存储下来内存会炸),而且枚举时容易超时… ...
- Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function
因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...
- TA-LIB】之MACD
移动平滑异同平均线(Moving Average Convergence Divergence,简称MACD指标)策略.MACD是查拉尔·阿佩尔(Geral Appel)于1979年提出的,由一快及一 ...
- 千锋教育Vue组件--vue基础的方法
课程地址: https://ke.qq.com/course/251029#term_id=100295989 <!DOCTYPE html> <html> <head& ...