CodeForces - 555A
俄罗斯套娃,要想将他分开必须从最外面开始操作
首先假设是完全分开的需要n-1次操作试他们连接起来,
而在输入的序列中只有当1开头的一段连续序列不需要解开
其余非1开头序列必须全部解开,
而1开头的只需要解开尾部不连续的部分,即前面的不需要链接,
只需要减去前面已连接的操作就可以了,
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
struct h{
int s;
int m;
};
bool cmp (h a,h b){
return a.s<b.s;
}
int p[300000];
int main(){
while(~scanf("%d%d",&n,&m)){
int ans=n-1;
for(int i=0;i<m;i++){
int x;
scanf("%d",&x);
for(int j=0;j<x;j++){
scanf("%d",&p[j]);
}
if(p[0]!=1){
ans+=(x-1);
continue;
}
int f=0;
for(int j=1;j<x;j++){
if(p[j]-p[j-1]!=1){
f=x-j;
break;
}
}
ans+=f;
ans-=(x-1-f);
}
if(m==0)
printf("%d\n",n);
else
printf("%d\n",ans);
}
return 0;
}