zoukankan      html  css  js  c++  java
  • CodeForces

    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.

    思路:链上从1开始连续的数字不用拆。一旦遇到不连续,则从这个不连续的娃娃开始后面的全部都要拆。

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    int a[maxn];
    int main()
    {
       int n,k;
       cin>>n>>k;
       int m;
       ll sum1=0,sum2=0;
       for(int t=0;t<k;t++)
       {
       	   scanf("%d",&m);
       	   int s=1;
       	   int x,x1;
       	   scanf("%d",&a[0]);
       	   int flag=0;
       	   if(a[0]==1)
       	   {
       	   	flag=1;
    	   }
       	   for(int k=1;k<m;k++)
       	   {
       	      	scanf("%d",&a[k]);
       	      	if(flag==1&&(a[k]-a[k-1]==1))
       	      	{
       	      		continue;
    			}
    			else
    			{
    				flag=0;
    				s++;
       	      		sum2++;
    			}
    	   }
    	   sum1+=s;
       }
       sum1=sum1-1+sum2;
       if(sum1<0)
       {
       	cout<<0<<endl;
       }
       else
       cout<<sum1<<endl;
       return 0;
    }
    
  • 相关阅读:
    我的ZigBee学习之路
    php form表单post提交获取不到数据,而使用get提交能获取到数据 的解决办法
    Mac phpstorm破解版安装(简单,有效)
    Mac下phpstorm 浏览器出现 502 bad gateway 解决办法
    LayUI之table数据表格获取行、行高亮等相关操作
    钉钉自定义机器人配合SVN钩子事件进行消息的推送实践
    电脑无故失去焦点,罪魁祸首是谁?终极解决办法
    Java实现的电脑已连接WiFi热点的导入导出小工具 wifi备份
    C# DataGridView自定义分页控件
    C# DataGridView控件禁止拷贝数据
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781740.html
Copyright © 2011-2022 走看看