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. Java™ 教程(Queue接口)
  2. Python pip工具初步学习
  3. 用 Hadoop 进行分布式并行编程, 第 3 部分 部署到分布式环境
  4. PLSQL 执行 for update 语句被锁定的解决办法
  5. python做自动化控制postman_python自动化测试入门篇-postman
  6. wampServer2.1错误(Could not execute menu item (internal error)
  7. Linux学习总结(49)——应当竭力避免在系统中运行的 Linux 命令
  8. HTML5块元素标签的使用
  9. php++erp,php云ERP进销存V8标准版
  10. 兰州交通大学php,航拍兰州交通大学校园∣让我再看你一遍 从南到北
  11. 大数据存储1----什么是hdfs!!!
  12. 你可知道,让你发胖的食物不是高脂肪食物,而是高碳水化合物
  13. 【数据库】ER模型的简单例子
  14. 什么是Mysql的next-key、插入缓冲、二次写、自适应哈希索引和预读
  15. 如何打造又快又好的PPT(一)
  16. 服务降级--服务降级的一点认识
  17. cae计算机仿真分析技术,仿真分析工作在研发中的定位
  18. 辟谣、催债、倒闭.....2018年后,将再无创业黄金期!
  19. FineReport 参数级联查询与树集分组序号
  20. Python实验之——输出数字金字塔

热门文章

  1. c语言基础循环练习题,C语言练习题:循环部分
  2. Java-Set集合
  3. 简单CRM系统开发(二)
  4. 【大数据分析专业毕设之基于python爬虫的电影票房大数据预测分析+大屏可视化分析
  5. 高级搜索算法之迭代加深
  6. java实验报告:实验一 基于控制台的购书系统
  7. 奋斗在制造业----转行信息化
  8. Java Log4j和Log4j2的区别
  9. 云安全 | 云访问安全代理 CASB
  10. 时间序列数据、自协方差函数、自相关函数与平稳性