BNUOJ 26228 Juggler

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))么?
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxn = 100010;
17 LL d[maxn];
18 int pos[maxn],n;
19 int lowbit(int x){
20     return x&-x;
21 }
22 void add(int x,int val){
23     for(; x < maxn; x += lowbit(x)){
24         d[x] += val;
25     }
26 }
27 LL sum(int x){
28     LL temp = 0;
29     for(; x > 0; x -= lowbit(x))
30         temp += d[x];
31     return temp;
32 }
33 int main(){
34     int i,j,temp,cnt;
35     LL ans;
36     while(scanf("%d",&n),n){
37         memset(d,0,sizeof(d));
38         memset(pos,0,sizeof(pos));
39         for(i = 1; i <= n; i++){
40             scanf("%d",&temp);
41             pos[temp] = i;
42             add(i,1);
43         }
44         cnt = 1;
45         ans = 0;
46         for(i = 1; i <= n; i++){
47             ans++;
48             if(cnt != pos[i]){
49                 LL df = abs(sum(cnt-1)-sum(pos[i]-1));
50                 ans += min(df,n-i-df+1);
51             }
52             cnt = pos[i];
53             add(pos[i],-1);
54         }
55         printf("%I64d\n",ans);
56     }
57     return 0;
58 }
View Code

 

posted @ 2014-08-14 10:32  狂徒归来  阅读(206)  评论(0编辑  收藏  举报