time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

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 2

2 1 2

1 3

output

1

input

7 3

3 1 3 7

2 2 5

2 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.

【题目链接】:http://codeforces.com/contest/556/problem/C

【题解】



英文题………

这个是俄罗斯套娃。。。。。。



读懂题意之后..

其实很简单了。。

每个套娃必须是单个才能套在其他套娃身上..(其他套娃里面可以套没关系,但是它本身必须是在最外面的);

且拆也只能一个一个的拆.

但是最后又必须是1..n的顺序;

则只能是序号为1的套娃,那连续的几个套娃能够节省,否则都要拆成1个一个的.

比如

7 2

4 1 2 3 5

3 4 6 7

则只有1 2 3这个可以不用拆开

剩余的都要拆成单个

即5 4 6 7都拆成单个的.

然后再一个一个地拼在1 2 3的后面.(1 2 3作为整体);

而且

7 2

4 5 1 2 3

3 4 6 7

这种也一个都不能节省需要全部都拆成单个的

(1 2 3也不能作为整体了);

因为1 2 3前面有一个5;

这个5肯定要拆开的…

而要拆开5 只能把3 、2、1都一个一个地拆开…



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n,k;
int b[MAXN];
vector <int> a; int main()
{
//freopen("F:\\rush.txt","r",stdin);
int ans = 0,tot =0;
rei(n);rei(k);
rep1(i,1,k)
{
int num,temp1=0;
rei(num);
rep1(i,1,num)
rei(b[i]);
temp1+=num;
int now = 1;
if (b[now]==1)
{
while (now+1<=num && b[now+1]==b[now]+1)
now++;
temp1-=now;
}
tot+=temp1;
if (temp1>0)
ans+=temp1-1;
if (temp1>0 && temp1 < num)
ans++;
}
//cout << ans << endl;
ans+=tot;
cout << ans << endl;
return 0;
}

【35.37%】【codeforces 556C】Case of Matryoshkas的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【35.29%】【codeforces 557C】Arthur and Table

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【35.02%】【codeforces 734A】Vladik and flights

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  7. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  8. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  9. 【链表】【模拟】Codeforces 706E Working routine

    题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...

随机推荐

  1. 【Uva 1289】Stacking Plates

    [Link]: [Description] 有n(1≤n≤50)堆盘子,第i堆盘子有hi个盘子(1≤hi≤50),从上到下直径不减.所有盘 子的直径均不超过10000.有如下两种操作. split:把 ...

  2. Dubbo学习总结(1)——Dubbo入门基础与实例讲解

    Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo是一个分布式服务框架,致力于 ...

  3. linux 配置IP地址

    linux 配置IP网址能够使用neat,netconfig,ifconfig等进行配置,当中前两个实用户界面,第三个命令仅仅是暂时设置IP,机器重新启动后配置将丢失. 有时候图形用户界面的程序难以获 ...

  4. hdu5400Arithmetic Sequence

    //一个序列,两个公差d1,d2 //问有多少个区间使得这个区间存在一个点,它的左边是公差为d1的序列 //它的右边是公差为d2的序列 //直接存入每一个点向左和向右延伸的公差长度,乘一下即可 //还 ...

  5. local-语言切换监听事件

    今天在更改时钟的问题的时候,需要监听语言切换来刷新时钟的显示.记录下监听方法 //注册监听事件 intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED) ...

  6. Java学习笔记二.2

    5.运算符:变量的意义就在于可以进行运算,运算需要用运算符来表示,分为以下几种 class test { public static void main(String[] args) { //赋值运算 ...

  7. (转)Tomcat调优

    问题定位 对于Tomcat的处理耗时较长的问题主要有当时的并发量.session数.内存及内存的回收等几个方面造成的.出现问题之后就要进行分析了. 1.关于Tomcat的session数目 这个可以直 ...

  8. Spring入门--控制反转(IOC)与依赖注入(DI)

        1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control).它把传统上由程序 ...

  9. Nginx日志优化

    一 日志轮训切割 [root@centos7 tools]# cat nginx_log.sh #!/bin/bash cd /var/log/nginx/ &&\ /bin/mv a ...

  10. vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)

    自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...