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 ;
}

BNUOJ 26228 Juggler的更多相关文章

  1. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  2. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  3. bnuoj 44359 快来买肉松饼

    http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms     Case Time Lim ...

  4. BNUOJ 1006 Primary Arithmetic

    Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...

  5. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  6. bnuoj 25659 A Famous City (单调栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...

  7. bnuoj 25662 A Famous Grid (构图+BFS)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...

  8. bnuoj 4207 台风(模拟题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4207 [题意]:中文题,略 [题解]:模拟 [code]: #include <iostrea ...

  9. bnuoj 4208 Bubble sort

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4208 [题意]:如题,求冒泡排序遍历趟数 [题解]:这题开始2B了,先模拟TLE,然后想了一下,能不 ...

随机推荐

  1. [Usaco2005 Jan]Muddy Fields泥泞的牧场

    Description 雨连续不断的击打了放牛的牧场,一个R行C列的格子(1<=R<=50,1<=C<=50).虽然这对草来说是件好事,但这却使得一些没有草遮盖的土地变得很泥泞 ...

  2. vijos1846 [NOIP2013] 华容道【最短路】

    传送门:https://vijos.org/p/1983 (其实noip的题各个oj都会有的,就不贴其它传送门了) 这道题真的是,怎么说,我都不知道怎么评价了= =.果然数据量小的题怎么暴力都可以过. ...

  3. Optimizing Downloads for Efficient Network Access

    Optimizing Downloads for Efficient Network Access Previous  Next 1.This lesson teaches you to Unders ...

  4. shell脚本中定义路径变量出现的BUG

    =========================================================================== if 语句中的定义路径变量 引发命令的PATH路 ...

  5. 移动端UI自动化Appium测试——获取APK的Package及Activity属性值

    1.如果有代码环境,直接在AndroidManifest.xml中查找: package值: Activity值: 2.如果没有开发代码,直接用命令获取: cmd进入到 android-sdk-win ...

  6. $.extend(x,y); 函数用法介绍。

    第一篇资料:  转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...

  7. HTML5 File API的应用

    HTML5 File API简介 HTML5File API协议族 Directories and System   文件系统和目录读取 FileWriter   写入文件 FileReader   ...

  8. 仿陌陌的ios客户端+服务端源码

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...

  9. PostgreSQL 数据库错误状态编号解释[附带列表

    PostgreSQL 服务器发出的所有消息都赋予了五个字符的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错误 ...

  10. vultr系统重建

    vultr服务器重启后ssh连接不上,view console只能guest登录,遍寻方法,无解,无奈重装系统. 服务器地址:Los Angeles 系统: Ubuntu 14.04 x64 用户:r ...