题目:

  

  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 

题意:

  给你一个序列,对它进行操作1,2,1:把前i个数字非降序排列。2:把前i个数非升序排列。输出排列后的序列。

题目分析:

  首先,暴力肯定是妥妥的t掉的:每次都是nlogn,m次,显然不能解决这个问题,那我们能进行什么优化呢。

  考虑一下有什么无用的操作:举个例子:如果我对前5个数进行了某种排序,再对前10个数进行某种排序,前5个排序的操作就可以直接忽略掉了。这个应该很容易想到吧。于是,我们就可以维护一个类似单调队列的数据结构:如果某个操作比另一个操作晚出现,而且排的数比早的排的数要多,前面那一个操作就可以忽略掉了。最后,我们会得到这样一些操作:

  第i个操作为对前ai个数进行某种排序,且a1>a2>a3>...>an。

  这样的话,我们继续贪心:进行完第i个操作之后,我们就可以确定ai+1+1到ai的所有数了,于是,我们通过走一遍维护的“单调队列”,就可确定每一个数了。然后就是注意一下细节就好了。

代码:

 #include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=+;
int a[maxn];
int b[maxn];
int que[maxn];
int c[maxn];
int End;//最好大写首字母
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int js1,js2;
for(int i=;i<=m;i++){
scanf("%d%d",&js1,&js2);
while(End&&que[End]<=js2)
End--;
End++;
que[End]=js2;
c[End]=js1;
}
sort(a+,a++que[]);
for(int i=que[]+;i<=n;i++)//把没有改变过的直接赋值
b[i]=a[i];
int left=,right=que[];
que[End+]=;//保证跑完后所有的数都被处理了
for(int i=;i<=End;i++){
if(c[i]==){
for(int j=left,j1=que[i];j<=left+que[i]-que[i+]-;j++,j1--)
b[j1]=a[j];
left+=que[i]-que[i+];
}
else{
for(int j=right,j1=que[i];j>=right+que[i+]-que[i]-;j--,j1--)
b[j1]=a[j];
right-=que[i]-que[i+];
}
}
for(int i=;i<=n;i++)
printf("%d ",b[i]);
return ;
}

  

Report,又是一道思维题的更多相关文章

  1. hdu2094—看似拓扑实际上是一道思维题

    HDU2094  产生冠军 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2094 题意:中文题,就不解释了.题意已经非常清楚了. 这道题的看起来像是一 ...

  2. poj1986 Distance Queries(lca又是一道模版题)

    题目链接:http://poj.org/problem?id=1986 题意:就是老问题求val[u]+val[v]-2*val[root]就行.还有这题没有给出不联通怎么输出那么题目给出的数据一定 ...

  3. 又是一道水题 hdu背包

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  4. Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)

    524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...

  5. Problem C Emergency Evacuation 一道思维题

    题目描述 输入 输出 样例 样例输入 样例输入一 样例输入二 样例输出 样例输出一 9 样例输出二 1008 一句话题意:给你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值. 分析 ...

  6. 一道思维题 &&递归改循环

    思路: 比如5 2 12345--> 1245 从3开始,这时候5变成了1.剩下4512,对应1234.只需要找到现在n-1,k中的数对应原来的编号的映射. 比如1-->3 是1+2 mo ...

  7. CodeForces - 631C ——(思维题)

    Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...

  8. BZOJ4401: 块的计数 思维题

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...

  9. CodeForces - 1102A(思维题)

    https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...

随机推荐

  1. linux下git相关命令

    请参照以下文章:https://www.cnblogs.com/pengtangtang/articles/PengTangTang_git_one.html

  2. Maven发布Release到中心仓库历程记录(无个人域名)

    Maven发布Release到中心仓库历程记录(无个人域名) 前言 因为前段时间自己做了一个爬虫项目(地址),自己很希望分享到maven中心仓库上,感觉拥有自己的jar包令我兴奋,便开始了maven发 ...

  3. hadoop启动后,9000端口无法连接,netstat -tpnl中找不到该端口

    已解决: 需要重新格式化hdfs. 1.停止hdfs: 2.删除hdfs的相关文件目录(hdfs-site.xml中配置的存放文件的目录). 3.启动journalnode:sbin/hadoop-d ...

  4. tensorflow2.0学习笔记第二章第三节

    2.3激活函数sigmoid函数 f(x)= 1/(1 + e^-x)tf.nn.sigmoid(x)特点:(1)求导后的数值在0-0.25之间,链式相乘之后容易使得值趋近于0,形成梯度消失 (2)输 ...

  5. 96题--不同的二叉搜索树(java、中等难度)

    题目描述:给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例如下: 分析:本题可用动态规划的方法求解. 设 dp[n] 表示以 1 ... n 为节点组成的二叉搜索树的种类 ...

  6. Redis学习笔记(十六) Sentinel(哨兵)(下)

    消失了一段时间,我又回来啦.不多说,继续把哨兵看完. 检测主观下线状态 默认情况下,Sentinel会以每秒一次的频率向所有与他创建了命令连接的实例(主从服务器以及其他Sentinel)发送PING命 ...

  7. org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate——解决方案汇总

    近期将自己本地的 maven 仓库进行了迁移,idea 的版本也升级到了IntelliJ IDEA 2019.3.3 x64,但是遇到了 Plugins 报红的情况,尝试很多方法,终于解决,现在做一下 ...

  8. MacOS配置.bash_profile,重启终端后配置失效和MacOS .zshrc does not exist问题

    MacOS配置.bash_profile,重启终端后配置失效和MacOS .zshrc does not exist问题 场景 ​ 在Mac中配置golang环境变量更改GOPATH路径,在~/.ba ...

  9. [转] C++项目中的extern "C" {}

    点击阅读原文 引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: #ifdef __cplusplus extern "C" { #endif /*...*/ #if ...

  10. numpy中transpose的功能

    看了网上一堆解释,有用相互交换来解释的,我看了半天也看不出所以然来.心想着自己试验一下. numpy.transpose的用法很简单:假如你有一个四维的数组,那么四个维度就是0,1,2,3.风格会像下 ...