zoukankan      html  css  js  c++  java
  • CodeForces

    该题code forces链接:http://codeforces.com/problemset/problem/631/C

    VJ暑期训练链接:https://vjudge.net/contest/238680#problem/H

    参考博客链接:https://blog.csdn.net/yhyyxt/article/details/50808441

    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 manager i + 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 integers ti 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.

    题意:当ti=1时把1-ri的进行升序,当ti=2时把1-ri的进行降序。

    思路:

    当满足(i>j)&&(ri>=rj)时则表明j之前的操作毫无意义。可以通过这个来得出哪些操作时有用的。并且得出的r[0]一定时最大的r。而且得出的有用操作的r[i]一定是从大—小的(这里还请读者自己思考一下)

    我们只需要对1—maxr内的数据进行排序,而(maxr+1)—(n)的数据则不需要动。

    这里需要复制一个数组b,并且把它1-maxri内的数据进行倒序。

    当t[i]==1时,可以知道是升序,那么只要把a的下标r[i+1]r[i]的部分对应b的后面部分;

    当t[i]==2时,是降序那么只要把a的下标r[i+1]—r[i]的部分对应b的前面部分

    下面是代码:

     1 #include<stdio.h>
     2 #include<algorithm>
     3 using namespace std;
     4 int a[200100];
     5 int b[200100];
     6 int t[200100];
     7 int r[200100];
     8 bool cmp(int a,int b)
     9 {
    10     return a>b;
    11 }
    12 int main()
    13 {
    14     int n,m;
    15     scanf("%d%d",&n,&m);
    16     for (int i=1;i<=n;i++)
    17     {
    18         scanf("%d",&a[i]);
    19         b[i]=a[i];
    20     }
    21     int len=0;
    22     for (int i=0;i<m;i++)
    23     {
    24         int t1,r1;
    25         scanf("%d%d",&t1,&r1);
    26         while(len&&r1>=r[len-1]) len--;//筛选有用的操作 
    27         t[len]=t1;r[len]=r1;len++;//筛选有用的操作 
    28     }
    29     sort(b+1,b+1+r[0]);//把b小-大排序 
    30     int left=1,right=r[0];
    31     r[len]=0;//这里是确保最后一次移动把a[1]位子也移动进去了。 
    32     for (int i=1;i<len+1;i++)
    33     {
    34         for (int j=r[i-1];j>r[i];j--)
    35         {
    36             if(t[i-1]==1)
    37             {
    38                 a[j]=b[right--];//赋后部分 
    39             }
    40             else
    41             {
    42                 a[j]=b[left++];//赋前部分 
    43             }
    44         }
    45     }
    46     for (int i=1;i<=n;i++)
    47     {
    48         if(i!=n)
    49         printf("%d ",a[i]);
    50         else
    51         printf("%d
    ",a[i]);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    java执行构造器和初始化字段的顺序
    java语言中的varargs
    对Java语言的byte类型变量进行无符号提升
    VisualStudio 切换帐号 (原帐号已过期且无法登录时用)
    C/C++ 的关系运算符采用短路运算
    实现std::string的ltrim、rtrim和trim方法
    Excel 用于批量把单元格设置为"文本格式保存的数字"的宏
    为什么要用webUI?
    CEF3中js调用delphi内部方法
    2016-1-1最新版本的linphone-android在mac上编译通过,同时建立了IDEA工程
  • 原文地址:https://www.cnblogs.com/bendandedaima/p/9341437.html
Copyright © 2011-2022 走看看