newcoder61132L <multiset 维护中位数>
1.newcoder61132F <结论:排序最小交换次数>
2.newcoder61132L <multiset 维护中位数>
题目
中位数
多次询问,每次修改数组中一个数,问修改后n个数的中位数
思路
- 使用
multiset
,分别维护数组的较大的 个和较小的 个; - 根据数据范围,或许可用线段树+二分...
代码
Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
using LL = long long;
const int N = 1e6 + 10;
int a[N];
int b[N];
void print(multiset<int> &a)
{
for (auto it: a) cout << it << ' ';
cout << endl;
}
void solv()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
b[i] = a[i];
}
sort(b+1, b+1+n);
multiset<int> big, small;
int mid = n/2 + 1;
for (int i = 1; i < mid; i ++) small.insert(b[i]);
for (int i = mid; i <= n; i ++) big.insert(b[i]); // mid -> big.begin
int p, x;
for (int i = 1; i <= m; i ++)
{
// cout << "i = " << i << endl;
cin >> p >> x;
auto pos = small.find(a[p]);
if (pos != small.end()) small.erase(pos);
else big.erase(big.find(a[p]));
big.insert(x); a[p] = x;
while (big.size() > small.size() + 1)
{
auto pos1 = big.begin();
small.insert(*pos1);
big.erase(pos1);
}
auto pos1 = small.end(); pos1 --;
auto pos2 = big.begin();
while(*pos1 > *pos2)
{
big.insert(*pos1); small.insert(*pos2);
big.erase(pos2); small.erase(pos1);
pos1 = small.end(); pos1 --;
pos2 = big.begin();
}
cout << (*pos2) << endl;
// cout << "small: " ; print(small);
// cout << "big: " ; print(big);
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
{
solv();
}
return 0;
}
本文来自博客园,作者:O2iginal,转载请注明原文链接:https://www.cnblogs.com/o2iginal/p/17548630.html
合集:
ACM-newcoder
分类:
ACM竞赛
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律