問題一覧 > 通常問題

No.742 Nyan Nyan Nyan: Cat Greetings

レベル : / AI推定難易度 : 1000 / 実行時間制限 : 1ケース 2.500秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 171
作問者 : horiesiniti / テスター : はむこ
お気に入りにしたユーザー ProblemId : 2147 / 自分の提出
問題文最終更新日: 2018-10-06 19:28:32
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 201 (順位表) の他の問題:

Problem Statement

There are n cats in total, one at each integer coordinate from 1 to n on a number line.
Every cat moves from its current coordinate to its destination integer coordinate.
The cats greet each other every time they pass each other.
All the cats start moving at the same time, and each of them moves at a constant speed. How many greetings are exchanged in total? Given the movement data, find the number of greetings.

A cat that reaches its destination integer coordinate first waits there and greets every time another cat passes it.
The cats are polite, so whenever they pass each other or end up at the same coordinate, they always greet one-on-one.
That is, if m cats are at the same coordinate, m(m-1)/2 greetings occur.

2018/10/06 19:28 The constraint was changed to 2.5 seconds.

Input

$N$
$M_1$
$\dots$
$M_i$
$\dots$
$M_n$

N is the number of cats.
$3 \le N \le 30000$
$M_i$ means that the cat at integer coordinate $i$ moves to integer coordinate $M_i$.
$1 \le M_i \le N$
For distinct $i$,$j$, $M_i \ne M_j$

Output

Ans Print a newline at the end.

Sample

Sample 1
Input
3
1
2
3
Output
0

No cat moves from its initial position, so naturally there are no greetings. The cats seem to be taking a nap.

Sample 2
Input
9
9
3
4
2
5
7
6
8
1
Output
18

The point to note is that the cats on squares 2,3 do not overtake each other, so they do not greet each other. The cat on square 1 passes the waiting cats on squares 2 and 3, so greetings occur.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。