zoukankan      html  css  js  c++  java
  • CodeForces 556C

    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个套娃和一共k个链,最后要求从1-n按顺序组装好的最小时间,在一秒内你可以有两种操作:操作一:将该链上的最后一个套娃拆除,操作二:在该链上链接一个套娃。

    解题思路:刚开始我没有读懂题,总的来说就是一个链接拆除的过程,但是每次只能对一个套娃进行操作,题目要求是求按顺序组装套娃所需的最短时间,我们先分析 如果链1是 1 2 3 5 链2 是4 6 7 则123不动,把4拆除,在链2中:我们都要拆除(不管是连续还是不连续的),因为最后我们要按顺序组装,而且每次只能操作一个套娃,所以不管链2是否连续 我们都必须拆除才能组装到最后的链子上。经过分析可以发现,除了1所在的链条,其他链条必须全部拆除然后全部重装,而对于1链而言,只需要拆除和重装后面非连续的部分,例如1-2-4-5-6,我们只需要拆除重装4 5 6即可,因为1 2 已经在最后要组装的链子了。对于这个题。我们只需要判断当前处理的是不是1链条即可。如果不是,则拆除s-1次,重装s次(s为该链的长度),如果是,则求出1链非连续的长度s 拆数s-1次,重装 s 次(需要特殊判断1链是不是本来就是连续的,如果本来是连续的则不需要做任何处理),我们开两个变量sum和sum1,每次重装的次数加到sum中,每次拆除的次数放在sum1中,最后输出(sum+sum1)即可。AC代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int _max=1e5+50;
    int a[_max];
    int n,k;
    int main()
    {
    	ios::sync_with_stdio(false);
    	int sum=0,sum1=0;
    	cin>>n>>k;
    	while(k--)
    	{
    		int s;
    		cin>>s;
    		for(int i=1;i<=s;i++)
    		  cin>>a[i];
    		if(a[1]==1)
    		{
    			int flag=0;//需判断是不是本来就是连续的
    			for(int i=1;i<=s;i++)
    			  if(a[i]!=i)
    			  {
    				  flag=i;
    				  break;
    			  }
    			if(flag!=0)
    			{
    				sum+=(s-flag+1);
    				sum1+=(s-flag+1);
    			}
    		}
    		else
    		{
    			sum+=s;
    			sum1+=(s-1);
    		}
    	}
    	cout<<(sum+sum1)<<endl;
    	return 0;
    }
    
  • 相关阅读:
    Package manager has died异常PackageInfo 引发 Crash
    Android Bitmap变迁与原理解析(4.x-8.x)
    Rxjava2不能再发射Null了
    [转]C语言的int最值问题,以及原码反码及补码
    自定义gradle插件
    ReentrantLock(重入锁)的使用
    HashSet、TreeSet和LinkedHashSet分别基于HashMap、TreeMap和LinkedHashMap
    Java类加载双亲委托模式优点
    为什么HTTPS比HTTP安全,以及两者的优缺点
    android4.4之后的HttpUrlConnection的实现是基于okhttp
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294306.html
Copyright © 2011-2022 走看看