Codeforces Round #469 (Div. 2) C. Zebras

题目链接:点击打开链接

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

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 001001010 are zebras, while sequences 101100101 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|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li 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
Copy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
input
Copy
111
output
-1

题意:给你一个只含01的串。定义,0,010,01010 ····为斑马串(必须以0开头,以0结束)。然后从给定的字符串中构造斑马串,问能否构造。能的话输出构造结果,不能的话输出-1.构造出来的斑马串用的数字的位置 必须单调递增。

题解:用vector模拟。遇到0就加到1后面,遇到1就放到0后面,若1没有0可放输出-1。但是会时间超限。n方的时间复杂度过不去。需要优化。分析题意可知每次放数字,只需要知道放在那个正在构造的斑马串后。所以可以用两个队列来记录当前0或1,应该放在那个斑马串后面。网上题解有一种骚操作。但是我没明白啥意思。但是我感觉我得操作最骚 嘿嘿嘿。

具体实现过程看代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
char s[200005];
int a[200005];
vector<int> v[200005]; // 模拟斑马串 
queue<int>q; // 存1的位置的队列 
queue<int>q0; // 存0的位置的队列 
int main(){
	for(int i = 0 ; i <= 200005 ; i ++)
		v[i].clear();
	while(!q.empty()){
		q.pop();
	}
	while(!q0.empty()){
		q0.pop();
	}
	scanf("%s",s);
	int len = strlen(s);
	if(s[0] == '1' || s[len-1] == '1'){  // 若第一个或最后一个为1,那么必定无法构造。 
		cout << "-1" << endl;
		return 0;
	}
	for(int i = 0 ; i < len ; i ++)
		a[i] = s[i] - '0';
	int ans = 1;
	v[0].push_back(0);
	q0.push(0);
	for(int i = 1; i < len ; i ++){
		if(a[i] == 0){
			if(q.empty()){ // 没有1可放。那么另起一行  ,然后记录0的位置 
				q0.push(ans);
				v[ans ++].push_back(i);
				
			}
			else { // 否者就放到1后面,然后记录0的位置 在那个斑马串后面 
				v[q.front()].push_back(i);
				q0.push(q.front());
				q.pop();
			}
		}
		else {
			if(q0.empty()){ // 若没有0可放 就输出-1 
				cout << "-1" << endl;
				return 0;
			}
			else { // 若有 就放到0后面,然后记录1的位置在那个斑马串后面 
				v[q0.front()].push_back(i);
				q.push(q0.front());
				q0.pop();
			}
		}
	}
		for(int i = 0 ; i < ans ; i ++){ // 判断是否都是斑马串 
			int end = v[i].size();
			if(a[v[i][end-1]])
			{
				cout << -1 << endl;
				return 0;
				}	
 		}
		//cout << " GG" << endl;
		printf("%d\n",ans);
		int sum = 0 ;
		for(int i = 0 ; i < ans ; i ++){
			int num = v[i].size();
			printf("%d ",num);
			for(int j = 0 ; j < num ; j ++){
				printf("%d ",v[i][j]+1);
			}
			cout << endl;
		}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值