C. Report

题意:给出n个无序的数以及m个操作,每个操作由两个数组成,第一个数是操作的方式,第二个数 i 是操作的范围,若第一个数是1,则给 1-i 个数按升序排序,若第二个数是2,则给 1-i 个数按降序排列。输出所有操作完成后的序列。

int n, m;
arr a, ans;
int top = 0;
int tot = 0;
struct node
{
    int r, tp, id;
} p[N], q[N];
bool cmp(node a, node b)
{
    if (a.r == b.r)
        return a.id > b.id;
    return a.r > b.r;
}

int main()
{
    // file("test");
    sdf(n), sdf(m);
    For(i, 1, n) sdf(a[i]);
    For(i, 1, m)
    {
        int x, y;
        sdf(x), sdf(y);
        p[i] = (node){y, x, i};
    }
    sort(p + 1, p + 1 + m, cmp);
    int st = 1;
    For(i, 1, m)
    {
        if (p[i].id < st)
            continue;
        st = p[i].id;
        if (i >= 2 && p[i].tp == q[tot].tp)
            continue;
        q[++tot] = p[i];
        if (p[i].id == m)
            break;
    }
    top = n+1;
    FFor(i, n, q[1].r + 1)
        ans[--top] = a[i];
    sort(a + 1, a + 1 + q[1].r);
    int L = 1, R = q[1].r;

    For(i, 1, tot)
    {
        int dt = q[i].r - q[i + 1].r;
        if (q[i].tp == 1)
        {
            FFor(j, R, R-dt+1)
                ans[--top] = a[j];
            R -= dt;
        }
        else
        {
            For(j, L, L + dt - 1)
                ans[--top] = a[j];
            L += dt;
        }
    }
    For(i,1,n)
        printf("%d ", ans[i]);

    return 0;
}
原文地址:https://www.cnblogs.com/planche/p/9434231.html