将每个球按输入顺序编号,建立 它第几个被扔掉->编号 的映射关系。

记录当前在手里的球的编号,按扔掉的顺序查找这个球的编号,看看这个球是逆时针转到手里更近还是顺时针转到手里更近,即当前扔掉球的编号与当前手里球的编号之间有几个球。

树状数组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 树状数组的更多相关文章

  1. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  2. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  4. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  5. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  6. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  7. hdu 1541 (基本树状数组) Stars

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...

  8. hdu 4031(树状数组+辅助数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    ...

  9. HDU 4325 Flowers 树状数组+离散化

    Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...

随机推荐

  1. react(一):组件的生命周期

    最近兄弟团队让我去帮忙优化两个页面,前端用的react全家桶,后端用的python,上一次写react代码都过去一年了,顺着以前的的学习思路,再捋顺一下react的要点 组件的生命周期就是Reac的工 ...

  2. Python测量时间,用time.time还是time.clock

    在计算机领域有多种时间.第一种称作CPU时间或执行时间,用于测量在执行一个程序时CPU所花费的时间.第二种称作挂钟时间,测量执行一个程序时的总时间.挂钟时间也被称作流逝时间或运行时间.与CPU时间相比 ...

  3. CentOS 7 下 jdk8 安装教程

    方法一: 一.下载   官网下载地址   下载需要确认当前系统是32位还是64位,可通过命令查询:   sudo uname --m   根据查询结果下载对应的jdk版本(如):   i686 //表 ...

  4. forEach、for...in、for...of

    forEach 数组实例的遍历方法 const arr=['red', 'green', 'blue']; arr.forEach(function(element, index) { console ...

  5. js加减乘除精确计算

    Javascript精确计算时的bug JS无法进行精确计算的bug 在做CRM,二代审核需求审核详情页面时.需要按比例(后端传类似0.8的小数)把用户输入的数字显示在不同的地方. 在做dubheIn ...

  6. avalon.js的循环操作在表格中的应用

    avalon.js的循环操作在表格中的应用 一个JAVA开发,因为做的门户系统中,数据的展示加载的速度很影响使用效果,想到的是尽量少的请求后台,然后接触到了avalon,看介绍这是一个很轻很轻的MVV ...

  7. http2.2配置

    http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...

  8. 深入理解PHP数组函数和预定义接口

    一. PHP对数组的过滤 函数: array_filter(p1[,p2]) 参数p1是要过滤的数组,参数p2是自定义过滤会掉函数(可以是匿名函数) 例子: <?php $arr = ['',n ...

  9. phpstorm 安装XeDbug

    第一步:根据phpinfo()下载相对应的Xdebug插件,Xdebug下载路径https://xdebug.org/download.php 第二步:将下载好的Xdebug放到 G:\Service ...

  10. C语言基础篇(三) 指针

    导航: 1.指针     2. 数组     3. 结构体,共用体     4. 内存分布图     5. 段错误分析 ----->x<------------->x<---- ...