贪心+构造( Codeforces Round #344 (Div. 2))

题目:Report

题意:有两种操作:

        1)t = 1,前r个数字按升序排列;
          2)t = 2,前r个数字按降序排列;

       求执行m次操作后的排列顺序。

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>

#define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
using namespace std;
#define N 200005
const int M = 1e9 +7;
typedef long long LL;
int a[N];
struct manager{
    int t,r;
}ma[N];
int b[N];
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n;i++){
        scanf("%d",a+i);
    }
    int k = 0,x,y;
    while (m--){
        scanf("%d%d", &x, &y);
        while(k > 0 && ma[k-1].r <= y) k--;
        if(k > 0 && x == ma[k-1].t) continue;
        ma[k].t = x;
        ma[k].r = y;
        k++;
    }
    ma[k].t = 1;
    ma[k].r = 0;
    for(int i = 0; i < ma[0].r; i++)
        b[i] = a[i];
    sort(b, b+ma[0].r);
    int l = 0,r = ma[0].r;
    int p = r;
    for(int i = 0; i < k; i++){
        //printf("%5d%5d 
",ma[i].t,ma[i].r);
        while(p > ma[i+1].r){
            p--;
            if(ma[i].t == 1) a[p] = b[--r];
            else if(ma[i].t == 2)a[p] = b[l++];
        }
    }
    for(int i = 0;i < n;i++){
        printf("%d ",a[i]);
    }
    printf("
");
    return 0;
}
原文地址:https://www.cnblogs.com/yoyo-sincerely/p/5259506.html