Codeforces 950 C. Zebras
http://codeforces.com/contest/950/problem/C
题意:
给出一个01序列,问能否将这个序列分为若干个0开头0结尾的序列
输出方案
如果有解,几个1能在一个序列就在一个序列 一定可以找到解
因为0可以单独1个序列,1必须要依托于0
2个1如果分开 要4个0,连起来要3个0
暴力的做法就是:
如果当前是0,扫一遍已有序列,如果有1结尾的就把这个0放到那个1的后面
没有以1结尾的序列,新建一个序列
如果当前是1,扫一遍已有序列,如果有0结尾的就把这个1放到那个0的后面
没有以0结尾的就无解
极限复杂度是n^2 的
怎么优化?
用vector,cnt记录下一个0要放在哪个vector
如果是0就放在这个cnt所在的vector,cnt++
如果是1那就放到cnt-1所在的vector,先--cnt,再放1
#include<cstdio>
#include<vector>
#include<cstring> using namespace std; #define N 200005 char s[N];
vector<int>V[N]; int main()
{
scanf("%s",s+);
int len=strlen(s+);
if(s[]=='')
{
printf("-1");
return ;
}
int cnt=,tot=;
for(int i=;i<=len;++i)
{
if(s[i]=='') V[cnt++].push_back(i);
else V[--cnt].push_back(i);
if(!cnt && s[i+]=='')
{
printf("-1");
return ;
}
tot=cnt>=tot ? cnt : tot;
}
for(int i=;i<tot;++i)
if(s[V[i][V[i].size()-]]=='')
{
printf("-1");
return ;
}
printf("%d",tot);
int siz;
for(int i=;i<tot;++i)
{
siz=V[i].size();
printf("\n%d",siz);
for(int j=;j<siz;++j) printf(" %d",V[i][j]);
}
return ;
}
Codeforces 950 C. Zebras的更多相关文章
- Codeforces 950 D. A Leapfrog in the Array
http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...
- Codeforces 950.E Data Center Maintenance
E. Data Center Maintenance time limit per test 1 second memory limit per test 512 megabytes input st ...
- Codeforces 950 010子序列拆分 数列跳棋
A B a,b两个序列分成K段 每一段的值相等 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset ...
- Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)
C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...
- CodeForces - 950C Zebras 模拟变脑洞的天秀代码
题意:给你一个01串,问其是否能拆成若干形如0101010的子串,若能,输出所有子串的0,1 的位置. 题解:一开是暴力,然后瞎找规律, 最后找到一种神奇的线性构造法:扫一遍字符串,若为0就一直竖着往 ...
- 【codeforces】【比赛题解】#950 CF Round #469 (Div. 2)
剧毒比赛,至少涨了分对吧.: ( [A]Left-handers, Right-handers and Ambidexters 题意: 有\(l\)个右撇子,\(r\)个左撇子,\(a\)个双手都惯用 ...
- Zebras CodeForces - 950C(思维)
借鉴自: https://www.cnblogs.com/SuuT/p/8619227.html https://blog.csdn.net/my_sunshine26/article/details ...
- Codeforces 950C Zebras ( 贪心 && 模拟 )
题意 : 给出一个 01 串,要求你将其分隔出若干个子序列 ( 每个数字只能属于某一个子序列 ) ,子序列必须满足由 0 开头和结尾,且中间需 01 交替构成.若无法做到,则输出 -1. 分析 : ...
- Data Center Maintenance CodeForces - 950E
http://codeforces.com/contest/950/problem/E 贴一份板子 #include<cstdio> #include<vector> #inc ...
随机推荐
- HDU 2033 人见人爱A+B
http://acm.hdu.edu.cn/showproblem.php?pid=2033 Problem Description HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的 ...
- Enum service under CentOS7
service --status all systemctl list-unit-files
- MySQL分区和分表
一.概念 1.为什么要分表和分区?日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询 ...
- hive视图
简化复杂的查询 员工好.姓名.月薪.年薪.在一个emp表中; 部门名称在dept的表中;并未年薪起了一个名字annlsal 查询视图 视图是一个虚表,是不存数据的
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- pipreqs 组件
作用 帮助你查询所有需要用的组件 安装 pip install pipreqs 使用 """ 切换到项目的路径下 执行查询当前文件下所需要的所有的组件 会生成一个 r ...
- day11 filter函数
场景模拟:我想判断某个列表里面的某个元素怎么怎么样 基础方法,如果需要判断多次则重复代码 ret = [] move_peole = ["alex","sb_wupeiq ...
- python3.5 opencv3显示视频fps
由于要进行多路视频的处理,所以fps就很重要 fps介绍 模板: 1.获取某一时刻的fps import time while True: start_time = time.time() # sta ...
- MT【50】高中曲线系集大成之双切线法
[历史使人聪明,诗歌使人机智,数学使人精细,哲学使人深邃,道德使人严肃,逻辑与修辞使人善辩.--- Bacon,Francis] 练习: 评:这道2011高考题的解析做法参考答案也值得一看,但我这边在 ...
- .htaccess 配置
常规wordpress配置 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ ...