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

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

树状数组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. Search in Rotated Sorted Array——LeetCode

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. django生命周期示意图

    1.图示

  3. =>符号的意义

    => 是 Oracle 中调用存储过程的时候, 指定参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用. 下面是具体的例子. 参数的默认值SQL> CREATE ...

  4. cordova-plugin-themeablebrowser 0.2.17 "ThemeableBrowser"ionic跳转外链插件在ios中heardBar会遮住内容的bug

    ionic+angular的app项目中需要在App打开一个外部的url链接,用了这个插件发现在iPhone手机中会出现toolbar挡住url页面内容 解决方法: 在原有基础上加上statusBar ...

  5. Java分享笔记:使用缓冲流复制文件

    [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...

  6. MySQL工作经验

    以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...

  7. Redis 4.0 从节点写入不同步问题

    redis4.0出现了很多新的特性,删除键值unlink,slowlog记录来源ip.内存统计信息等.其中一个重要的同步祭祀是Psync2.psync2主要让redis在从实例重启和主实例故障切换场景 ...

  8. Servlet学习笔记01——什么是servlet?

    1.什么是Servlet? sun公司制订的一种用来扩展web服务器功能的组件规范. (1)扩展web服务器功能 早期的web服务器(apache web server,iis) 只能处理静态资源的请 ...

  9. Python学习笔记:logging(日志处理)

    在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...

  10. 笔记-scrapy与twisted

    笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...