Codeforces 556C Case of Matryoshkas【模拟】
来源:互联网 发布:安卓手机php服务器 编辑:程序博客网 时间:2024/05/16 10:38
题目大意:
一共有N个娃娃,我们的目的就是将所有的娃娃串在一起。
我们需要保证从左到右的娃娃的编号是递增的才行。
我们有两种操作:
①拆出当前串中最大编号的娃娃(且一定是最右边的娃娃)。
②连接一个单个的娃娃(不能和其他娃娃相连着的娃娃)。
问最少操作次数,使得所有娃娃串在一起。
思路:
1、根据题目要求可知,如果当前这一串娃娃的起点不是 1,那么显然这一整串是一定需要拆开的,操作次数为:这一串娃娃的个数-1.
如果当前这一串娃娃的起点是1,但是之后的娃娃不是以a【i】-a【i-1】==1的形式递增的,那么后边的部分也要拆开。
2、将所有的娃娃都拆开了之后,我们再将每个娃娃一个一个的搭上去即可。
Ac代码:
#include<stdio.h>#include<queue>#include<string.h>using namespace std;int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { int output=0; int len=0; for(int i=0;i<m;i++) { int k; int flag=0; scanf("%d",&k); for(int j=0;j<k;j++) { int x; scanf("%d",&x); if(j==0&&x==1)flag=1,len=1; if(j>0&&flag==0)output++; if(j>0&&flag==1) { if(j==x-1) { len++; } else { flag=0; output++; } } } } printf("%d\n",output+n-len); }}
0 0
- Codeforces 556C Case of Matryoshkas【模拟】
- CodeForces-556C Case of Matryoshkas
- 【35.37%】【codeforces 556C】Case of Matryoshkas
- CodeForces 556C Case of Matryoshkas 娃娃组装
- Case of Matryoshkas CodeForces
- Codeforces #310 div2 C. Case of Matryoshkas
- C. Case of Matryoshkas( Codeforces Round #310 (Div. 2) 模拟)
- 【CodeForces 555A】Case of Matryoshkas(模拟)
- CF#310-C-Case of Matryoshkas-模拟水题
- CodeForces 567C Case of Matryoshkas map+递推
- COdeforces 555 A. Case of Matryoshkas
- CodeForces 555A Case of Matryoshkas
- Codeforces #310ACase of Matryoshkas(模拟)
- codeforces_C. Case of Matryoshkas
- A. Case of Matryoshkas
- Codeforces 555A Case of Matryoshkas 套娃娃
- CodeForces 555A Case of Matryoshkas 娃娃组装
- [Codeforces Round #310 DIV1A (CF555A)] Case of Matryoshkas
- git merge 原理 ,wiz 等云同步原理
- Atitit 常用比较复杂的图像滤镜 attilax大总结
- leetcode(36).125. Valid Palindrome
- mybatis基本配置之别名(TypeAlias)
- SpringMVC整合Quartz实现定时任务和Crontrigger时间配置和版本问题
- Codeforces 556C Case of Matryoshkas【模拟】
- 解析——java匿名内部类使用外部变量时,外部变量必须是final
- ps搞定“网上报名要求照片在20k以内”【图文】
- 给出一列数a1,a2,a3....an,求它们的逆序对数,即有多少个有序对(i,j) 使得i<j,ai>aj,n高达10的6次方
- cp
- 左值和右值
- js调用百度地图API,实现地图标注位置
- JS ready()和onload()的区别
- 复杂链表的复制