隐藏页面特效

The 2024 ICPC Asia East Continent Online Contest (I) G.The Median of the Median of the Median

Link: The Median of the Median of the Median

考虑二分答案,对中位数进行二分,每次去判断是否比中位数大即可。

我们钦定了一个中位数 x,对于 {a} 数组,若 aix,则令 ai=1,否则 ai=0,这样有一个好处,我们只关心 10 的数量,就可以知道中位数是否比 x 大。

如果 {a}1 的个数多于 0 的个数,说明中位数大于等于 x,否则中位数小于 x

考虑对所有的 {bi,j}lijr 求值,不妨枚举左端点 l,求出一个以 l 为左端点的 {bi,j}lijr 的中位数,我们通过前缀和可以求出有多少个数是大于 x 的。

cnt[i][j] 表示以 i 为左端点,所有 ikjbi,k 大于等于 x 的个数,pre[0/1] 表示 a[ij]0/1 的数量,容易发现:

cnt[i][j]=cnt[i][j1]+[pre[1]>pre[0]]

考虑如何计算 {cl,r}1lrn,我们枚举 r,对所有 1lr 求和,因为我们计算的 cnt[i][j] 是以左端点固定的,因此计算一对 (l,r) 时,对 lcnt[i][r] 的后缀和即可,不过因为 cnt[i][j] 表示的是一个段,因此,0 的个数变化 ji+1cnt[i][j]1 的个数变化 cnt[i][j],对答案贡献 1 当且仅当每个段 1 的个数大于 0 的个数

1 的个数大于 0 时,说明中位数大于等于 x,二分答案即可。

时间复杂度 O(n2log|a|)

复制#include<bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i ++ ) cin >> a[i]; auto check = [&](int x) -> int { vector<int> b(n + 1); vector<vector<int>> cnt(n + 1, vector<int>(n + 1)); for (int i = 1; i <= n; i ++ ) b[i] = (a[i] >= x); for (int i = 1; i <= n; i ++ ) { vector<int> pre(2); for (int j = i; j <= n; j ++ ) { pre[b[j]] ++ ; cnt[i][j] = cnt[i][j - 1] + (pre[1] > pre[0]); } } vector<int> sum(2); for (int i = 1; i <= n; i ++ ) { vector<int> pre(2); for (int j = i; j; j -- ) { pre[1] += cnt[j][i]; pre[0] += i - j + 1 - cnt[j][i]; sum[pre[1] > pre[0]] ++ ; } } return sum[1] > sum[0]; }; int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } cout << r << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); solve(); return 0; }

__EOF__

本文作者Yip.Chip
本文链接https://www.cnblogs.com/YipChipqwq/p/18538718.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   YipChip  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示