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.
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.
题目连接:http://codeforces.com/contest/631/problem/C
题意:一个长度为n的一位数组a,有m操作,每次操作把前r个元素进行非升序或者非降序进行排列。输出最后数组a。
思路:如果后面的操作的元素个数比前面的元素个数多或者相等,前面的操作就会无效。所以优化之后的操作就是r依次递减的。将a的前max(最大的r)位进行非降序排序Q。前一次操作为ti,ri;后一次操作为tj,rj。那么数组a的后ri-rj就确定了,如果ti==1,取剩下的Q的后ri-rj位作为a,如果ti==2,取剩下的Q的前ri-rj作为a。
代码:(Q用数组模拟,标记Q的头s和尾)
#include<bits/stdc++.h>
using namespace std;
struct reorder
{
int p,t,r;
friend bool operator < (reorder a,reorder b)
{
if(a.r!=b.r) return a.r>b.r;
else return a.p>b.p;
}
} reo[];
int a[];
deque<int>Q; ///双向队列
int main()
{
int i,j,n,m;
scanf("%d%d",&n,&m);
for(i=; i<n; i++) scanf("%d",&a[i]);
for(i=; i<m; i++)
{
scanf("%d%d",&reo[i].t,&reo[i].r);
reo[i].p=i+;
}
sort(reo,reo+m);
int cou=reo[].r,sign=reo[].p,flag=reo[].t;
sort(a,a+cou);
for(i=; i<cou; i++) Q.push_back(a[i]); ///队列尾入
reo[m].t=,reo[m].r=,reo[m].p=m+;
for(i=; i<=m; i++)
{
if(reo[i].p>sign)
{
if(flag==)
{
int gg=cou-reo[i].r;
while(gg--)
{
a[--cou]=Q.back(); ///队列尾元素
Q.pop_back(); ///尾列尾出
}
}
else
{
int gg=cou-reo[i].r;
while(gg--)
{
a[--cou]=Q.front(); ///队列头元素
Q.pop_front(); ///队列头出
}
}
sign=reo[i].p;
flag=reo[i].t;
}
}
for(i=; i<n; i++) cout<<a[i]<<" ";
cout<<endl;
return ;
}
数组模拟
关于运算符重载:传送门
关于STL:传送门
Codeforces 631C. Report 模拟的更多相关文章
- Codeforces 631C Report【其他】
题意: 给定序列,将前a个数进行逆序或正序排列,多次操作后,求最终得到的序列. 分析: 仔细分析可以想到j<i,且rj小于ri的操作是没有意义的,对于每个i把类似j的操作删去(这里可以用mult ...
- codeforces 631C. Report
题目链接 按题目给出的r, 维护一个递减的数列,然后在末尾补一个0. 比如样例给出的 4 21 2 4 32 31 2 递减的数列就是3 2 0, 操作的时候, 先变[3, 2), 然后变[2, 0) ...
- codeforces 631C C. Report
C. Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Report CodeForces - 631C (栈)
题目链接 题目大意:给定序列, 给定若干操作, 每次操作将$[1,r]$元素升序或降序排列, 求操作完序列 首先可以发现对最后结果有影响的序列$r$一定非增, 并且是升序降序交替的 可以用单调栈维护这 ...
- Codeforces 389B(十字模拟)
Fox and Cross Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- Codeforces 626B Cards(模拟+规律)
B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...
- Codeforces 679B. Barnicle 模拟
B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...
- CodeForces 382C【模拟】
活生生打成了大模拟... #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsig ...
随机推荐
- LRU的理解与Java实现
简介 LRU(Least Recently Used)直译为"最近最少使用".其实很多老外发明的词直译过来对于我们来说并不是特别好理解,甚至有些词并不在国人的思维模式之内,比如快速 ...
- django项目环境设置
1.连接数据库,如mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_comi ...
- IPv4报文分片
1:为什么需要分片 每个数据链路层协议都有自己的帧格式,在这个格式中有一个字段是"数据字段最大长度"(MTU,最大传输单元),当数据报被封装成帧时,数据报的总长度必须小于这个最大长 ...
- 逆地址解析协议RARP
解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...
- (6/24) 插件配置:轻松配置JS文件压缩
实际开发中,在项目上线之前,我们编写的js代码是需要进行压缩的,我们可以采取压缩软件或者在线进行压缩,这不是我们的重点,在webpack中实现JS代码的压缩才是本节的核心. 通过webpack中可实现 ...
- windows巡检
参考网站: http://www.jb51.net/os/windows/525017.html 系统自带工具巡检 : 先说说如何检查系统健康度的方法,Win+R只有只要输入一个命令: perf ...
- 基于OpenGL编写一个简易的2D渲染框架-09 重构渲染器-Shader
Shader 只是进行一些简单的封装,主要功能: 1.编译着色程序 2.绑定 Uniform 数据 3.根据着色程序的顶点属性传递顶点数据到 GPU 着色程序的编译 GLuint Shader::cr ...
- python之建完model之后操作admin
1)建完model 之后,运行./manage.py migrate 2)建立管理员:./manage.py createsuperuser 3)输入用户名和命令上提示的信息,在点击网址,输入admi ...
- js正则表达使用实例
(1)替换掉htmlStr中所有的<font..>和</font> var htmlstr='<font color="#fff">ABC< ...
- HTML实现文件拖动上传
在大型企业的开发过程中,很多比较有趣而实际的功能往往都是让大家望而却步,我给大家带来一个百度云盘和360云盘的HTML5多文件拖动上传技术: 1:记得导入:common-fileupload.jar包 ...