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.
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.
Print n integers — the final report, which will be passed to Blake by manager number m.
3 1 1 2 3 2 2
2 1 3
4 2 1 2 4 3 2 3 1 2
2 4 1 3
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个操作:
①1 x,表示让前x个数按照从小到大排序。
②2 x,表示让前x个数按照从大到小排序。
思路:
1、首先我们不难想到,如果有这样顺序的两个操作:
1 5
2 6
或者是这样的两个操作:
2 7
1 8
亦或者是这样的两个操作:
1 5
2 6
很明显,三种顺序的操作中,第一个操作都是没有必要的操作,那么对应我们肯定最终要得到一个按照x从大到小递减的一些关键操作,非关键操作都是可以省略掉不关注的。
那么我们将输入进来的m个操作,按照x从大到小排序,截取出来关键操作.
2、假设原序列为:
3 4 5 6 7 1 2
然后我们截取出来了三个操作:
2 6
1 4
2 2
按照步骤我们手写出来过程:
3 4 5 6 7 1 2
7 6 5 4 3 1|2
4 5 6 7|3 1|2
5 4|6 7|3 1|2
不难发现,对于答案的确定,我们对于第一次操作就能已知会改变的区间.按照上述例子就知道【1,6】是会改变的【7,7】是不会改变的,那么我们将【1,6】区间内的数按照从小到大排序,放到一个双端队列中。
接下来对于操作1,我们从队尾(从大到小)每次取一个数放到最末尾,一共放几个数呢?我们根据本次操作和下一次操作的区间长度差决定。
接下来对于操作2,我们从队头(从小到大)每次取一个数放到最末尾,同理,放的数的个数根据本次操作和下一次操作的区间长度差决定。
本部分时间复杂度O(n);
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<deque>
using namespace std;
struct node
{
int op,x,pos;
}b[200600];
int a[200600];
int ans[200600];
int p[200600][2];
int cmp(node a,node b)
{
if(a.x==b.x)return a.pos<b.pos;
else
return a.x>b.x;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int cnt=0;
int fop,fx;
for(int i=0;i<m;i++)
{
int op,x;
scanf("%d%d",&b[i].op,&b[i].x);
b[i].pos=i;
}
sort(b,b+m,cmp);
p[cnt][0]=b[0].op;
p[cnt++][1]=b[0].x;
int prepos=b[0].pos;
for(int i=1;i<m;i++)
{
if(b[i].pos>prepos)
{
prepos=b[i].pos;
p[cnt][0]=b[i].op;
p[cnt][1]=b[i].x;
cnt++;
}
}
deque<int >s;
sort(a+1,a+p[0][1]+1);
for(int i=p[0][1]+1;i<=n;i++)ans[i]=a[i];
for(int i=1;i<=p[0][1];i++)s.push_back(a[i]);
p[n][1]=0;
for(int i=0;i<n;i++)
{
if(p[i][0]==2)
{
int tt=p[i][1]-p[i+1][1];
int pos=p[i][1];
while(tt--)
{
if(s.size()==0)break;
ans[pos]=s.front();
s.pop_front();
pos--;
}
}
else
{
int tt=p[i][1]-p[i+1][1];
int pos=p[i][1];
while(tt--)
{
if(s.size()==0)break;
ans[pos]=s.back();
s.pop_back();
pos--;
}
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
}
本文介绍了一种高效的算法,用于处理一系列对商品收入报告进行排序的操作,通过优化排序过程,确保最终报告正确且快速地生成。
626

被折叠的 条评论
为什么被折叠?



