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:
3 1
1 2 3
2 2
2 1 3
4 2
1 2 4 3
2 3
1 2
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,又是一道思维题的更多相关文章
- hdu2094—看似拓扑实际上是一道思维题
HDU2094 产生冠军 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2094 题意:中文题,就不解释了.题意已经非常清楚了. 这道题的看起来像是一 ...
- poj1986 Distance Queries(lca又是一道模版题)
题目链接:http://poj.org/problem?id=1986 题意:就是老问题求val[u]+val[v]-2*val[root]就行.还有这题没有给出不联通怎么输出那么题目给出的数据一定 ...
- 又是一道水题 hdu背包
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...
- Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)
524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...
- Problem C Emergency Evacuation 一道思维题
题目描述 输入 输出 样例 样例输入 样例输入一 样例输入二 样例输出 样例输出一 9 样例输出二 1008 一句话题意:给你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值. 分析 ...
- 一道思维题 &&递归改循环
思路: 比如5 2 12345--> 1245 从3开始,这时候5变成了1.剩下4512,对应1234.只需要找到现在n-1,k中的数对应原来的编号的映射. 比如1-->3 是1+2 mo ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- BZOJ4401: 块的计数 思维题
Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
随机推荐
- vi命令总结
VI常用技巧 VI命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,但是因为它的命令集众多,很多人都不习惯使用它,其实您只需要掌握基本命令,然后加以灵活运用,就会发现它的优势,并会逐 ...
- 真香,撸一个SpringBoot在线代码修改器
前言 项目上线之后,如果是后端报错,只能重新编译打包部署然后重启:如果仅仅是前端页面.样式.脚本修改,只需要替换到就可以了. 小公司的话可能比较自由,可以随意替换,但是有些公司权限设置的比较严格,需要 ...
- MySQL 8.0权限认证(上)
MySQL 8.0授权认证 一.系统权限表 user 存放用户账户信息以及全局级别(所有数据库)权限,决定了来自哪些主机的哪些用户可以访问数据库实例,如果有全局权限则意味着对所有数据库都有此权 ...
- kworkerds挖矿木马
昨天一朋友的公司服务器中了挖矿病毒,一起帮忙查看并做下记录. 病毒信息 名称:kworkerds 目录:/tmp/ 关键点:文件 -i 属性 i :这个i可就很厉害了.它可以让一个文件“不能被 ...
- Vue点击改变属性(改变文字颜色)
<template> <div class="tab-control"> <div v-for="(item , index) in tit ...
- 详解 Flink DataStream中min(),minBy(),max(),max()之间的区别
解释 官方文档中: The difference between min and minBy is that min returns the minimum value, whereas minBy ...
- mysql8.0 Mac下的安装
一.下载及安装 首先去官网下载mac对应版本的Mysql,尾缀为.dmg的程序包 下载地址:https://dev.mysql.com/downloads/mysql/ 二.启动 在 Setting ...
- OpenSSL & 加密解密
OpenSSL&加密解密(思维导图) 1. 网络通信概述 传输层协议 进程间通信 监听端口 SSL 裸套接字 2. 加密和解密 2.1 加密的方式 对称加密 公钥加密 单向加密 认证加密 2. ...
- python中的常用BIF
BIF:就是python的内置函数,为了方便程序员的! 1.input()输入 在这里,只讲python3,因为python2到python3有不小的变化 通俗的解释就是你在控制台不管输入什么东西,都 ...
- pip环境变量配置
找到python安装目录,进入C:\Users\EDZ\AppData\Local\Programs\Python\Python37-32\Scripts 下.添加此地址到path中 打开cmd 输 ...