求助
  • 板块灌水区
  • 楼主Floating_Trees
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/2 12:48
  • 上次更新2024/5/2 16:22:47
查看原帖
求助
953181
Floating_Trees楼主2024/5/2 12:48

here

最后三个点 T 了,求大佬帮看一下,最好说下时间复杂度。

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'

using namespace std;

struct D
{
    int type;
    deque<int> d;
    D(int _type, deque<int> _d) : type(_type), d(_d)
    {
    }
};

deque<D> q;

void output()
{
    for (auto i : q)
        cout << i.d.front() << " ";
    cout << endl;
}
D merge(D a, D b)
{
    D ret = a;
    for (auto i : b.d)
        ret.d.push_back(i);
    return ret;
}
void upd()
{
    deque<D> t;
    while (!q.empty())
    {
        D f = q.front();
        f.d.pop_front();
        q.pop_front();
        if (!f.d.empty())
        {
            if (!t.empty() && f.type == t.back().type)
            {
                D x = merge(t.back(), f);
                t.pop_back();
                t.push_back(x);
            }
            else
            {
                t.push_back(f);
            }
        }
    }
    swap(t, q);
}

/*
void debug()
{
    for (deque<D>::iterator it = q.begin(); it != q.end(); ++it)
    {
        for (deque<int>::iterator i = (*it).d.begin(); i != (*it).d.end(); ++i)
            cout << (*i) << " ";
        cout << endl;
    }
}
*/

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, x, lst;
    //  int a,b; cin >> a >> b;
    //	cout << a + b << endl;
    cin >> n >> x;
    lst = x;
    deque<int> add;
    add.push_back(1);

    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        if (x == lst)
            add.push_back(i);
        else
            q.push_back(D(lst, add)), add.clear(), lst = x, add.push_back(i);
    }

    q.push_back(D(lst, add));

    while (!q.empty())
    {
        output();
        upd();
        // debug();
        // system("pause");
    }

    return 0;
}
2024/5/2 12:48
加载中...