C. Case of Matryoshkas( Codeforces Round #310 (Div. 2) 模拟)

来源:互联网 发布:象过河软件怎么用 编辑:程序博客网 时间:2024/05/16 07:15
C. 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 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 ofb.

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

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 ~ n。要求一条链子上的娃娃必须满足编号小的娃娃套在编号大的娃娃上。规定两种操作:1、拆下编号最大的娃娃。2、将一条链上的娃娃套在一个没有套在任何其他链子上的娃娃上(有点绕)。意思就是比如有一条链1→2→4→5,你可以拆下5,然后变成两条链1→2→4和5,或者将这条链套在6上,但是6不能已经套在任何其他链子上,变成1→2→4→5→6。题目要求用最少的操作步数将所有的娃娃套在一个链子上。(谢谢这位提供题意的博主)


点击打开链接


#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<stack>using namespace std;int a[100005];int n,m;int k;int cnt;int main() {    while(scanf("%d%d",&n,&m)!=EOF) {        int t = 0;        cnt = 0;        for(int i=0; i<m; i++) {            scanf("%d",&k);            int flag = 0;            int minn = 999999;            for(int j=0; j<k; j++) {                scanf("%d",&a[j]);                if(j == 0 && a[0] == 1) {                    flag = 1;                } else if(flag == 1) {                    if(a[j] - 1 != a[j-1]) {                        flag = 2;                        t = a[j-1];                        cnt += k - j;                    }                } else if(flag == 0) {                    if(a[j]>minn) {                        cnt++;                    } else {                        minn = a[j];                    }                }            }            if(flag == 1) {                t = a[k-1];            }        }        cnt += n - t;        printf("%d\n",cnt);    }    return 0;}


0 0
原创粉丝点击