CodeForces 631C Report
2016-03-04 18:32
274 查看
题目链接:http://codeforces.com/problemset/problem/631/C
C. Report
time limit per test:
2 seconds
memory limit per test:
256 megabytes
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 manageri + 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 integersti 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
output
input
output
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,表示有n个产品和m个经理。
然后给出初始的时候的数组,表示每个产品的收益。
然后是m行,每行表示一个经理对数组的操作。
1 ri表示对前ri个数按非降序排列。
2 ri表示对前ri个数按非升序排列。
要求经过m个经理的操作后,最终的数组。
思路:
可以马上发现如果第i个操作的长度>前面所有的操作,那么前面所有的操作都是无效的。
所以先找出一个经理的操作序列,是ri递减的。
然后在区间[r[i]+1, r[i-1]]上的数字,事实上只被处理出来的第i次操作所影响。
所以可以先把初始数组a复制到b数组,然后把初始数组a从小到大排序。
设置两个指针分别表示当前未用的最小数和当前没有使用的最大数。
然后处理出来的操作序列分配这些数就可以了。
找ri递减的操作序列的时候用了线段树,感觉自己像个智障。。。不过懒得改了。。。
C. Report
time limit per test:
2 seconds
memory limit per test:
256 megabytes
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 manageri + 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 integersti 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,表示有n个产品和m个经理。
然后给出初始的时候的数组,表示每个产品的收益。
然后是m行,每行表示一个经理对数组的操作。
1 ri表示对前ri个数按非降序排列。
2 ri表示对前ri个数按非升序排列。
要求经过m个经理的操作后,最终的数组。
思路:
可以马上发现如果第i个操作的长度>前面所有的操作,那么前面所有的操作都是无效的。
所以先找出一个经理的操作序列,是ri递减的。
然后在区间[r[i]+1, r[i-1]]上的数字,事实上只被处理出来的第i次操作所影响。
所以可以先把初始数组a复制到b数组,然后把初始数组a从小到大排序。
设置两个指针分别表示当前未用的最小数和当前没有使用的最大数。
然后处理出来的操作序列分配这些数就可以了。
找ri递减的操作序列的时候用了线段树,感觉自己像个智障。。。不过懒得改了。。。
#include <bits/stdc++.h> using namespace std; #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 int n, M; #define maxn 200010 bool cmpx(int a, int b){return a<b;} bool cmpd(int a, int b){return a>b;} int a[maxn],b[maxn]; int op[maxn], c[maxn]; int co; struct Node { int val, pos; Node(){} }node[maxn<<2]; vector <int> v; void PushUp(int rt) { if(node[rt<<1].val >= node[rt<<1|1].val) { node[rt].val = node[rt<<1].val; node[rt].pos = node[rt<<1].pos; } else { node[rt].val = node[rt<<1|1].val; node[rt].pos = node[rt<<1|1].pos; } } void build(int l, int r, int rt) { if(l == r) { node[rt].val = c[co]; node[rt].pos = co; co++; return; } int m = (l+r)>>1; build(lson); build(rson); PushUp(rt); } Node query(int L, int R, int l, int r, int rt) { if(L <= l && R >= r) { return node[rt]; } int m = (l+r)>>1; Node ret; ret.val = 0; if(L <= m) ret = query(L, R, lson); if(R > m) { Node ret2 = query(L, R, rson); if(ret2.val > ret.val) return ret2; } return ret; } int main() { // freopen("in.txt", "r", stdin); while(~scanf("%d%d", &n, &M)) { for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= M; i++) scanf("%d%d", &op[i], &c[i]); v.clear(); co = 1; build(1, M, 1); Node x = query(1, M, 1, M, 1); int nowpos; nowpos = x.pos; v.push_back(nowpos); while(nowpos < M) { Node x = query(nowpos+1, M, 1, M, 1); nowpos = x.pos; v.push_back(nowpos); } for(int i = 1; i <= n; i++) b[i] = a[i]; sort(a+1, a+1+c[v[0]], cmpx); int l = 1, r = c[v[0]]; for(int i = 1; i < v.size(); i++) { for(int j = c[v[i-1]]; j > c[v[i]]; j--) { if(op[v[i-1]] == 1) b[j] = a[r--]; else if(op[v[i-1]] == 2) b[j] = a[l++]; } } for(int j = c[v[v.size()-1]]; j >= 1; j--) { if(op[v[v.size()-1]] == 1) b[j] = a[r--]; else b[j] = a[l++]; } for(int i = 1; i <= n; i++) { if(i == 1) printf("%d", b[i]); else printf(" %d", b[i]); } printf("\n"); } return 0; }
相关文章推荐
- find:paths must precede expression问题及解决
- 使用vi编辑器遇到的问题
- JNI调用native方法出现 java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()异常的解决办法
- NS_ENUM vs. NS_OPTIONS
- 模板页显示Excel数据Gridview增删改查
- Epic Games创始人眼中的虚拟现实
- 最强大脑与最强团队
- C#中,带关键字的按页获取查询,这是部份代码,但可以看出大概实现方式
- 【VS开发】内存映射文件进程间共享内存
- ONOS安装和运行
- 新型PID控制及其应用(二)
- 文章标题
- 重学备忘
- 关于xib布局,不写死centerY,根据屏幕比例进行适应
- 【安卓基础四】adb命令使用Heap检测和分析Android应用内存
- Android开发中的View类的视图属性focusableInTouchMode这个属性跟focusable有什么区别?
- Java自动装箱与拆箱
- 二分图判断(交叉染色)
- ViewGroup的事件分发机制
- c++复制构造函数实例