2125. Boss

单点时限: 2.0 sec

内存限制: 256 MB

An organization has a well-structured human resource. The Chief Executive Officer (CEO) is the top boss of the entire organization. He is the most important personnel and has no boss to report. All other personnel in the organization have a boss to report directly. A boss in turn has direct control over a number of subordinate personnel who are ordered according to their importance. A subordinate under the direct control of a boss may or may not be a boss.

All personnel including the CEO are ordered totally according to their importance in the organization. A personnel X is considered more important than another personnel Y if any of the following is true:

  • X is the CEO,

  • X is not the CEO but is the boss of Y,

  • X and Y have the same boss and X is more important than Y,

  • X and Y have different bosses and boss of X is more important than boss of Y.

Let the organization has n personnel. A personnel is identified by a distinct integer 1,2, … or n, called the ID of the personnel, according to the order of importance. The most important personnel viz., the CEO has ID equal to 1, while the least important personnel has ID equal to n.

A boss may communicate directly to a subordinate in 1(one) unit of communication time(UCT) while a subordinate requires 2 (two) UCT to communicate with the boss.

The organization maintains a list L of all personnel identified by their IDs. A boss appears in L only after all direct subordinates appear in order of their importance. Given L and two personnel X and Y, you are required to write a program that finds the time T in UCT for X to communicate a message to Y.


The input consists of multiple test cases. Each test case contains data in two lines. The first line gives three integers n, X and Y defined above. The next line gives a sequence of n integers representing the list L. Fields in input are separated by a single space.

The input terminates when a line containing 0 appears.


For each test case output the required time T as an integer.


7 2 4
2 6 7 4 5 3 1
7 4 2
2 6 7 4 5 3 1

7 人解决,21 人已尝试。

8 份提交通过,共有 60 份提交。

8.0 EMB 奖励。

创建: 13 年,8 月前.

修改: 4 年,5 月前.

最后提交: 1 年,2 月前.

来源: Codewars 2006