CodeForces - 631C

Posted bendandedaima

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 631C相关的知识,希望对你有一定的参考价值。

该题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 }

 






以上是关于CodeForces - 631C的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 631C Report

Report CodeForces - 631C (单调栈)

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)