CodeForces - 556C Case of Matryoshkas

Posted Styx-ferryman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 556C Case of Matryoshkas相关的知识,希望对你有一定的参考价值。

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

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

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

var
  a,b:array[1..100000] of longint;
  t,i,j,k,m,n,o:longint;
begin
  readln(m,n);
  o:=1;
  for i:=1 to n do
  begin
    read(k);
    for j:=1 to k do
    begin
        a[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.

 

以上是关于CodeForces - 556C Case of Matryoshkas的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 556D Case of Fugitive (贪心+排序)

Codeforces 556D - Case of Fugitive

CodeForces - 556A Case of the Zeros and Ones

Codeforces 555 B. Case of Fugitive

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

delphi中的case of语句