CodeForces - 631C

本文解析了CodeForces 631C题目的算法思路,通过筛选有效操作并采用特定排序策略,实现了对经济报告数据的快速整理。文章详细介绍了如何根据经理的操作指示对数据进行升序或降序排列,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

参考博客链接:https://blog.youkuaiyun.com/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\n",a[i]);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/bendandedaima/p/9341437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值