Juggler

Time Limit: 3000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4262
64-bit integer IO format: %I64d      Java class name: Main

 
 
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?

 

Input

There will be several test cases in the input. Each test case begins with an integer n, (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≤ki≤n), which describes a single ball: i is the position of the ball starting clockwise from the juggler’s hand, and ki 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.

 

Output

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.

 

Sample Input

3
3
2
1
0

Sample Output

5
Hint

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.

Source

 
 
解题:树状数组的使用
 
 解释下样例
 
3 3 2 1 三个球,先扔第3个球,再扔第2个球,最后扔第一个球!每扔然一个球,在树状数组中将当前位置删除,即表示当前位置没有球。由于当前位置没有球!并不影响树状数组的统计。
 
每次左旋或者右旋,择其步骤小者。 ans += abs(sum(cnt-1) - sum(pos[i]-1));为什么都要减一啊?假设cnt = 1,pos[i] = 5; 从1->5 要多少步?
关键得看 [1 ,4] 之间有多少个1,对的闭区间。如何求[1,4] 之间有多少个1?sum(4) - sum(0)。。不正是abs(sum(cnt-1) - sum(pos[i]-1))么?
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
LL d[maxn];
int pos[maxn],n;
int lowbit(int x){
return x&-x;
}
void add(int x,int val){
for(; x < maxn; x += lowbit(x)){
d[x] += val;
}
}
LL sum(int x){
LL temp = ;
for(; x > ; x -= lowbit(x))
temp += d[x];
return temp;
}
int main(){
int i,j,temp,cnt;
LL ans;
while(scanf("%d",&n),n){
memset(d,,sizeof(d));
memset(pos,,sizeof(pos));
for(i = ; i <= n; i++){
scanf("%d",&temp);
pos[temp] = i;
add(i,);
}
cnt = ;
ans = ;
for(i = ; i <= n; i++){
ans++;
if(cnt != pos[i]){
LL df = abs(sum(cnt-)-sum(pos[i]-));
ans += min(df,n-i-df+);
}
cnt = pos[i];
add(pos[i],-);
}
printf("%I64d\n",ans);
}
return ;
}

最新文章

  1. jQuery uploadify 文件上传
  2. DOM document object model learn
  3. 深入理解javascript原型和闭包(11)——执行上下文栈
  4. Mysql之高可用
  5. jQuery通过判断 checkbox 元素的 checked 属性,判断 checkbox是否被选中
  6. paip.判断字符是否中文与以及判读是否是汉字uapi python java php
  7. Jquery each和map 的区别
  8. Oracle修改表空间大小
  9. 练习--分治法--Merge k Sorted Lists
  10. CSS小记(持续更新......)
  11. ASP.NET Core 一步步搭建个人网站(2)_一键部署和用户注册登录
  12. 安装Leanote极客范的云笔记
  13. opencv3.2.0图像处理之高斯滤波GaussianBlur API函数
  14. GIT-windows系统部署git服务器
  15. docker vm 性能优劣
  16. [原创]Cadence Allegro小技巧之解决Out of date shapes问题
  17. cin与cout格式化输出
  18. 03 uni-app框架学习:轮播图组件的使用
  19. 16款纯CSS3实现的loading加载动画
  20. Object-C 类和对象

热门文章

  1. 题解报告:hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
  2. SQL SELECT DISTINCT 语句 用法
  3. 给ambari集群里的kafka安装基于web的kafka管理工具Kafka-manager(图文详解)
  4. Win10 UWP开发:摄像头扫描二维码/一维码功能
  5. 关于 user agent ua
  6. 460在全志r16平台tinav3.0系统下使用i2c-tools
  7. [6818开发板]八核开发板|4G开发板|GPS开发板|嵌入式开发平台
  8. 华硕笔记本无法设置U盘启动,快捷启动不能识别
  9. vue脚手架引入swiper
  10. vim要粘贴的话,先set paste,然后粘贴,然后再set nopaste