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的更多相关文章

  1. Codeforces 950 D. A Leapfrog in the Array

    http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...

  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 ...

  3. Codeforces 950 010子序列拆分 数列跳棋

    A B a,b两个序列分成K段 每一段的值相等 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset ...

  4. Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)

    C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...

  5. CodeForces - 950C Zebras 模拟变脑洞的天秀代码

    题意:给你一个01串,问其是否能拆成若干形如0101010的子串,若能,输出所有子串的0,1 的位置. 题解:一开是暴力,然后瞎找规律, 最后找到一种神奇的线性构造法:扫一遍字符串,若为0就一直竖着往 ...

  6. 【codeforces】【比赛题解】#950 CF Round #469 (Div. 2)

    剧毒比赛,至少涨了分对吧.: ( [A]Left-handers, Right-handers and Ambidexters 题意: 有\(l\)个右撇子,\(r\)个左撇子,\(a\)个双手都惯用 ...

  7. Zebras CodeForces - 950C(思维)

    借鉴自: https://www.cnblogs.com/SuuT/p/8619227.html https://blog.csdn.net/my_sunshine26/article/details ...

  8. Codeforces 950C Zebras ( 贪心 && 模拟 )

    题意 : 给出一个 01 串,要求你将其分隔出若干个子序列 ( 每个数字只能属于某一个子序列 ) ,子序列必须满足由 0 开头和结尾,且中间需 01 交替构成.若无法做到,则输出 -1. 分析 :  ...

  9. Data Center Maintenance CodeForces - 950E

    http://codeforces.com/contest/950/problem/E 贴一份板子 #include<cstdio> #include<vector> #inc ...

随机推荐

  1. Maven-Build Lifecycle(构建生命周期)

    https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html https://www.w3cschoo ...

  2. DVWA的安装与简单使用

    参考资料: http://www.freebuf.com/articles/web/119150.html 尝试使用linux机器安装,但是因为下载php版本以及各种兼容性的问题耗时较长, 所以后来选 ...

  3. k8s 1.9 安装

    测试环境 主机 系统 master CentOS 7.3 node CentOS 7.3 2.关闭selinux(所有节点都执行) [root@matser ~]# getenforce Disabl ...

  4. GlusterFS 增删节点及改变复制份数

    一.增加节点 1.需要主机添加到主机池中 gluster peer  probe server3 gluster peer  probe server4 2.查看状态 3.添加节点并复制2份(增加复制 ...

  5. PostgreSQL之Sequence序列(转)

    本文转载自:https://blog.csdn.net/omelon1/article/details/78798961 Sequence序列 Sequence是一种自动增加的数字序列,一般作为行或者 ...

  6. JavaScript & Dom 之 基本语法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Dapper 连表查询

    实体类: UserInfo: public partial class UserInfo { public UserInfo() { this.Persion = new HashSet<Per ...

  8. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

  9. Python网络爬虫:空姐网、糗百、xxx结果图与源码

    如前面所述,我们上手写了空姐网爬虫,糗百爬虫,先放一下传送门: Python网络爬虫requests.bs4爬取空姐网图片Python爬虫框架Scrapy之爬取糗事百科大量段子数据Python爬虫框架 ...

  10. 自学Zabbix4.2.1 Application介绍

    自学Zabbix4.2.1 Application介绍 Applications应用程序是item的一个组.例如我们要监控MySQL,我们可以将所有和MySQL相关的item放到这个应用程序中.例如M ...