Codeforces Round #310 (Div. 1)——A水——Case of Matryoshkas
2015-07-16 20:16
603 查看
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 1to 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 ofb.
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 numbersai1, 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 Input
Input
Output
Input
Output
Hint
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.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1to 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 ofb.
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 numbersai1, 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 Input
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
Hint
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<cstring> #include<algorithm> using namespace std; int n, k; int a[100010]; int main() { int m, sum; while(~scanf("%d%d", &n, &k)){ int ans = 0; int sum = 0; int flag = 0; for(int i = 1; i <= k ;i++){ scanf("%d", &m); ans += m - 1; memset(a, 0, sizeof(a)); for(int j = 1; j <= m ;j++){ scanf("%d", &a[j]); } if(flag == 1) continue; for(int j = 1; j < m; j++){ if(a[j] == 1 && a[j+1] == 2 && flag == 0) {flag = 1; sum = 1;} else if(flag == 1 && a[j+1] == a[j] + 1) sum++; else if(flag == 1) break; } } ans -= sum ; ans += (n - sum - 1); printf("%d\n", ans); } return 0; }
相关文章推荐
- 2015 HUAS Provincial Select Contest #3 C
- MeteoInfoLab脚本示例:MODIS Sinusoidal投影HDF数据
- 习题2-1 水仙花数(daffodil)
- HTML 5 应用程序缓存
- CSS规范和最佳实践(转)
- 指针的问题3 指针的强制类型转换
- Git笔记
- 6.osg中光照的使用
- Android性能优化相关资料
- 习题1-7 年份(year)
- 一点安全攻防
- 第一个hello word 驱动加载失败--------
- 第一个hello word 驱动加载失败--------
- MMO可见格子算法
- 我的Android进阶之旅------>Android中的布局优化 include、merge 、ViewStub
- fio使用指南
- python yaml使用
- 5.osg中用顶点绘制球体并贴上地球纹理
- ffmpeg基本用法(转)
- POJ - 2411 Mondriaan's Dream (状态压缩)