Doing the Container Shuffle

시간 제한2초메모리 제한1024 MB

요약
컨테이너 1부터 n까지를 두 스택에 무작위로 넣었을 때, 주어진 순서대로 꺼내는 데 필요한 이동 횟수의 기댓값을 구한다.
난이도

어려움10점 중 8점

유형
확률, 수학, 배열, 누적 합
정답자
아직 제출이 없습니다

문제

Majestic cargo ships, each carrying thousands of shipping containers, roam the world’s seas every day. They make modern trade possible by being so efficient that shipping goods halfway around the world costs only pennies.

Once the ships reach their destination, their standard-size cargo containers are unloaded from the ship onto stacks on land, from which they are moved to trains or trucks that deliver them to their destination. It turns out that moving containers is expensive, so port operators try to minimize the number of moves necessary for delivering cargo.

In this problem, we consider such a container-unloading scenario. We need to unload nn containers, which are placed into two stacks built from bottom to top. The placement of each container is at random, with equal probability it will be put onto the first or the second stack (independently of other containers). Once all containers are unloaded, they will be picked up by trucks in a given order. When a truck wants to load a specific container, there are two cases. If the container is on top of its stack, then the container can be moved to the truck without moving any other containers. Otherwise, containers have to be moved from one stack to the other until the requested container is at the top of its stack. At that point the container can be moved onto the truck.

As an example, consider a case of three containers that arrive in order 1, 2, 3. Assume that 1 and 3 are in the first stack, and 2 is in the second. If the containers are moved onto trucks in order 1, 2, 3, then five moves of containers have to take place:

Stack 1Stack 2Comment
1 32Initial configuration (stacks bottom to top)
12 3Move container 3 from stack 1 to stack 2
2 3Move container 1 to truck
32Move container 3 from stack 2 to stack 1
3Move container 2 to truck
Move container 3 to truck

Table 1: Example moves of containers requested in order 1 2 3.

We want to know how many moves are necessary to deliver all containers to the customers. Assuming that container placement is random, we ask you to compute the expected number of moves necessary for a given truck-loading order.

입력

The first line of input contains an integer nn (1≤n≤1061≤n≤10^6), the number of containers. The containers are numbered 1,2,…,n1,2,\dots ,n, and are unloaded from the ship in this order. The second line of input contains nn integers a_1,…,a_na\_1,\dots ,a\_n. These numbers are a permutation of 1,2,…,n\\{1,2,\dots ,n\\}, and specify the order in which the containers are loaded onto trucks.

출력

Output the expected number of moves necessary to load the containers onto the trucks — this excludes the cost of unloading them from the ship, but includes both moves between stacks and from a stack to a truck. Your answer should have an absolute error of at most 10−310^{-3}.

예제2

  1. 예제 1

    입력
    5
    4 2 5 3 1
    
    예상 출력
    7.000
    
  2. 예제 2

    입력
    6
    1 2 3 4 5 6
    
    예상 출력
    13.500