【Codeforces 631C 】Report(单调栈,思维模拟)

题干:

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.

题目大意:

给定n个数和m次操作。每次操作分两种输入格式: 
1   xi 。表示将前xi个数升序排列。
2   xi 。表示将前xi个数降序排列。
让你输出操作完以后的序列。

解题报告:

      首先对于不同位置的操作,显然对于第i个操作,若有第j个操作(1<=j<i)(1<=j<i) 且(xj<=xi)(xj<=xi),则第j个操作是可以无视的。这样我们就维护一个xi严格递减的单调栈,在两个邻近操作之间倒着填数就可以了,因为这些操作的特性就是,最左边的数字确定不了,但是右侧的数字一定是确定的,,最后一个操作单独处理(全填上就可以了)

 

超时代码1:(这个是又改过一点了?忘了答案正确与否了,直接从电脑上贴过来了)(确定了,这个是WA的)

#include<bits/stdc++.h>using namespace std;
int a[200000 + 5];
int op[200000 + 5],zxz[200000 + 5],R[200000 + 5],zz[200000 + 5];
int n,m,top;
bool cmp(const int & x,const int & y) {return x>y;
}
int main()
{scanf("%d%d",&n,&m);int maxx = 0,maxi = 1;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=m; i++) {scanf("%d%d",op+i,zxz+i);if(zxz[i] > maxx) {maxx = zxz[i];maxi = i;} }stack<int > sk;for(int i = 1; i<=m; i++) {while(!sk.empty() && zxz[sk.top()] < zxz[i]) sk.pop();if(sk.empty()) R[i] = 0;else R[sk.top()] = i;sk.push(i);}while(maxi !=0 ) {zz[++top] = maxi;maxi = R[maxi];}
//	printf("%d %d \n%d %d \n",zz[1],zz[2],zxz[zz[1]],zxz[zz[2]]);if(op[zz[top]] == 1) sort(a+1,a+zxz[zz[top]] + 1);else sort(a+1,a+zxz[zz[top]]+1,cmp);for(int i = top-1; i>=1; i--) {if(op[zz[i]] == 1) {sort(a+zxz[zz[i+1]],a + zxz[zz[i]] + 1);}else {sort(a+zxz[zz[i+1]],a + zxz[zz[i]] + 1,cmp);}}
//		for(int i = 1; i<=m; i++) printf("%d  %d\n",i,R[i]);for(int i = 1; i<=n; i++) {printf("%d%c",a[i],i == n ? '\n' : ' ');}return 0 ;
}

真*超时代码:(思路十分清晰的超时代码)

#include<bits/stdc++.h>using namespace std;
int a[200000 + 5];
int op[200000 + 5],zxz[200000 + 5],R[200000 + 5];
int n,m;
bool cmp(int x,int y) {return x>y;
}
int main()
{while(~scanf("%d%d",&n,&m)) {int maxx = 0,maxi = 1;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=m; i++) {scanf("%d%d",op+i,zxz+i);if(zxz[i] > maxx) {maxx = zxz[i];maxi = i;} }stack<int > sk;for(int i = 1; i<=m; i++) {while(!sk.empty() && zxz[sk.top()] < zxz[i]) sk.pop();if(sk.empty()) R[i] = 0;else R[sk.top()] = i;sk.push(i);}while(maxi !=0 ) {if(op[maxi] == 1) {sort(a+1,a + zxz[maxi] + 1);}else {sort(a+1,a+zxz[maxi]+1,cmp);}maxi = R[maxi];}
//		for(int i = 1; i<=m; i++) printf("%d  %d\n",i,R[i]);for(int i = 1; i<=n; i++) {printf("%d%c",a[i],i == n ? '\n' : ' ');}}return 0 ;} 

AC代码:(202ms)

#include<bits/stdc++.h>using namespace std;
int a[200000 + 5],b[200000 + 5];
int op[200000 + 5],zxz[200000 + 5],R[200000 + 5],zz[200000 + 5];
int n,m,top;
bool cmp(const int & x,const int & y) {return x>y;
}
int main()
{scanf("%d%d",&n,&m);int maxx = 0,maxi = 1;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=m; i++) {scanf("%d%d",op+i,zxz+i);if(zxz[i] > maxx) {maxx = zxz[i];maxi = i;} }stack<int > sk;//右侧临近比他小的中  最大的 for(int i = 1; i<=m; i++) {while(!sk.empty() && zxz[sk.top()] < zxz[i]) sk.pop();if(sk.empty()) R[i] = 0;else R[sk.top()] = i;sk.push(i);}while(maxi !=0 ) {zz[++top] = maxi;maxi = R[maxi];}if(op[zz[1]] == 1) sort(a+1,a+zxz[zz[1]] + 1);else sort(a+1,a+zxz[zz[1]]+1,cmp);for(int i = 1; i<=zxz[zz[1]]; i++) {b[i] = a[i];//先从小到大保存下来,最后输出a就可以了 } sort(b+1,b+zxz[zz[1]]+1);//从小到大  即我们需要填入这些数进入a数组中 int l = 1,r = zxz[zz[1]];for(int i = 1; i<top; i++) {for(int j = zxz[zz[i]]; j>=zxz[zz[i+1]]+1; j--) {if(op[zz[i]] == 1) a[j] = b[r--];else a[j] = b[l++];}}for(int j = zxz[zz[top]]; j>=1; j--) {if(op[zz[top]] == 1) a[j] = b[r--];else a[j] = b[l++];}for(int i = 1; i<=n; i++) {printf("%d%c",a[i],i == n ? '\n' : ' ');}return 0 ;
}

