CodeForces 555A Case of Matryoshkas 娃娃组装

来源:互联网 发布:淘宝买aj哪家店好 编辑:程序博客网 时间:2024/05/17 07:03

链接:http://codeforces.com/problemset/problem/555/A

A. Case of Matryoshkas
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 from1 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 matryoshkab, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may puta in b;
  • Having a matryoshka a directly contained in matryoshkab, such that b is not nested in any other matryoshka, you may geta 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) andk (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next k lines contain the descriptions of the chains: thei-th line first contains number mi (1 ≤ mi ≤ n), and thenmi numbersai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshkaai2, that is nested into matryoshkaai3, and so on till the matryoshkaaimi 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.

Sample test(s)
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 get1 → 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.



题意:将N个娃娃从组装成1->2->3->4->.....->n。组装规则,只有独立的大娃娃可以她在比它小的链中(小是指链的最右端即最大值小于这个独立的大娃娃)


分析:需要组装1到n的娃娃,组装前需要拆。
这道题我们可以看作是火车的拼接。
1是火车头必须在最前面不解释,后面必须跟着2,3,……n。
假设我们只有一个出发轨道,装完就出发了,那么最先装的就是1,然后是2……
每次拆只能从后面拆1个,组装也是从后面添加一个,所以除了从1开始的连续序列可以保留,其他的都要拆。
因为原来的火车都是单调上升的序列,所以如果有1肯定在最前面,只需要保留连续单调升序列,剩下的拆,不是1开头的拆完。
组装的时候就是从那个1开头的连续序列开始加。


至于结果ans=2(n-cnt)-k+1,大家理解以上就很容易可以自己推解出来。


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int main(){   int n,k;   while(scanf("%d %d",&n,&k) != EOF)   {       int cnt=0,c,x;       for(int i=1;i<=k;i++)       {           scanf("%d",&c);           for(int j=1;j<=c;j++)           {               scanf("%d",&x);               if(x==j) cnt++;           }       }       int ans=2*(n-cnt)-k+1;       printf("%d\n",ans);   }   return 0;}


0 0