求助时间复杂度,我用list套list,内部list维护堆
#include <bits/stdc++.h>
using namespace std;
struct Fruit
{
int id;
char type;
};
int main()
{
int n;
cin >> n;
if (n<1) return 0;
list<list<Fruit>> l;
char preType = -1; //表示还没有开始
for (int i=1; i<=n; i++)
{
Fruit f; f.id = i;
scanf("%d",&(f.type));
if (preType != f.type) {
list<Fruit> ll;
l.push_back(ll);
}
preType = f.type;
l.back().push_back(f);
}
while (!l.empty())
{
list<list<Fruit>>::iterator it;
bool start = true;
for (it=l.begin(); it!=l.end(); it++)
{
list<Fruit> ll = *it;
//第一个前面不用空格
if (start) printf("%d",ll.front().id);
else printf(" %d",ll.front().id);
start = false;
ll.erase(ll.begin());
*it = ll;
}
//重新整理l,已经为空的要删除,删除后要判断是否前后合并
for (it=l.begin(); it!=l.end(); )
{
list<Fruit> ll = *it;
if (ll.empty())
{
//如果是第一个或者是最后一个,那么直接移除,不用合并
if (it==l.begin() || next(it)==l.end())
it = l.erase(it) ;
else
{
//判断it前后节点是否水果种类一样,若一样,则合并
list<list<Fruit>>::iterator itPre = prev(it);
it = l.erase(it);
if (itPre->front().type == it->front().type)
{
itPre->insert(itPre->end(), it->begin(), it->end());
it= l.erase(it); //这里要把it指向下一个
}
}
}
else it++;
}
cout << endl;
}
return 0;
}