Codeforces 556C Case of Matryoshkas【模拟】

本文探讨了一个有趣的算法问题:如何通过最少的操作步骤将多个已排序的俄罗斯套娃链重构为一个单一的大链。文章分析了两种基本操作——拆分与组合,并提供了一种高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题目大意:

一共有N个娃娃,我们的目的就是将所有的娃娃串在一起。

我们需要保证从左到右的娃娃的编号是递增的才行。

我们有两种操作:

①拆出当前串中最大编号的娃娃(且一定是最右边的娃娃)。

②连接一个单个的娃娃(不能和其他娃娃相连着的娃娃)。

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


思路:


1、根据题目要求可知,如果当前这一串娃娃的起点不是 1,那么显然这一整串是一定需要拆开的,操作次数为:这一串娃娃的个数-1.

如果当前这一串娃娃的起点是1,但是之后的娃娃不是以a【i】-a【i-1】==1的形式递增的,那么后边的部分也要拆开。


2、将所有的娃娃都拆开了之后,我们再将每个娃娃一个一个的搭上去即可。


Ac代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int output=0;
        int len=0;
        for(int i=0;i<m;i++)
        {
            int k;
            int flag=0;
            scanf("%d",&k);
            for(int j=0;j<k;j++)
            {
                int x;
                scanf("%d",&x);
                if(j==0&&x==1)flag=1,len=1;
                if(j>0&&flag==0)output++;
                if(j>0&&flag==1)
                {
                    if(j==x-1)
                    {
                        len++;
                    }
                    else
                    {
                        flag=0;
                        output++;
                    }
                }
            }
        }
        printf("%d\n",output+n-len);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值