51Nod1774 多重排序

Problem

有一个数组a,长度为n,下标从1开始。现在要对a进行m次排序,每一次排序给定两个参数t[i],r[i]表示要对数组的前r[i]个元素进行排序,如果t[i]=1则按照非降序排序,t[i]=2则按照非升序排序。

请输出经过m次排序之后的数组a。

样例解释:

第一个样例中,初始序列为:1 2 3。经过第一次排序之后变成了:2 1 3。

第二个样例中,初始序列为:1 2 4 3。经过第一次排序之后变成了:4 2 1 3。经过第二次排序之后变成了:2 4 1 3。

Solution

搞一个r单调递减的栈,从最大的r向前赋值,t=1,赋最大的值,否则赋最小的值。

搞stdio.h居然效率最高,欢迎吊打QAQ

Code

#include<stdio.h>
#include<set>
//#include<iostream>
#include<algorithm>
typedef long long ll;
typedef long double ld;
typedef double db;
//#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
//using namespace std;
int n,m;
int a[200020],tmp[200020];
struct E{
    int t,r;
}e[200020];
E x;
int top=0,head,tail;
int main() {
    //io_opt;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x.t,&x.r);
        if(top&&x.r>=e[top].r){
            while(top>0&&x.r>=e[top].r){
                top--;
            }
        }
        e[++top]=x;
    }
    head=1,tail=e[1].r;
    for(int i=1;i<=e[1].r;i++){
        tmp[i]=a[i];
    }
    std::sort(tmp+1,tmp+1+tail);
    e[top+1].r=0;
    for(int i=1;i<=top;i++){
        if(e[i].t==1){
            for(int j=e[i].r;j>e[i+1].r;j--){
                a[j]=tmp[tail];
                tail--;
            }
        }
        else{
            for(int j=e[i].r;j>e[i+1].r;j--){
                a[j]=tmp[head];
                head++;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d ",a[i]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sz-wcc/p/11727017.html