原题: http://codeforces.com/contest/556/problem/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.
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到n的娃娃,组装前需要拆。
这道题我们可以看作是火车的拼接。
1是火车头必须在最前面不解释,后面必须跟着2,3,……n。
假设我们只有一个出发轨道,装完就出发了,那么最先装的就是1,然后是2……
每次拆只能从后面拆1个,组装也是从后面添加一个,所以除了从1开始的连续序列可以保留,其他的都要拆。
因为原来的火车都是单调上升的序列,所以如果有1肯定在最前面,只需要保留连续单调升序列,剩下的拆,不是1开头的拆完。
组装的时候就是从那个1开头的连续序列开始加。
代码:
#include <iostream>
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"algorithm"
using namespace std;
typedef long long int lint;
int num[100005];
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
int len=0;
int ans=0;
int k;
bool flag=0;
while(m--)
{
scanf("%d",&k);
for(int i=1; i<=k; ++i)
{
scanf("%d",&num[i]);
if(num[i]==1)
{
flag=1;
}
}
if(flag) //拆后面的
{
len=1; //不用拆的长度
for(int i=2; i<=k; ++i)
{
if((num[i])==(num[i-1]+1))
len++;
else break;
}
ans=ans+k-len;
flag=0;
}
else //全拆
{
ans=ans+k-1;
}
}
ans=ans+n-len;
printf("%d\n",ans);
}
return 0;
}