帮助我们深刻理解一下单调栈中存的元素的性质!!其实可以不用while那一步来存一个zz,,直接可以用栈内的元素其实就是我们想要的。

另外我们写的这个单调栈还是有点讲究的啊。。写成了R数组中存的是不严格小于的了、、、这样还是会有重复操作的,。,改成严格小于的应该会好一些?还没提交试一下、、、(试过了,WA15了,想想也确实不能这么写, 因为就是得不严格小于,也就是小于等于,因为你想啊,万一后面还有一个和你一样大的,那肯定选后面那个不选你啊,因为你这个操作肯定会被后面那个操作覆盖掉的。。。)

附:简洁的30行代码:(红名大佬Orz,不过思路还是可以学习一下的)(187ms)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,maxx,x;
int main()
{scanf("%d%d",&n,&m);vector<int> a, b(n + 2, 0), c(n + 2, 0);for(int i = 0; i<n; i++) {scanf("%d",&x);a.push_back(x);//或者直接读入cin>>a[i]; }for(int i = 1; i<=m; i++) {int x, y;scanf("%d%d", &x, &y);b[y - 1] = x;//操作 c[y - 1] = i;//操作数maxx = max(maxx, y);}vector<int> tmp = a, ans = a;sort(tmp.begin(),tmp.begin() + maxx);int l = 0, r = maxx - 1;for(int i = maxx - 1; i >= 0; i--) {if(c[i] < c[i + 1]) {c[i] = c[i + 1];b[i] = b[i + 1];}}for(int i = maxx - 1; i >= 0; i--) {if(b[i] == 2) ans[i] = tmp[l++];else ans[i] = tmp[r--];}for(int i = 0; i < n; ++i)printf("%d ", ans[i]);return 0;
}

改成了数组的形式还是187ms:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,maxx,x;
int a[200000 + 5],b[200000 + 5],c[200000 + 5],tmp[200000 + 5],ans[200000 + 5];
int main()
{scanf("%d%d",&n,&m);for(int i = 0; i<n; i++) {scanf("%d",a+i);}for(int i = 1; i<=m; i++) {int x, y;scanf("%d%d", &x, &y);b[y - 1] = x;//操作 c[y - 1] = i;//操作数maxx = max(maxx, y);}for(int i = 0; i<n; i++) {ans[i] = tmp[i] = a[i];}
//	vector<int> tmp = a, ans = a;sort(tmp,tmp+maxx);int l = 0, r = maxx - 1;for(int i = maxx - 1; i >= 0; i--) {if(c[i] < c[i + 1]) {c[i] = c[i + 1];b[i] = b[i + 1];}}for(int i = maxx - 1; i >= 0; i--) {if(b[i] == 2) ans[i] = tmp[l++];else ans[i] = tmp[r--];}for(int i = 0; i < n; ++i)printf("%d ", ans[i]);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441315.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle开放视图,Oracle视图

Oracle视图在Oracle中&#xff0c;视图是实际上并不存在的虚拟表。它存储在Oracle数据字典中&#xff0c;不存储任何数据。可以在调用时执行。通过连接一个或多个表的查询创建视图。Oracle创建视图句法&#xff1a;参数&#xff1a;view_name&#xff1a;它指定要创建的Oracle …

【CodeForces - 214C 】Game (拓扑排序,思维)

题干&#xff1a; Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that th…

php$this-conn可以不先定义吗,CodeIgniter 是不是支持PDO 查询?还是本来就不支持

CodeIgniter 是否支持PDO 查询&#xff1f;还是本来就不支持&#xff1f;本帖最后由 default7 于 2014-11-15 19:34:55 编辑配置CodeIgniter 的database 连接方式为PDO 类型&#xff0c;但是怎么都查询不到数据&#xff0c;却可以查处有多少记录&#xff01;自带代码仔细跟踪代…

【ZOJ - 2836 】Number Puzzle (容斥原理)

题干&#xff1a; Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list. Input The input contains several test cases. …

轮番滑动PHP,touch事件之滑动判断(左右上下方向)

touch事件之滑动判断(左右上下方向)作者: 2020.03.11 本文发布于18天前 分类:$("body").on("touchstart", function(e) {// 判断默认行为是否可以被禁用if (e.cancelable) {// 判断默认行为是否已经被禁用if (!e.defaultPrevented) {e.preventDefault();}}…

【CodeForces - 215A】Bicycle Chain (水题)

题干&#xff1a; Vasyas bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th sta…

ubuntu 在线安装php,ubuntu在线安装LNMP

一直以来个人安装lamp环境都是源码编译的&#xff0c;这个过程呢其实也要去经历的&#xff0c;但是毕竟占用时间久&#xff0c;有些时候在做一些测试环境的时候&#xff0c;可以在线安装比较快源码编译nginx可看往期&#xff1a;Nginx的安装对于lnmp的在线安装&#xff0c;如下…

【CodeForces - 215B 】Olympic Medal (数学,公式推导)

题干&#xff1a; The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1)made of metal with density p1 g/cm3. The second part is an i…

oracle的分支语句,oracle中的分支与循环语句

分支语句 if的三种写法一, if 2 < 1 thendbms_output.put_line(‘条件成立‘);end if;二, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsedbms_output.put_line(‘条件不成立‘);end if;三, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsif 4 > 3 …

【CodeForces - 215C 】Crosses (思维,图形题)

题干&#xff1a; There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and co…

Scaffold php,GitHub - yiiplus/scaffold: scaffold是一个基于Yii2高级项目模版工程化实现的应用程序...

Yii 2 Scaffold Project Kit易加-脚手架(scaffold)是一个基于Yii2高级项目模版工程化实现的应用程序&#xff0c;它将更加高效、规范和工程化的满足项目开发的需求。DIRECTORY STRUCTUREcommonconfig/ contains shared configurationsmail/ contains view files for e-mailsmod…

*【CodeForces - 214D 】Numbers (dp,组合数学)

题干&#xff1a; Furik loves writing all sorts of problems, especially such that he cant solve himself. Youve got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it. There is integer n and array a, consisting of ten integers…

oracle修改某个数据类型,Oracle 修改某个字段的数据类型三种方式

1.将该列设置为null,再修改其类型(这样会丢失数据)2.最简单的方法&#xff1a;假设你的表名为 tab_targetcreate table test as select * from tab_target whre 12;alter table test modify (col_name number(5));insert into test select * from tab_target;drop table tab_t…

【EOJ Monthly 2018.10 - B】 莫干山奇遇 (思维构造,数学,数组,贪心)(总结)

题干&#xff1a; Time limit per test: 2.0 seconds Memory limit: 512 megabytes 出题人当然是希望出的题目有关 oxx&#xff0c;于是想方设法给题目配上一些有关 oxx 的背景故事&#xff0c;使得它看起来不那么无趣。但有的时候却无法引入合适的小姐姐&#xff0c;使得 o…

有奶瓶的linux系统,用U盘启动BEINI(奶瓶)系统

用U盘启动&#xff1a;奶瓶(beini)这个系统&#xff0c;是一款基于Tiny Core Linux 搭建的无线网络安全测试系统&#xff0c;当然由于它是用来安全测试的系统&#xff0c;因此在安全方面自然有着强大的功能。而且&#xff0c;这个系统非常简便易学&#xff0c;因此现在已经逐渐…

【CodeForces - 227A】Where do I Turn? (计算几何,叉积判断直线拐向)

题干&#xff1a; Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled at point C and began to terrorize the residents of the surrounding villages. A brave hero decided to put an end to the dragon. He moved from…

linux内核镜像sd卡,【原创】Linux QT镜像的制作--制作SD卡启动盘

最近买了个新的开发板&#xff0c;原生的是Android操作系统&#xff0c;需要自己少个启动盘&#xff0c;制作LinuxQT操作系统。新的开发板带这个制作的源文件&#xff0c;要先把这个文件拷贝到虚拟机Ubunbtu的共享目录下。打开share文件下显示文件如下&#xff1a;打开文件夹命…

【CodeForces - 227B 】Effective Approach (STL,思维)

题干&#xff1a; Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array. According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the numbe…

linux信号值头文件位置,Linux C 信号处理机制

一 . 信号1. 信号&#xff1a;是内核发送给某一进程的一种消息 。2. 信号机制&#xff1a;是Linux系统中用于进程之间相互通信或操作的一种机制。3. 信号的来源&#xff1a;信号来源于内核4. 产生原因&#xff1a; (1)用户通过终端输入 (2)进程执行(3)一个进程调用kill向另一个…

【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)

题干&#xff1a; This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into num…