用c++实现这个问题,时间限制1s
时间: 2024-04-12 09:28:58 浏览: 20
以下是使用C++实现的代码:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n, m;
std::cin >> n >> m;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::vector<int> sorted_a(a);
std::sort(sorted_a.begin(), sorted_a.end());
std::cout << sorted_a[n / 2] << std::endl;
for (int i = 0; i < m; i++) {
int p, x;
std::cin >> p >> x;
p--;
int idx = std::lower_bound(sorted_a.begin(), sorted_a.end(), a[p]) - sorted_a.begin();
sorted_a.erase(sorted_a.begin() + idx);
a[p] = x;
sorted_a.insert(std::lower_bound(sorted_a.begin(), sorted_a.end(), x), x);
std::cout << sorted_a[n / 2] << std::endl;
}
return 0;
}
```
该代码使用了`vector`来存储序列a和有序序列sorted_a,使用`sort`函数对sorted_a进行排序,并使用`lower_bound`函数进行二分查找和插入操作。
希望对你有帮助!如果还有其他问题,请随时提问。