codeforce469DIV2——C. Zebras

时间:2023-03-09 01:48:12
codeforce469DIV2——C. Zebras

题意

0, 010, 01010 这一类的01交替且开头和结尾都为0的序列被称为zebra序列。给出一段01序列,尝试能否把他分为k个子序列使得每个子序列都是zebra序列。

分析

这个题应该算是水题把,但是确实把我卡了。

一开始暴力找在序列中找1,然后分别往前往后各找一个0。但是最坏情况到O(n^2),TLE在第八组。然后我尝试了各种Set,vector,二分之类的瞎搞结果一直都没把复杂度降下来一直T在第八组绝望了。

赛后看其他人的代码发现··可以O(n)扫一遍,当s[i]是0的时候找加入最后一个是1的方案,若是1则加入最后一个是0的方案。如果没有合适的方案,就新开一个。可以用优先队列或者set搞

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn=+;
char s[maxn];
priority_queue<int,vector<int>,greater<int> >q0,q1;
vector<int>Q[maxn];
int n,p;
int main(){
scanf("%s",s+);
n=strlen(s+);
p=;
bool ok=;
for(int i=;i<=n;i++){
if(s[i]==''){
if(q0.empty()){
ok=;
break;
}
int tmp=q0.top();
q0.pop();
Q[tmp].push_back(i);
q1.push(tmp);
}else{
if(q1.empty()){
p++;
Q[p].push_back(i);
q0.push(p);
}else{
int tmp=q1.top();
q1.pop();
Q[tmp].push_back(i);
q0.push(tmp);
}
}
}
if(!ok||!q1.empty()){
printf("-1");
return ;
}
printf("%d\n",p);
for(int i=;i<=p;i++){
printf("%d ",Q[i].size());
for(int j=;j<Q[i].size();j++)
printf("%d ",Q[i][j]);
printf("\n");
}
return ;
}