C题:这题说的是套娃,如果做题的时候知道是套娃,那就好理解多了
规则1:套娃A可以放到套娃B里面,当且仅当套娃B没有放在其他套娃里面
规则2:套娃A放在套娃B里面,且套娃B没有放在其他套娃里面,那么可以把A从B中拿出来
问我们最少要操作多少次,才能将套娃全部套起来,拆开和组装都算是一次操作
思路:找到序号为1的套娃的哪一组,然后统计该组有多少个套娃是连在1后面,且每次序号都是加1的,那么这些个套娃是不用拆开的。那么剩下的套娃都是要拆开的
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <string> 12 #include <math.h> 13 using namespace std; 14 #pragma warning(disable:4996) 15 typedef long long LL; 16 const int INF = 1<<30; 17 /* 18 19 */ 20 int a[100000 + 10]; 21 int main() 22 { 23 24 int n, k, m; 25 int ans; 26 int cnt; 27 while (scanf("%d%d", &n, &k) != EOF) 28 { 29 ans = cnt = 0; 30 for (int i = 0; i < k; ++i) 31 { 32 scanf("%d", &m); 33 bool flag = false; 34 for (int j = 0; j < m; ++j) 35 { 36 scanf("%d", &a[j]); 37 if (a[j] == 1) 38 { 39 flag = true; 40 } 41 } 42 43 if (flag) 44 { 45 cnt = 1; 46 for (int j = 1; j < m; ++j) 47 { 48 if (a[j] - 1 == a[j - 1]) 49 cnt++; 50 else 51 break; 52 } 53 ans += m - cnt; 54 flag = false; 55 } 56 else 57 ans += m - 1; 58 } 59 ans += n - cnt; 60 printf("%d\n", ans); 61 } 62 return 0; 63 }