CF1326E Bombs
Problem
给定两个长度均为
排列
Input
第一行输入一个整数
第二行输入
第三行输入
Output
输出一行
Sample
Input 1
3
3 2 1
1 2 3
Output 1
3 2 1
Input 2
6
2 3 6 1 5 4
5 2 1 4 6 3
Output 2
6 5 5 5 4 1
Solution
容易看出答案是单调不增的。
考虑当前答案为
然后就很好做了,把添加炸弹当作
代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int kmax = 3e5 + 3;
struct T {
int s, laz;
} t[kmax << 2];
int n, a[kmax], b[kmax];
int p[kmax];
int res;
void Pushup(int x) {
t[x].s = max(t[x << 1].s, t[x << 1 | 1].s);
}
void Pushdown(int x) {
if (!t[x].laz) return;
t[x << 1].laz += t[x].laz, t[x << 1 | 1].laz += t[x].laz;
t[x << 1].s += t[x].laz, t[x << 1 | 1].s += t[x].laz;
t[x].laz = 0;
}
void Modify(int x, int l, int r, int _l, int _r, int v) { // 线段树区间修改
if (_l <= l && r <= _r) {
t[x].s += v;
t[x].laz += v;
return;
}
Pushdown(x);
int mid = (l + r) >> 1;
if (_l <= mid) Modify(x << 1, l, mid, _l, _r, v);
if (_r > mid) Modify(x << 1 | 1, mid + 1, r, _l, _r, v);
Pushup(x);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
res = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = i; // 记录下每个值所在的位置
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
cout << n << ' '; // 最开始答案显然为n
Modify(1, 1, n, 1, p[n], 1); // 修改
for (int i = 1; i < n; i++) {
for (Modify(1, 1, n, 1, b[i], -1); t[1].s <= 0; Modify(1, 1, n, 1, p[--res], 1)) { // 添加炸弹,如果不存在符合要求的,更新答案
}
cout << res << ' ';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】