• CodeForces 631C Report


    题目链接:http://codeforces.com/problemset/problem/631/C

    C. Report
    time limit per test: 
    2 seconds
    memory limit per test: 
    256 megabytes

    Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first ri numbers in non-descending or non-ascending order and then passes the report to the manageri + 1, or directly to Blake (if this manager has number i = m).

    Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence ai of length n and the description of each manager, that is value ri and his favourite order. You are asked to speed up the process and determine how the final report will look like.

    Input

    The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.

    The second line contains n integers ai (|ai| ≤ 109) — the initial report before it gets to the first manager.

    Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integersti and ri (1 ≤ ri ≤ n), meaning that the i-th manager sorts the first ri numbers either in the non-descending (if ti = 1) or non-ascending (if ti = 2) order.

    Output

    Print n integers — the final report, which will be passed to Blake by manager number m.

    Examples
    input
    3 1
    1 2 3
    2 2
    output
    2 1 3 
    input
    4 2
    1 2 4 3
    2 3
    1 2
    output
    2 4 1 3 
    Note

    In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.

    In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.

    题意:

    给出一个n和m,表示有n个产品和m个经理。

    然后给出初始的时候的数组,表示每个产品的收益。

    然后是m行,每行表示一个经理对数组的操作。

    1 ri表示对前ri个数按非降序排列。

    2 ri表示对前ri个数按非升序排列。

    要求经过m个经理的操作后,最终的数组。

    思路:

    可以马上发现如果第i个操作的长度>前面所有的操作,那么前面所有的操作都是无效的。

    所以先找出一个经理的操作序列,是ri递减的。

    然后在区间[r[i]+1, r[i-1]]上的数字,事实上只被处理出来的第i次操作所影响。

    所以可以先把初始数组a复制到b数组,然后把初始数组a从小到大排序。

    设置两个指针分别表示当前未用的最小数和当前没有使用的最大数。

    然后处理出来的操作序列分配这些数就可以了。

    找ri递减的操作序列的时候用了线段树,感觉自己像个智障。。。不过懒得改了。。。

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 #define lson l, m, rt<<1
      4 #define rson m+1, r, rt<<1|1
      5 int n, M;
      6 #define maxn 200010
      7 bool cmpx(int a, int b){return a<b;}
      8 bool cmpd(int a, int b){return a>b;}
      9 int a[maxn],b[maxn];
     10 int op[maxn], c[maxn];
     11 int co;
     12 struct Node
     13 {
     14     int val, pos;
     15     Node(){}
     16 }node[maxn<<2];
     17 vector <int> v;
     18 void PushUp(int rt)
     19 {
     20     if(node[rt<<1].val >= node[rt<<1|1].val)
     21     {
     22         node[rt].val = node[rt<<1].val;
     23         node[rt].pos = node[rt<<1].pos;
     24     }
     25     else
     26     {
     27         node[rt].val = node[rt<<1|1].val;
     28         node[rt].pos = node[rt<<1|1].pos;
     29     }
     30 }
     31 void build(int l, int r, int rt)
     32 {
     33     if(l == r)
     34     {
     35         node[rt].val = c[co];
     36         node[rt].pos = co;
     37         co++;
     38         return;
     39     }
     40     int m = (l+r)>>1;
     41     build(lson);
     42     build(rson);
     43     PushUp(rt);
     44 }
     45 Node query(int L, int R, int l, int r, int rt)
     46 {
     47     if(L <= l && R >= r)
     48     {
     49         return node[rt];
     50     }
     51     int m = (l+r)>>1;
     52     Node ret; ret.val = 0; 
     53     if(L <= m) ret = query(L, R, lson); 
     54     if(R > m)
     55     {
     56         Node ret2 = query(L, R, rson);
     57         if(ret2.val > ret.val) return ret2;
     58     }
     59     return ret;
     60 }
     61 int main() 
     62 {
     63    // freopen("in.txt", "r", stdin);
     64     while(~scanf("%d%d", &n, &M))
     65     {
     66         for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
     67         for(int i = 1; i <= M; i++) scanf("%d%d", &op[i], &c[i]);
     68         v.clear();
     69         co = 1;
     70         build(1, M, 1);
     71         Node x = query(1, M, 1, M, 1);
     72         int nowpos;
     73         nowpos = x.pos;
     74         v.push_back(nowpos);
     75         while(nowpos < M)
     76         {
     77             Node x = query(nowpos+1, M, 1, M, 1);
     78             nowpos = x.pos;
     79             v.push_back(nowpos);
     80         }
     81         for(int i = 1; i <= n; i++) b[i] = a[i];
     82         sort(a+1, a+1+c[v[0]], cmpx); 
     83         
     84         int l = 1, r = c[v[0]];      
     85         for(int i = 1; i < v.size(); i++)
     86         {
     87             for(int j = c[v[i-1]]; j > c[v[i]]; j--)
     88             {
     89                 if(op[v[i-1]] == 1)  b[j] = a[r--];
     90                 else if(op[v[i-1]] == 2) b[j] = a[l++];
     91             }
     92         }
     93         for(int j = c[v[v.size()-1]]; j >= 1; j--)
     94         {
     95             if(op[v[v.size()-1]] == 1) b[j] = a[r--];
     96             else b[j] = a[l++];
     97         }
     98         
     99         for(int i = 1; i <= n; i++) 
    100         {
    101             if(i == 1) printf("%d", b[i]);
    102             else printf(" %d", b[i]);
    103         }
    104         printf("
    ");
    105     }
    106     return 0;
    107 }
  • 相关阅读:
    实验一
    BZOJ 2564
    P4557 [JSOI2018]战争
    移动自动化-Mac-IOS-appium环境搭建
    Node安装mac版本
    删除N天前文件和空文件
    Python之jsonpath模块
    性能学习
    参数化
    查找测试用例
  • 原文地址:https://www.cnblogs.com/titicia/p/5243041.html
Copyright © 2020-2023  润新知