Logo
(追記) (追記ここまで)

23857번 - Bookshelf Sorting 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB24121260.000%

문제

Irma works in a library. Every day she watches visitors take a couple of books from the bookshelf, read them, and put books back in the same places they took them. Usually people mess up the order and swap two books they read. Let's take a look at one specific bookshelf with $n$ books in some order, numbered from 1ドル$ to $n$ from left to right. The $i$-th visitor takes books from positions $x_i$ and $y_i$ and puts them back on the same positions, but in the wrong order. After the $i$-th visitor, the book that was at $x_i$ is now at position $y_i$ and vice versa.

In the evening, after the library is closed, Irma wants to put all the books back on their places. For each book there is a number $p_i$ --- a position where that book should end up in the end. To rearrange books, Irma can take any book from the shelf and insert it in the beginning or in the end (so it ends up in the first or in the last place on the shelf).

What is the minimum number of moves Irma can do to put all books in order? Answer this question for some initial placement of books, determined by $p_i,ドル and after each visitor that swapped places of some two books.

입력

The first line contains two integers $n$ and $q$ (2ドル \le n \le 2 \cdot 10^5$; 0ドル \le q \le 2 \cdot 10^5$) --- the number of books on the shelf and the number of visitors. The next line contains $n$ distinct integers $p_i$ (1ドル \le p_i \le n$), meaning that the book in the $i$-th position must end up in position $p_i$.

Next $q$ lines describe library's visitors. Each line contains two integers $x_i,ドル $y_i$ (1ドル \le x_i < y_i \le n$), that mean that the $i$-th visitor swapped two books on positions $x_i$ and $y_i$.

출력

Print $q + 1$ integers --- the minimum number of moves required to sort all books for the initial order, then for the order the after the first visitor, \ldots, after all $q$ visitors.

제한

서브태스크

번호배점제한
110

$n, q \le 8$

215

$n, q \le 200$

315

$n \le 2000$; $q = 0$

415

$n, q \le 2000$

520

$n \le 2 \cdot 10^5$; $q = 0$

625

$n, q \le 2 \cdot 10^5$

예제 입력 1

5 2
5 1 2 4 3
4 5
1 4

예제 출력 1

2
1
3

힌트

The initial order of books is $(5, 1, 2, 4, 3)$. To sort these books out, first, put the book 4ドル$ in the end, then do the same with book 5ドル$. After the first visitor, the bookshelf now looks like $(5, 1, 2, 3, 4)$; it's enough to just move the book 5ドル$ to the end. After the second visitor the order of books is $(3, 1, 2, 5, 4)$. For this order the minimum number of moves is 3ドル,ドル there are several ways to achieve the final order in 3ドル$ moves.

출처

Contest > Innopolis Open in Informatics > Innopolis Open 2020-2021, Qualification, Contest 1 D번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /