题目链接:https://codeforces.com/problemset/problem/950/C
博客园食用链接:https://www.cnblogs.com/lonely-wind-/p/13453216.html

Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.

Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg’s life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.

Input
In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg’s life. Its length (denoted as |s|) does not exceed 200 000 characters.

Output
If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k(1≤k≤∣s∣)k (1 ≤ k ≤ |s|)k(1 ≤ k ≤ ∣s∣), the resulting number of subsequences. In the i-th of following k lines first print the integer li(1≤li≤∣s∣)l _i (1 ≤ l _i ≤ |s|)li​(1 ≤ li​ ≤ ∣s∣), which is the length of the i-th subsequence, and then l i indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples
Input
0010100
Output
3
3 1 3 4
3 2 5 6
1 7

Input
111
Output
-1

题目大意:给你一个序列,问你能否将其拆分为若干个斑马序列,若能请输出其拆分方法,否则输出-1,斑马序列的定义如下:开头是0,结尾是0,整个序列是01交替的,当然单个的0也是合法的。

emmm,不要想太多了,直接按照题目要求的来构造就行了,我们知道,1一定要放在0后面的,0也一定要放在1后面的(如果有1的话),然后多出来的0就单独成一段。

那么就可以用vector<int>g[maxn]vector<int>g[maxn]vector<int>g[maxn]来保存序列,假设现在的序列个数已经跟新到了第cntcntcnt个,那么每次出现0的时候我们查询是否在g[1−cnt]g[1-cnt]g[1−cnt]中是否有以1为结尾的,如果有我们就直接接上去,如果没有就单独开创,同时cnt++cnt++cnt++,如果出现的是1,那么我们出现之前是否有以0为结尾的,如果有就接上去,没有就直接-1了。

现在有个问题就是怎么快速地查询这些以01为结尾的编号,如果for一遍的话时间复杂度有点高,所以我们可以用两个队列来保存这些编号,每次都将队首给弹出去就好了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;const int mac=2e5+10;char s[mac];
vector<int>g[mac];
queue<int>tail1,tail0;int main(int argc, char const *argv[])
{scanf ("%s",s);int len=strlen(s);int one=0,zero=0;for (int i=0; i<len; i++)if (s[i]=='0') zero++;else one++;if (one>=zero) {printf("-1\n"); return 0;}int cnt=0,mk=0;for (int i=0; i<len; i++){if (s[i]=='1' && tail0.empty()) {mk=1; break;}else if (s[i]=='1') {int v=tail0.front();tail0.pop();g[v].push_back(i+1);tail1.push(v);}else if (s[i]=='0'){if (tail1.empty()) {g[++cnt].push_back(i+1); tail0.push(cnt);continue;}int v=tail1.front();tail1.pop();g[v].push_back(i+1);tail0.push(v);}}for (int i=1; i<=cnt; i++)if (s[g[i][g[i].size()-1]-1]=='1') {mk=1; break;}if (mk) {printf("-1\n"); return 0;};printf("%d\n",cnt);for (int i=1; i<=cnt; i++){printf("%d",g[i].size());for (auto x:g[i])printf(" %d",x);printf("\n");}return 0;
}

