当前位置: 首页 > news >正文

决斗(模拟赛题目T3)分析

题目概述

田忌赛马的最大字典序。

分析

感觉很典,所以就记下来了。

我们考虑一个非常重要的事实:

  • 田忌赛马对于每一个 \(a_i\) 找的是第一个比他大的 \(b_i\)
  • 而字典序最大又需要前面的尽可能大。

这似乎产生了矛盾,让这道题目看起来有点难。

我们考虑不用 multiset 或者双指针求这个值,我们考虑分治的过程。

CDQ 分治是这样的:

对于左区间算好其分内的答案,右区间算好其分内的答案。

要合并区间的时候,左区间和右区间匹配就行了。

那么我们只需要记录每个区间剩下多少个 \(a\) 中的元素和 \(b\) 中的元素还没有匹配即可。

最精华的部分来了:由于我们对于每一个 \(a_i\) 肯定进行二分答案是否可行,因为字典序本质上就有一个贪心的过程。我们考虑把这个思想搬到一个可支持修改的线段树上面即可。

在线段树上面这个是好维护的,于是这道题就做完了。

代码

时间复杂度 \(\mathcal{O}(n\log V\log n)\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <set>
#define int long long
#define N 100005
using namespace std;
struct node{int val,a,b;
}tr[N << 2];
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
void pushup(int x) {int res = min(tr[ls(x)].a,tr[rs(x)].b);tr[x].a = tr[ls(x)].a + tr[rs(x)].a - res;tr[x].b = tr[ls(x)].b + tr[rs(x)].b - res;tr[x].val = tr[ls(x)].val + tr[rs(x)].val + res;
}
void update(int x,int l,int r,int pos,int a,int b) {if (l == r) {tr[x].a += a,tr[x].b += b;return;}int mid = l + r >> 1;if (pos <= mid) update(ls(x),l,mid,pos,a,b);else update(rs(x),mid + 1,r,pos,a,b);pushup(x);
}
int n,a[N],b[N];
multiset<int> st;
signed main(){cin >> n;for (int i = 1;i <= n;i ++) scanf("%lld",&a[i]);for (int i = 1;i <= n;i ++) scanf("%lld",&b[i]);int m = 1e5;for (int i = 1;i <= n;i ++) {st.insert(b[i]);update(1,1,m,a[i],1,0);update(1,1,m,b[i],0,1);}int mx = tr[1].val;for (int i = 1;i <= n;i ++) {update(1,1,m,a[i],-1,0);int l = a[i] + 1,r = *st.rbegin(),res = -1;while(l <= r) {int mid = l + r >> 1;auto t = st.lower_bound(mid);if (t == st.end()) {r = mid - 1;continue;}update(1,1,m,*t,0,-1);if (tr[1].val + (a[i] < *t) == mx) l = mid + 1,res = *t;else r = mid - 1;update(1,1,m,*t,0,1);}if (res != -1) {st.erase(st.find(res));update(1,1,m,res,0,-1);printf("%lld ",res);mx --;continue;}l = *st.begin(),r = a[i],res = l;while(l <= r) {int mid = l + r >> 1;auto t = st.lower_bound(mid);if (t == st.end()) {r = mid - 1;continue;}update(1,1,m,*t,0,-1);if (tr[1].val == mx) l = mid + 1,res = *t;else r = mid - 1;update(1,1,m,*t,0,1);}update(1,1,m,res,0,-1);printf("%lld ",res);st.erase(st.find(res));}return 0;
}
http://www.gsyq.cn/news/17974.html

相关文章:

  • gitlen中,已经提交了内容,如何回退到修改前?
  • HCIP-IoT/H52-111 真题详解(章节C),接入实用的技术和网络设计 /Part1
  • MySQL 8.0 my.cnf 配置详解
  • dremio sql server uniqueidentifier 数据类型问题
  • Why cant developing countries become developed?
  • 22 LCA模拟赛2T1 奶龙与贝利亚 题解
  • 微软拼音输入法自定义短语批量导入导出工具(支持Windows 10/11)
  • 01-Vue3阶段必会的前置知识-01变量和常量
  • 这是我的第一个个人博客
  • BLDC中的Q15
  • MaxProduct
  • Lab 4 Challenge - Sum of Proper Elements
  • Ignite3 竟然变成分布式数据库了!
  • WCH低功耗蓝牙系列芯片usb烧录故障排查
  • 使用docker构建.net api镜像及nginx反向代理 - binzi
  • Docker实用篇(初识Docker,Docker的基本操作,Dockerfile自定义镜像,Docker-Compose,Docker镜像仓库) - a
  • C 语言的验证码图像识别系统实现
  • 一个有趣的网站,可以给自己生成一个奖牌:aitokenawards.com
  • 109
  • lzr 的区间(interval)
  • 使用c#操作elasticsearch8
  • 使用虚幻引擎|UE5制作自动开关门 - 教程
  • 计算机中级
  • CF45C Dancing Lessons 题解
  • APUE学习笔记之文件IO(三) - Invinc
  • 供应链优化技术助力应对疫情挑战
  • 搜索关键词 - 呓语
  • 阅读《构建之法》产生的问题
  • 每日反思(2025.10.09)
  • 软件工程学习日志2025.10.9