(看起来似乎是正解,提交却超时QWQ)
思路:外面一层队列存放块,里面再用队列存水果
大概看了看题解……似乎差不多呀……?
#include<iostream>
#include<fstream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct kkk
{
queue<int> a;//水果
queue<int> b;//水果ID
int last;//最后一个水果
}w;
void clear(queue<int>& q)
{
queue<int> empty;
swap(empty, q);
}//清空队列
int n,v,tt;
queue<kkk> q;//块
void defi()
{
clear(w.a);
clear(w.b);
w.last=0;
}//清空w(这里应该会有更好的方法……奈何我太菜了)
int main()
{
scanf("%d",&n);
defi();
scanf("%d",&v);
w.a.push(v);
w.b.push(1);
w.last=v;
for(int i=2;i<=n;i++)
{
scanf("%d",&v);
if(v==w.last)
{
w.a.push(v);
w.b.push(i);
w.last=v;
}
else
{
q.push(w);
defi();
w.a.push(v);
w.b.push(i);
w.last=v;
}
}
q.push(w);
kkk tail;
tail.last=555;
q.push(tail);//水果链的结尾(用于换行
tt=-1;
while(q.size()>1)
{
kkk s=q.front();
q.pop();
if(s.last==555)//换行
{
printf("\n");
tt=5;
q.push(s);
continue;
}
if(s.a.front()!=tt)//与前面的块不能合并
{
printf("%d ",s.b.front());
tt=s.a.back();
s.a.pop();
s.b.pop();
}
if(s.a.empty()==false) q.push(s);//这个块水果拿完了
}
return 0;
}
//似乎是O(n)做法……为啥超时TAT