codeforces556C 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 ain 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

题意:

输入样例n和k为n个娃娃和k个娃娃串,然后有k行,每行的第一个为这一串上有m个娃娃。

我们的目的就是将这k串的娃娃串在一起成一个串且从左到右的娃娃的编号是递增的才行

我们有两种操作:

1.拆出当前串中最大编号的娃娃,让这个娃娃单着(且一定是最右边的娃娃)。

2.将一个单着的娃娃连接到串上(不能和其他娃娃相连着的娃娃)。

问最少操作次数,使得所有娃娃串在一起

思路:如果当前这一串娃娃的起点不是 1,那么因为要一个一个娃娃的加到串上,显然这一整串是一定需要拆开的,然后操作次数为:这一串娃娃的个数-1

如果当前这一串娃娃的起点是1,那么找到他是否按123……递增,找到他断开的位置ans,然后用m - ans就是他后面需要断开次数。

最后在现在的sun记录的断开的次数,因为断开的次数总是比单着的串的次数少k-1,例如一个串上有五个娃娃,那个将其断开需要操作4次,

而将其连接到总串上却要操作五次

所以将sum*2也就相当于将他连接到总串上然后再加上少着的k-1就是将少着的那一次加上。

具体看代码。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N];
int main()
{
    int n,k,m,t;
    cin>>n>>k;
    t = k;
    int sum = 0;
    while(t--) {
        cin>>m;
        cin>>a[0];
        if(a[0] == 1) {
            for(int i = 2; i <= m; i++) {
                cin>>a[i];
            }
            int ans = 1;
            for(int i = 2; i <= m; i++) {
                if(a[i] == i) {
                    ans = i;
                }
                else {  //记录不连续也就是断开的位置
                    break;
                }
            }
            sum += m - ans;   //将这一串断开的操作数
        }
        else {    
            for(int i = 2; i <= m; i++) {
                cin>>a[i];
            }
            sum += m - 1;   //只要这一串的开头不是1,就必须全断开
        }
    }
    sum *= 2;   //因为断开的时候时操作数,连接起来的时候时所有单着的娃娃连接,
                       所以先乘以2,然后再加上缺失的k-1
    sum += k - 1;
    cout<<sum<<endl;
}                

  

原文地址:https://www.cnblogs.com/clb123/p/12442550.html