牛客练习赛48 E 小w的矩阵前k大元素
思路:
优先队列,将迭代器变量作为结构体的变量。
迭代器走的时候只能像一个方向走,另外一个方向只有最开始才走。如下图所示:

如果两个方向同时走,同一个值会被遍历多次,像上图那样就能保证每个位置都走到且只走一次。
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head const int N = 1e5 + ;
int n, m, q, k;
int a[N], b[N];
char op[];
multiset<int> sa, sb;
struct Node {
int x;
multiset<int>::iterator pa, pb;
bool operator < (const Node & rhs) const {
return x > rhs.x;
}
};
int main() {
scanf("%d %d %d", &n, &m, &q);
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
for (int i = ; i <= m; ++i) scanf("%d", &b[i]);
int x = , y = ;
sa.insert(a[]), sb.insert(b[]);
while(q--) {
scanf("%s %d", op, &k);
if(op[] == 'R') {
int ny = min(m, y+k);
for (int i = y+; i <= ny; ++i) sb.insert(b[i]);
y = ny;
}
else if(op[] == 'D') {
int nx = min(n, x+k);
for (int i = x+; i <= nx; ++i) sa.insert(a[i]);
x = nx;
}
else {
priority_queue<Node> q;
q.push({*sa.begin()+*sb.begin(), sa.begin(), sb.begin()});
for (int i = ; i <= k; ++i) {
Node t = q.top();
q.pop();
printf("%d%c", t.x, " \n"[i==k]);
if(t.pa == sa.begin()) {
auto tp = t.pb;
++tp;
if(tp != sb.end()) q.push({*t.pa+*tp, t.pa, tp});
}
auto ta = t.pa;
++ta;
if(ta != sa.end()) q.push({*ta+*t.pb, ta, t.pb});
}
}
}
return ;
}
牛客练习赛48 E 小w的矩阵前k大元素的更多相关文章
- 牛客练习赛$48E$ 小$w$的矩阵前$k$大元素 堆
正解:堆 解题报告: 传送门$QwQ$ 考虑把$b$从大往小排序,然后把$a_1+b_1,a_2+b_1,...,a_n+b_1$丢到堆里,顺便记录下$b$的下标 然后每次拿出一个最大值,设为$mx= ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)
牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...
- 牛客练习赛48 D 小w的基站网络
链接:https://ac.nowcoder.com/acm/contest/923/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 牛客练习赛48 C,D,E
C 小w的糖果 题意:3种操作,第一种是使pos右边的数全部+1,第二种是pos右边的数依次+k(k从1开始递增),第三种是pos右边的数依次+k^2(k从1开始递增). 解法:第一种我们很容易想到差 ...
- 牛客练习赛44 C 小y的质数 (数论,容斥定理)
链接:https://ac.nowcoder.com/acm/contest/634/C 来源:牛客网 题目描述 给出一个区间[L,R],求出[L,R]中孪生质数有多少对. 由于这是一个区间筛质数的模 ...
- 牛客练习赛44 B 小y的线段 (思维)
链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ,每次可以从第i条线段的j位置跳到第 ...
- 牛客练习赛44 A 小y的序列 (模拟,细节)
链接:https://ac.nowcoder.com/acm/contest/634/A 来源:牛客网 小y的序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语 ...
- 牛客练习赛40 A 小D的剧场 (思维dp)
链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福 若你摘得大的星星 你将得到大的财富 若两者都能摘得 你将得到 ...
随机推荐
- flask上下文管理相关 - threading.local 以及原理剖析
threading.local 面向对象相关: setattr/getattr class Foo(object): pass obj = Foo() obj.x1 = 123 # object.__ ...
- (长期更新)【机器学习实践】Pycharm编辑器的使用注意事项
1. 写Python代码,根据PEP8风格,默认一行的长度不超过 80 个字符. 但是pycharm 默认是第 120 个字符处,故进行修改: File→Settings→Editor→Code S ...
- linux中高级信号函数sigaction和sigqueue实例
/************************************************************************* > File Name: sigquque. ...
- Vue2.4+新增属性.sync、$attrs、$listeners
参考链接:https://www.jianshu.com/p/4649d317adfe
- solr7.2.1+tomcat8.5.37+jdk8安装配置
软件下载 solr7.2.1:http://archive.apache.org/dist/lucene/solr/7.2.1/solr-7.2.1.tgz 注意是.tgz结尾的文件,而不是.zip ...
- [转帖]Linux systemd 常用命令
Linux systemd 常用命令 https://www.cnblogs.com/tsdxdx/p/7288490.html systemctl hostnamectl timedatectl l ...
- Oracle的查询-多表查询中的一些概念
--笛卡尔积 select * from emp e,dept d; --等值连接 select * from emp e,dept d where e.deptno=d.deptno --内连接 s ...
- Redis 学习笔记(篇十):Sentinel
Sentinel(哨兵)是 Redis 的高可用解决方案:由一个或多个 Sentinel 实例组成的 Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主 ...
- ubuntu 拨号上网
如果没有安装的用户,可以使用 sudo apt-get install pppoe pppoeconf 然后配置上网 sudo pppoeconf 最后,使用 sudo pon dsl-provide ...
- idea 如何加入插件SonarLint
idea 如何加入插件SonarLint IDEA的插件安装有两种方法:一是在线安装:二是离线安装,即将插件的安装包下载以后从本地安装. 一.在线安装的过程: 1.打开IDEA ...