CF555A.Case of Matryoshkas
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
安卓侦探安德鲁伊德是银河系闻名的侦探。他目前正在调查一场当代艺术展上的蓄意破坏案件。
展览的核心展品是由 n 个套娃(俄罗斯套娃)组成的结构,这些套娃可以彼此嵌套。套娃编号为 1 到 n。编号较小的套娃可以嵌入编号较大的套娃中;任意一个套娃中不能直接嵌套两个或更多其他套娃,但允许形成嵌套链,例如 1→2→4→5。
每秒钟,你可以执行以下两种操作之一:
- 若存在一个未被任何其他套娃嵌套的套娃 a,以及一个内部未包含任何其他套娃、且自身也未被任何其他套娃嵌套的套娃 b,则可将 a 放入 b 中;
- 若存在一个套娃 a 直接嵌套于套娃 b 内部,且 b 未被任何其他套娃嵌套,则可将 a 从 b 中取出。
根据现代审美规范,展览中原本的套娃摆放方式是若干条彼此分离的嵌套链;然而罪犯依照某种神秘计划,将所有套娃全部取出,并重新组装成了一条单一的长链:1→2→…→n。为了继续调查,安德鲁伊德需要知道:完成这一重组操作所需的最短时间是多少?
输入格式
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — 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__i_1, _a__i_2, ..., a__im__i — the numbers of matryoshkas in the chain (matryoshka _a__i_1 is nested into matryoshka _a__i_2, that is nested into matryoshka _a__i_3, and so on till the matryoshka a__im__i 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.
第一行包含两个整数 n(1≤n≤105)和 k(1≤k≤105)——分别表示套娃的总数以及初始配置中套娃链的数量。
接下来的 k 行描述了这些链:第 i 行首先给出一个整数 mi(1≤mi≤n),然后是 mi 个整数 ai1, ai2, …, aimi——表示该链中套娃的编号(套娃 ai1 套入套娃 ai2,后者又套入套娃 ai3,依此类推,直到最外层的套娃 aimi,它不再套入任何其他套娃)。
保证 m1+m2+⋯+mk=n,所有链中的套娃编号互不相同,且每条链中的套娃编号按升序排列。
输出格式
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.
在第一个样例测试中,存在两条链:1 → 2 和 3。在一秒钟内,你可以将第一条链嵌套进第二条链中,从而得到 1 → 2 → 3。
在第二个样例测试中,你需要先用 2 + 1 + 1 = 4 秒将全部三条链拆解为单个套娃,然后再用 6 秒组装成一条长链。
输入解题思路,AI测评打分。不知道怎么写?