302863: CF555A. Case of Matryoshkas

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Case of Matryoshkas

题意翻译

这是一套有n个娃娃的套娃,由1到n依次编号。编号小的娃娃可以放进编号大的娃娃里面,但是不能平行的放入两个娃娃,即套娃在嵌套放置的时候只能一个套一个例如1 → 2 → 4 → 5。 又两个操作 1. 选择两个娃娃a,b,要求a<b,a没有被其他的娃娃套起来,b既没有被其他娃娃套起来,也没有套其他娃娃。然后把a放到b里面。 2. 选择两个娃娃a,b,要求a<b,a已经被b套起来,b没有被其他娃娃套起来。然后把a从b里面拿出来。 现在这n个娃娃相互嵌套成了k个套娃摆在桌上,Andrewid想知道最少多少次操作可以把这n个娃娃嵌套成1个套娃,即1→2→...→n-1→n。 $1<=k<=n<=10^5$

题目描述

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of $ n $ matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from $ 1 $ to $ n $ . A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, $ 1→2→4→5 $ . In one second, you can perform one of the two following operations: - Having a matryoshka $ a $ that isn't nested in any other matryoshka and a matryoshka $ b $ , such that $ b $ doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put $ a $ in $ b $ ; - Having a matryoshka $ a $ directly contained in matryoshka $ b $ , such that $ b $ is not nested in any other matryoshka, you may get $ a $ out of $ b $ . According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain ( $ 1→2→...→n $ ). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

输入输出格式

输入格式


The first line contains integers $ n $ ( $ 1<=n<=10^{5} $ ) and $ k $ ( $ 1<=k<=10^{5} $ ) — the number of matryoshkas and matryoshka chains in the initial configuration. The next $ k $ lines contain the descriptions of the chains: the $ i $ -th line first contains number $ m_{i} $ ( $ 1<=m_{i}<=n $ ), and then $ m_{i} $ numbers $ a_{i1},a_{i2},...,a_{imi} $ — the numbers of matryoshkas in the chain (matryoshka $ a_{i1} $ is nested into matryoshka $ a_{i2} $ , that is nested into matryoshka $ a_{i3} $ , and so on till the matryoshka $ a_{imi} $ that isn't nested into any other matryoshka). It is guaranteed that $ m_{1}+m_{2}+...+m_{k}=n $ , the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

输出格式


In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

输入输出样例

输入样例 #1

3 2
2 1 2
1 3

输出样例 #1

1

输入样例 #2

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

输出样例 #2

10

说明

In the first sample test there are two chains: $ 1→2 $ and $ 3 $ . In one second you can nest the first chain into the second one and get $ 1→2→3 $ . In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

Input

题意翻译

这是一套有n个娃娃的套娃,由1到n依次编号。编号小的娃娃可以放进编号大的娃娃里面,但是不能平行的放入两个娃娃,即套娃在嵌套放置的时候只能一个套一个例如1 → 2 → 4 → 5。 又两个操作 1. 选择两个娃娃a,b,要求a<b,a没有被其他的娃娃套起来,b既没有被其他娃娃套起来,也没有套其他娃娃。然后把a放到b里面。 2. 选择两个娃娃a,b,要求a<b,a已经被b套起来,b没有被其他娃娃套起来。然后把a从b里面拿出来。 现在这n个娃娃相互嵌套成了k个套娃摆在桌上,Andrewid想知道最少多少次操作可以把这n个娃娃嵌套成1个套娃,即1→2→...→n-1→n。 $1<=k<=n<=10^5$

加入题单

算法标签: