/// 
作者:stxy-ferryman
声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 
查看本文更新与讨论请点击:http://www.cnblogs.com/stxy-ferryman/
链接被删请百度:stxy-ferryman
///

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 bdoesn'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.

Input

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 mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).

It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

Output

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

Examples
input
3 22 1 21 3

output
1

input
7 33 1 3 72 2 52 4 6

output
10

Note

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.

题意:号称自己是宇宙知名的侦探xxx,突发奇想去玩套娃,结果就作死地被难住了。这套套娃有1-n个,1最小,n最大,被乱套成k堆。当然,出题人为了方便,这乱套的k堆遵循从大到小套,但不能两个小的套在同一个大套娃里。问你他把这群套娃重新按1-n排好供需几步?

题解: 如果不是从一开始连着套的套娃,总是要拆开来的,多分几份反而是方便了,有k份就少开了k个,合并共需n-1次。有1-l连的情况,拆分合并均省k次,综上所述,共需2n+1-k-2l次

vara,b:array[1..100000] of longint;t,i,j,k,m,n,o:longint;
beginreadln(m,n);o:=1;for i:=1 to n dobeginread(k);for j:=1 to k dobegina[j]:=0;read(a[j]);end;while a[o]=o do inc(o);readln;end;dec(o);writeln(m-n+m+1-o*2);
end.

转载于:https://www.cnblogs.com/stxy-ferryman/p/7070630.html

CodeForces - 556C Case of Matryoshkas相关推荐

  1. CodeForces - 555A Case of Matryoshkas(思维)

    题目链接:点击查看 题目大意:类似于俄罗斯套娃,给出两个规则,每次可以选择一个规则进行操作,问最少需要多少操作才能形成1->2->...->n的局面 可以将a套入b中,当且仅当 a没 ...

  2. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  3. CodeForces - 556A Case of the Zeros and Ones

    ///  作者:stxy-ferryman 声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0  查看本文更新与讨论请点击:http: ...

  4. codeforces每日5题(均1500)-第十三天

    Case of Matryoshkas 题面翻译 这是一套有n个娃娃的套娃,由1到n依次编号.编号小的娃娃可以放进编号大的娃娃里面,但是不能平行的放入两个娃娃,即套娃在嵌套放置的时候只能一个套一个例如 ...

  5. Codeforces Round #310 (Div. 1) B. Case of Fugitive set

    B. Case of Fugitive Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/p ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 暑期集训3:几何基础 练习题F:  CodeForces - 1007A ​​​​​​​

    2018学校暑期集训第三天--几何基础 练习题F  --   CodeForces - 1007A Reorder the Array You are given an array of intege ...

  8. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  9. Codeforces Round #698 (Div. 2)(A ~ F)6题全,超高质量题解)【每日亿题】2021/2/4

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 [每日亿题]Codeforces Round #698 (Div. 2)(A ~ F)6题全,超 ...

最新文章

  1. php中mysql_PHP中MySQL操作
  2. Mozilla公布WebVR API标准草案
  3. 简单的视频采集demo
  4. 未转变者空投指令服务器,未转变者空投指令 | 手游网游页游攻略大全
  5. 马云:我看到很多人去学MBA 但回来时都变蠢了
  6. idea中生成spring的 xml配置文件_【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~...
  7. 阿里云心选-T+财务软件助力线下实体的创富转型之道
  8. Android Sqite数据库 6
  9. 华为云全球首发企业级AI应用开发套件ModelArts Pro,加速行业AI落地
  10. idea swing 插件_【分享】我的idea配置
  11. [Teamcenter 2007 开发系列] web 非空验证
  12. 在windows server 2003下如何了启动远程管理(html)
  13. SOA架构设计的案例分析
  14. 关于apache httpd.conf脚本的理解
  15. 【心得感想】谈谈工作效率及沟通
  16. 现实版“武大郎和潘金莲”的婚姻情感纠葛
  17. 巴比特国际站观察 | 海外新晋“网红”亮相,数字人民币引密码社区热议
  18. MoCo v1 文献研究 [自监督学习]
  19. 一文读懂身份证ocr识别
  20. 攻防世界-misc-miss_01

热门文章

  1. 超边际分析不能用计算机,一种基于超边际分析的分布式计算资源分配方法-Journalof.PDF...
  2. mysql输入密码就闪退_MySQL在我的服务列表里凭空消失~
  3. html去除radio的样式,【HTML+CSS】纯CSS设置checkbox大小和样式 附Radio的去除默认样式和自定义样式...
  4. Sublime Text3运行node.js
  5. Linux系统时间和java的时间不一致
  6. mysql truncate很慢_mysql truncate 的问题
  7. 动态调试 ida linux,IDA动态调试-ELF
  8. android 层叠轮播,vue手写一个卡片化层叠轮播(支持滑动,移动端连续滚动,点击)...
  9. 网页左侧固定菜单栏的展开与收起
  10. 软件工程师的技能知识图谱0.1(偏后台方向)