贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

 

题目传送门

 1 /*
 2     题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步
 3     贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数,
 4                 其他是2次操作,还要减去k-1个娃娃是只要套上就可以
 5     详细解释:http://blog.csdn.net/firstlucker/article/details/46671251
 6 */
 7 #include <cstdio>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <cmath>
11 using namespace std;
12 
13 const int MAXN = 1e5 + 10;
14 const int INF = 0x3f3f3f3f;
15 int a[MAXN];
16 
17 int main(void)      //Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
18 {
19     // freopen ("C.in", "r", stdin);
20 
21     int n, k;
22     while (scanf ("%d%d", &n, &k) == 2)
23     {
24         int cnt = 0;
25         for (int i=1; i<=k; ++i)
26         {
27             int m;    scanf ("%d", &m);    int p = 0;
28             for (int j=1; j<=m; ++j)
29             {
30                 scanf ("%d", &a[j]);    if (a[j] == 1)    p = j;
31             }
32             if (p)
33             {
34                 for (int j=p+1; j<=m; ++j)
35                 {
36                     if (a[j] == a[j-1] + 1)    cnt++;
37                     else    break;
38                 }
39             }
40         }
41         printf ("%d\n", (n - 1 - cnt) * 2 - (k - 1));
42     }
43 
44     return 0;
45 }

 

posted @ 2015-06-29 17:04  Running_Time  阅读(232)  评论(0编辑  收藏  举报