D. Med-imize
D. Med-imize
Given two positive integers and , and another array of integers.
In one operation, you can select any subarray of size of , then remove it from the array without changing the order of other elements. More formally, let be an operation on subarray such that , then performing this operation means replacing with .
For example, if and we perform operation on this array, it will become . Moreover, operation results in , and operation results in .
You have to repeat the operation while the length of is greater than (which means ). What is the largest possible median of all remaining elements of the array after the process?
The median of an array of length is the element whose index is after we sort the elements in non-decreasing order. For example: , , and .
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains two integers and ().
The second line contains integers () — the array .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print a single integer — the largest median possible after performing the operations.
Example
Input
5
4 3
3 9 9 2
5 3
3 2 5 6 4
7 1
5 9 2 6 5 4 6
8 2
7 1 2 6 8 3 4 5
4 5
3 4 5 6
Output
3
4
9
6
4
Note
In the first test case, you can select a subarray which can be either or . Thus, two obtainable final arrays are and . The former one has the larger median () so the answer is .
In the second test case, three obtainable final arrays are , , and . Their medians are , , and respectively. The answer is .
In the third test case, only one element is left in the final array and it can be any element of the initial array. The largest one among them is , so the answer is .
解题思路
好久没做过中位数的题了,结果赛时罚坐了一个多小时都没做出来。卡了很久才想到可以二分,但把小于二分值的 设成了 ,其余设成 ,然后就是贪心找一定数量的 和 结果死活过不了。看题解才知道应该把小于二分值的 设成 ,并且选出来的下标在模 意义下是是依次加 的,有了这些提示就会做了,真是菜到炸了。
题目可以转换成选出 个数(下面记为 ),假设对应的下标为 ,相邻两个下标之间需要间隔 个元素,即 。
对于涉及中位数的题可以考虑二分,对于二分值 ,如果 则设为 ,否则设为 。这样如果选出的 个数的中位数大于等于 ,那么对应的 的和必定大于 。因此选出的 个数对应的 的和应该尽可能大,此时可以考虑 dp了。
选择的下标之间满足什么样的关系呢?可以知道选出的第一个数的下标 必定满足 ,并且由于 ,因此有 。进而发现对于选出的每个下标都有 。
定义 表示在前 个数中选择了 个数,且 作为第 个数,关于 的和的最大值,状态转移方程为
可以用一个 记录前缀最大值来加速转移。
最后答案就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 5e5 + 5;
int n, m;
int a[N];
int f[N], g[N];
bool check(int mid) {
memset(g, -0x3f, (n - 1) % m + 1 << 2);
for (int i = 0; i < n; i++) {
int w = a[i] >= mid ? 1 : -1;
if (i % m == 0) f[i] = w;
else f[i] = g[i % m - 1] + w;
g[i % m] = max(g[i % m], f[i]);
}
return g[(n - 1) % m] > 0;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int l = 1, r = *max_element(a, a + n);
while (l < r) {
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
Editorial of Codeforces Round 963 (Div. 2):https://codeforces.com/blog/entry/132185
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18344229
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
2022-08-05 耍杂技的牛