Codeforces 631C-Report
来源:互联网 发布:一首歌火了网络歌手 编辑:程序博客网 时间:2024/06/08 00:07
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There aren 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 ofm managers. Each of them may reorder the elements in some order. Namely, thei-th manager either sorts first ri numbers in non-descending or non-ascending order and then passes the report to the manageri + 1, or directly to Blake (if this manager has numberi = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequenceai of lengthn and the description of each manager, that is valueri 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 andm (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. Thei-th of these lines contains two integers ti andri (,1 ≤ ri ≤ n), meaning that thei-th manager sorts the first ri numbers either in the non-descending (ifti = 1) or non-ascending (ifti = 2) order.
Print n integers — the final report, which will be passed to Blake by manager numberm.
3 11 2 32 2
2 1 3
4 21 2 4 32 31 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.
题意:已知一段无序的数字,求经过m次操作后的序列,每次操作把前ri个数按不严格递增(ti=1)或不严格递减(ti=2)排序。
思路:如果ri>rj并且i>j的话,第j次操作就没有意义可以去除了,那么最后有意义的操作剩下ri<rj并且 i>j,这样的话可以用数组t和r记录有效操作,用数组b把数组a中0~最大的ri的数字copy下来(因为这个ri之后的数字的顺序都不会改),然后遍历数组r,根据ti把数组b从左读或者从右读进数组a的ri-1~ri的位置。
#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;#define maxn 200005int t[maxn], r[maxn], a[maxn], b[maxn];int main(){ int i, j, ti, ri, n, m, s, bl, br; scanf("%d %d", &n, &m); for(i = 0;i < n;i++) scanf("%d", &a[i]); s = 0; for(i = 0;i < m;i++){ scanf("%d %d", &ti, &ri); while(s > 0&&ri >= r[s-1]) s--; t[s] = ti, r[s] = ri, s++; } bl = 0, br = r[0], r[s++] = 0; for(i = 0;i < br;i++) b[i] = a[i]; sort(b, b+br); for(i = 1;i < s;i++) for(j = r[i-1];j > r[i];j--) a[j-1] = (t[i-1]==1)?b[--br]:b[bl++]; for(i = 0;i < n;i++) printf("%d%c", a[i], i==n-1?'\n':' ');}
- codeforces 631c Report
- Codeforces 631C:Report
- codeforces 631C Report
- Codeforces 631C-Report
- CodeForces 631C Report
- Codeforces 631C Report
- Codeforces 631C Report【其他】
- CodeForces 631C Report(找规律)
- Codeforces 631C Report 【单调栈】
- codeforces 631C Report(单调栈)
- Codeforces 631C Report排序+思维
- Codeforces 631C Report【思维脑洞题】
- 【Codeforces 631 C Report】+ 单调栈
- CodeForces 631 C.Report(单调栈)
- CodeForces 631C.Report(单调栈)
- Codeforces Round #344 (Div. 2) 631C Report(脑洞+stl)
- Codeforces Round #344 (Div. 2) C. Report
- Codeforces Round #344 (Div. 2) C. Report
- for 循环中植入两个变量
- Android开发之ListFragment结合LoaderManager加载数据
- UVA 437The Tower of Babylon - 简单dp
- 标签嵌套规范、网站结构制作实例笔记
- Java基础 常见List实现原理 - ArrayList
- Codeforces 631C-Report
- 单例模式,只要一个就够啦!
- 告诫自己要有目标
- 无法启动 Windows Event Log vpdn 无法启动 附带无法启动MYSQL服务”1067 进程意外终止”
- mysql 过程 动态执行sql
- 转 spring 常用工具类
- 搞搞hibernate.current_session_context_class
- PAT (Advanced Level) Practise 1104 Sum of Number Segments (20)
- 快速排序