题目链接:http://codeforces.com/contest/556/problem/C

题目意思:有 n 个数(1,2,...,n)组成 k 条链。第 i 条链由 mi 个数组成。每一秒只可以做其中一件事:(1)将链i连到只有一个数的链 j 中(链 i 的最大值比只由单个数组成的链 j 还要小);(2)将链上最大的数从链中分离。 然后我们最终是要得到一条1,2,3,。。。n。问最少需要连接的秒数。

  不难推出,我们需要找到编号为 1 的链包含多少个数,然后对于此链的其他数,我们需要先分离然后连接。其他链也需要先分离再连接,注意,对于某条含有 x 个数的链,分离的秒数为 x - 1,而连接的秒数为 x 。 那现在问题就转化为如何找出包含 1 的链最长为多少(数是连续的!)

  假设包含 1 的链为 cnt,那么最少的秒数应为 2(n - (k-1) - cnt) + (k-1) 。2(n - (k-1) - cnt) 这个表示所有需要分离和连接的总数,但由于每条链分离秒数是比连接的秒数少1的,所以需要加回 k -1 !

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; inline int Getint()
{
char ch = getchar();
while (ch < '' || ch > '')
ch = getchar();
int ret = ;
while (ch >= '' && ch <= '') {
ret = ret * + ch - '';
ch = getchar();
}
return ret;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, k, m, a;
// while (scanf("%d%d", &n, &k) != EOF) {
n = Getint(), k = Getint();
int cc = ;
for (int i = ; i < k; i++) {
m = Getint(), a = Getint();
int cnt = ;
if (a == ) {
cnt = ; // 序列第一个数就是 1 ,否则就是序中或序尾出现 1
}
for (int j = ; j <= m; j++) {
a = Getint();
if (cnt == j- && a == j) {
cnt++;
}
}
if (cnt) cc = cnt;
}
printf("%d\n", *(n-(k-)-cc)+(k-));
// }
return ;
}

codeforces 556C. Case of Matryoshkas 解题报告的更多相关文章

  1. CodeForces - 556C Case of Matryoshkas

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  2. CodeForces - 556C Case of Matryoshkas (水题)

    Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at ...

  3. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  4. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  6. codeforces 518B. Tanya and Postcard 解题报告

    题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...

  7. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  8. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  9. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

随机推荐

  1. getStyle(),修改样式属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. webapp中的meta

    <!--开发后删除--> <meta http-equiv="Pragma" name="no-store" /><!--必须联网 ...

  3. 遍历一个类的属性--并转换为Dictionary类型

    参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...

  4. mysql 总结一

    mysql 总结一 数据类型(四类): 整型(5种:tinyint,smallint,mediumint, int(或integer),bigint ): 浮点型:(float,double), 日期 ...

  5. 清北暑假模拟day1 爱

    /* 水题 */ #include<iostream> #include<cstdio> #include<string> #include<cstring& ...

  6. Net上传附件大小控控值(转)

    Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...

  7. android常见问题(一)

    一:文本的颜色选择器: 在res目录下面创建color文件夹,在color文件夹下面创建font_style_colors.xml文件<?xml version="1.0" ...

  8. iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view

    一.懒加载 只有使用到了商品数组才会创建数组 保证数组只会被创建一次 只要能够保证数组在使用时才创建, 并且只会创建一次, 那么我们就称之为懒加载 lazy - (void)viewDidLoad 控 ...

  9. ionic隐藏tabs方法

    <ion-tabs ng-class="{'tabs-item-hide': $root.hideTabs}"> <!-- tabs --> </io ...

  10. 如何使用coding.net

        由于我有一位十分聪明能干的室友会使用coding.net,于是我决定奉献一下室友的智慧,告诉大家如何使用conding.net上交作业.(如有说错的地方希望大家可以指出来) 首先登陆codin ...