Codeforces #310 div2 C. Case of Matryoshkas

C. Case of Matryoshkas

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.


(赛时补充)
It is not possible to put a matryoshka A inside the matryoshka B if matryoshka B is already nested in some other matryoshka C.

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.

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


题目意思就是,有一种有大小分别的玩偶,小的可以放进大的里面,最后可以放成仅有一个大玩偶。


可以理解成一个小的放进大的里,那我们从外面看就只能看到这个大的。
就是一个大的只能装一个小的,而这个小的只能装一个比它更小的。

具体每个操作就是:
1、可以把一个小的玩偶放进一个,还没有东西的大玩偶里面。
2、若一个大的玩偶里包含一个小的玩偶,这个小的玩偶也包含次小的玩偶,我们如果要拿出这个次小的玩偶,必须先拿出小玩偶先。

弄懂了上面2点,不难想到:
1、仅有连续的一条chain中有1,2,3,4 ……,n是不需要再取出来。
2、其余的玩偶都要有两个操作:取出,放入。
3、存在最大的玩偶于一条链,其余小玩偶需要放入最大玩偶的数为k-1。


反思:
打的时候没有做出来,思路很乱,而且写得也乱。


代码如下:

#include<cstdio>
using namespace std;
int a[100000 + 50];
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    int sum = 0;
    for(int i = 0; i < k; ++i) {
        int num;
        scanf("%d", &num);
        int flag1 = 0, flag2 = 0;
        for(int j = 0; j < num; ++j) {
            scanf("%d", &a[j]);
            if(!flag1 && a[0] == 1 && j > 0) {
                if(a[j]-a[j-1] != 1) {
                    if(j == 1) sum += (num-1)*2;
                    else sum += (num-j)*2; // j 指向第一个不连续的数
                    flag1 = 1;
                }
            }
            if(!flag2 && a[0] != 1) {
                sum += (num-1)*2;
                flag2 = 1;
            }
        }
    }
    printf("%d\n", sum + k - 1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值