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. 浅谈算法——Manacher

    字符串算法在各大高级比赛中均有用到,所以,学习好字符串算法对我们而言十分重要.那么,今天我们就给大家介绍一个快速求回文串的算法,Manacher算法,我们也习惯性叫它马拉车算法. 一.引入 首先我们要 ...

  2. QT5之2D绘图-绘制路径

    在绘制一个复杂的图形的时候,如果你需要重复绘制一个这样的图形,就可以使用到QPainterPath类,然后使用QPainter::drawPath()来进行绘制. QPainterPath类为绘制操作 ...

  3. Java标识符的习惯命名规范

    1 常量标识符:全部用大写字母和下划线表示.如SALES_MAX 2 类名或接口名:标识符用大写字母开头.如CreditCard 3 变量名和方法名:以小写字母开头,单词之间不要有分隔符,第二 及后面 ...

  4. Oracle11g导出dmp并导入Oracle10g的操作记录

    Oracle11g导出dmp并导入Oracle10g的操作记录. 操作环境说明: Oracle11g环境:Windows7,Oracle Database 11g Enterprise Edition ...

  5. Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏

    一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...

  6. Multitenant best Practice clone pdb seed and Clone a Pluggable Database – 12c Edition

    1. 1.Tnsnames when connecting to either Container or Pluggable instance The tnsnames.ora should be c ...

  7. .NET框架概述

    .NET战略目标: 任何时候(when),任何地方(where),使用任何工具(what)都能通过.NET的服务获得网络上的任何信息. .NET优势: 1.提供了一个面向对象的编程环境,完全支持面向对 ...

  8. RabbitMQ五:生产者--队列--多消费者

    一.生成者-队列-多消费者(前言) 上篇文章,我们做了一个简单的Demo,一个生产者对应一个消费者,本篇文章就介绍 生产者-队列-多个消费者,下面简单示意图 P 生产者    C 消费者  中间队列 ...

  9. Activiti工作流和shiro权限管理关系图

  10. 微信小程序flex布局

    一.flex布局基础 二.相对定位和绝对定位   flex的容器和元素   主轴(左-右),交叉轴(上-下)     flex容器属性详解 flex-direction 决定元素的排列方向(默认row ...