Codeforces 950C-Zebras(模拟构造)相关推荐

  1. Codeforces Round #743 (Div. 2) D. Xor of 3 模拟 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你一个010101序列aaa,定义一次操作是选择一个[1,n−2][1,n-2][1,n−2]范围内的下表,将ai,ai+1,ai+2a_i,a_{i+1} ...

  2. Codeforces C1. Prefix Flip (Easy Version) (二进制串 / 模拟 / 构造) (Roun #658 Div.2)

    传送门 题意: 给出两个长度为n的二进制串a和b,你每次可选取一段前缀子串取反并翻转(即:10010 -> 01101 -> 10110).已知在3 * n次操作内一定能将a变成b.先让你 ...

  3. CodeForces - 287C Lucky Permutation(构造)

    题目链接:点击查看 题目大意:构造一个合法的排列,满足 ppi=n−i+1p_{p_{i}}=n-i+1ppi​​=n−i+1 题目分析:因为第四个样例的存在降低了本题的难度,不然感觉还是有点难度的一 ...

  4. CodeForces - 468C Hack it!(构造+数位dp)

    题目链接:点击查看 题目大意:求出一段区间 [l,r][l,r][l,r] 的数位和对 aaa 取模后为 000.更具体的,设 f(x)f(x)f(x) 为 xxx 的数位和,本题需要求出一对 [l, ...

  5. CodeForces - 1561E Bottom-Tier Reversals(构造)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的排列,每次操作可以选择一个奇数长度的前缀然后反转,需要构造一种方案,使得在不超过 5n2\frac{5n}{2}25n​ 次操作后使得序列有序 ...

  6. CodeForces - 1491E Fib-tree(模拟)

    题目链接:点击查看 题目大意:给出一棵树,问是否为斐波那契树.斐波那契树的定义是,树的大小为斐波那契数列的其中一项,且可以通过删除掉一条边使其拆分的两个子树也为斐波那契树 题目分析:需要观察到,大小为 ...

  7. CodeForces - 1494E A-Z Graph(构造+思维)

    题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...

  8. CodeForces - 1494D Dogeforces(贪心+构造)

    题目链接:点击查看 题目大意:给出 nnn 个叶子结点和一个 n∗nn*nn∗n 的 LCALCALCA 矩阵,其中 LCALCALCA 表示的是最近公共祖先节点的权值,现在需要构造出一棵自顶向下权值 ...

  9. CodeForces - 1110G Tree-Tac-Toe(博弈+构造)

    题目链接:点击查看 题目大意:给出一棵树状棋盘,棋盘上初始时可能为空也可能为白色,小黑和小白轮流操作,每次操作小黑可以选择一个空位置染成黑色,小白可以选择一个空位置染成白色,胜利规则和五子棋类似,有三 ...

  10. CodeForces - 148C Terse princess (构造)

    题目链接:http://codeforces.com/problemset/problem/148/C点击打开链接 C. Terse princess time limit per test 1 se ...

最新文章

  1. Computer OS系统基本原理
  2. mysql查询2020年之前_2020年成人高考成绩如何查询?2020年成考录取结果如何查询?...
  3. 惠普打印信息页无法连接到服务器,惠普M400系列打印机网络连接无法打印怎么办?...
  4. Java实用面试题及参考答案分享
  5. [翻译:更新]Understanding Linux Network Internals - Table of Contents
  6. zemax微透镜阵列示例_阵列反向! Ruby中的示例方法
  7. bzoj1084 [SCOI2005]最大子矩阵 dp
  8. 【JVM】三色标记法与读写屏障
  9. ​我们为何需要更安全的系统编程语言?
  10. Qt Creator 2.8.1,qt4.8.5 需要含gcc4.4 的mingw
  11. 机器学习之支持向量机算法(二)
  12. 计算机论文的主要研究方法有哪些,9大实用的论文研究方法盘点
  13. peopleSoft常见错误诊断
  14. ORACLE中函数last_day()的用法
  15. Newoupui-pak配置失败怎么处理?
  16. 中国科学院计算机专业职称,中国科学院关于高级工程师职务分级的意见
  17. 两种方式实现线程通信:三个线程交替打印AABBCC
  18. 蓝牙与UWB的技术对比
  19. vue element-ui中有关表格中的数据整条显示红色/绿色等等颜色的问题
  20. final修饰的变量就是常量?final修饰局部变量在栈还是堆还是常量池中?

热门文章

  1. iOS游戏开发之Game Center研究
  2. backdrop-filter,让你的网站熠熠生”毛’
  3. 数据库复杂查询,左联右联 聚合 计数 时间查询等,持续更新
  4. WPS 手动去除广告
  5. dplyr包 mutate 和 transmute 函数
  6. CAN bus的移植
  7. 音频处理贤内助--libsndfile
  8. 38 Power Query-背后的贤内助 M 语言
  9. 家用路由器改造成交换机教程
  10. 小熊派BearPi-HM nano开发板 -- MobaXterm使用详情、VScode连接编译主机、小熊派源码获取及烧录