Juggler

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
 

描述

As part of my magical juggling act, I am currently juggling a number of objects in a circular path with one hand. However, as my rather elaborate act ends, I wish to drop all of the objects in a specific order, in a minimal amount of time. On each move, I can either rotate all of the objects counterclockwise by one, clockwise by one, or drop the object currently in my hand. If I drop the object currently in my hand, the next object (clockwise) will fall into my hand. What’s the minimum number of moves it takes to drop all of the balls I’m juggling?

输入

There will be several test cases in the input. Each test case begins with an integern, (1≤n≤100,000) on its own line, indicating the total number of balls begin juggled. Each of the next n lines consists of a single integer,ki (1≤kin), which describes a single ball: i is the position of the ball starting clockwise from the juggler’s hand, andki is the order in which the ball should be dropped. The set of numbers {k1,k2, …,kn} is guaranteed to be a permutation of the numbers 1..n. The input will terminate with a line containing a single 0.

输出

For each test case, output a single integer on its own line, indicating the minimum number of moves I need to drop all of the balls in the desired order. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which will fit in a signed 64-bit integer.

样例输入

3
3
2
1
0

样例输出

5

提示

Explanation of the sample input: The first ball is in the juggler’s hand and should be dropped third; the second ball is immediately clockwise from the first ball and
should be dropped second; the third ball is immediately clockwise from the second ball and should be dropped last.
树状数组记录区间里去掉了几个

#include <stdio.h>
#include <string.h>
const int MAX = 100010;
int n;
int a[MAX];
int map[MAX];int min(int x,int y)
{return x < y ? x : y;
}
int lowbit(int t)
{ return t & ( t ^ ( t - 1 ) );
}
int sum(int end)
{ int sum = 0; while(end > 0) { sum += a[end]; end -= lowbit(end); } return sum;
}
void plus(int pos , int num)
{ while(pos <= n) {a[pos] += num; pos += lowbit(pos); }
} int main()
{int i,x,s;while(scanf("%d",&n),n){memset(a,0,sizeof(a));for(i = 1; i <= n; i++){scanf("%d",&x);map[x] = i;}int k = 1;__int64 cnt = n;for(i = 1;i < n; i++){if(i == 1)s = map[i] - 1;else if(k < map[i])s = map[i] - k - (sum(map[i]) - sum(k)) - 1;elses = k - map[i] - (sum(k - 1) - sum(map[i] - 1));plus(map[i],1);cnt += min(s,n - i - s + 1);k = map[i];  }printf("%I64d\n",cnt);}return 0;
}

TOJ 4354 HDU 4262 Juggler / 树状数组相关推荐

  1. HDU 4262 Juggler 树状数组

    将每个球按输入顺序编号,建立 它第几个被扔掉->编号 的映射关系. 记录当前在手里的球的编号,按扔掉的顺序查找这个球的编号,看看这个球是逆时针转到手里更近还是顺时针转到手里更近,即当前扔掉球的编 ...

  2. HDU 6681(树状数组统计平面内射线的交点个数)

    HDU 6681(树状数组,统计平面内射线的交点个数) 题目链接:传送门 题意:给出k条射线,求射线将n∗mn*mn∗m 的区域分成几个联通块.每两条射线的端点x坐标和y坐标都互不相同. 思路:根据 ...

  3. hdu 5497 Inversion(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5497 解题思路: 用树状数组维护一段区间L,区间长度为m,依次枚举该区间的终点ai,即将该点加入到区间 ...

  4. C - Group HDU - 4638[离线+树状数组]

    感觉比较抽象:举个例子:a[] = {3 1 2 5 4} 树状数组里面:1,1,-1,1,-1 sum(2):就是[1,2]区间能分成多少个连续的段:1和3 sum(3):1 2 3是一段 如果是询 ...

  5. HDU 4638 Group 树状数组 + 思路

    实际上就是问这个区间编号连续的段的个数,假如一个编号连续的段有(a+b)个人,我把他们分在同一组能得到的分值为(a+b)^2,而把他们分成人数为a和b的两组的话,得到的分值就是a^2+b^2,显然(a ...

  6. hdu 5542(树状数组优化dp)

    题意: 求n个数中长度为m的上升子序列的个数 解题思路:dp[i][j]表示第i个数长度为j的上升序列的个数.dp[i][j] = sum{dp[k][j-1] | a[k] < a[i]},这 ...

  7. HDU 4325 离散化+树状数组 或者 不使用树状数组

    题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...

  8. Billboard HDU - 2795(树状数组,单点修改,区间查询)

    题目链接:https://vjudge.net/problem/HDU-2795 思路:h = 1e9行不通,因为广告是1*w的,所以n个广告最多只需要 h = n的高度,那么h=2e5就可以接受了. ...

  9. HDU 3584 三维树状数组

    三维树状数组模版.优化不动了. #include <set> #include <map> #include <stack> #include <cmath& ...

最新文章

  1. Entity Framework的启动速度优化
  2. java 字符间距_java中,label或者文本框怎么设置字符之间的间距
  3. EDAS ScheduleX 问题
  4. [Android]实现类似微信的延迟加载的Fragment——LazyFragment
  5. Angular2入门:TypeScript的装饰器
  6. 牛客题霸 NC19 子数组的最大累加和问题
  7. 6 获取数组中最小值_C语言每日一练8——数组中最大值和最小值
  8. 数据结构与算法之归并排序
  9. 《Unit Testing》1.1 -1.2 单元测试的目的
  10. mysql首次_mysql首次登陆任务
  11. Unity动态对象优化
  12. java锁的概念,Java ReentrantLock锁机制概念篇
  13. Mybatis+Oracle批处理
  14. solrCloud相关的管理命令
  15. php网页顶部菜单代码,5款个性的网页回到顶部特效代码
  16. Winhex的使用教程
  17. web前端开发工程师的三种级别
  18. ZOJ 3216 Compositions(矩阵优化DP)
  19. 数据分析4——挖掘建模(监督学习中的分类、回归模型,无监督学习)
  20. 人大金仓适配mysql和oracle函数适配

热门文章

  1. ios10苹果手机页面定位不准问题
  2. Spring3—AOP
  3. Android:Environment.getExternalStorageDirectory 废弃
  4. java 数字和字母_java-如何在字母和数字之间(或数字和字母之间)分割字符串?...
  5. 性能分析系列-小命令保证大性能
  6. N4BiasFieldCorrection
  7. python判断电话号码是否合理_Python正则匹配判断手机号是否合法的方法
  8. ubuntu18.04终极美化
  9. java 虚拟机(jvm)-06-JVM jdk 自带工具 jstack jconsole jvisualvm jmap jinfo
  10. 爱普生Me330 打印机改装连供系统计划