QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#576460 | #9314. The Median of the Median of the Median | daring | AC ✓ | 589ms | 123620kb | Java11 | 3.1kb | 2024-09-19 20:28:27 | 2024-09-19 20:28:27 |
Judging History
answer
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static final int MAXN = 2010; // 最大数组长度
static int n, tot; // n 为输入的数组长度,tot 为子数组的总数
static int[] a = new int[MAXN]; // 输入数组
static int[] p = new int[MAXN]; // 排序后的数组
static int[] prea = new int[MAXN]; // 前缀和数组,用于存储小于等于某个值的计数
static int[][] b = new int[MAXN][MAXN]; // b[i][j] 表示区间 [i, j] 中位数是否 <= val
static int[][] preb = new int[MAXN][MAXN]; // 统计 b 的前缀和
// 判断中位数是否 <= val
static boolean check(int val) {
for (int i = 1; i <= n; ++i) {
prea[i] = prea[i - 1] + (a[i] <= val ? 1 : 0); // 计算前缀和
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int sz = (j - i + 1);
// 判断区间 [i, j] 的中位数是否 <= val
b[i][j] = (prea[j] - prea[i - 1] >= (sz + 1) / 2) ? 1 : 0;
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
// 计算从左上角 (1, 1) 到右下角 (i, j) 的 b 的总和
preb[i][j] = b[i][j] + preb[i - 1][j] + preb[i][j - 1] - preb[i - 1][j - 1];
}
}
int res = 0; // 统计中位数 <= val 的个数
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
// 统计所有子区间 [l, r] 中 b 取值为 1 的个数
int num = preb[j][j] - preb[i - 1][j] - preb[j][i - 1] + preb[i - 1][i - 1];
int len = j - i + 1;
int sz = (1 + len) * len / 2; // 子区间总个数
if (num >= (sz + 1) / 2) {
++res; // 中位数符合条件,计数加一
}
}
}
// 如果数量超过一半,说明中位数 <= val
return res >= (tot + 1) / 2;
}
public static void solve() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt(); // 输入数组长度
for (int i = 1; i <= n; ++i) {
a[i] = scanner.nextInt(); // 输入数组元素
p[i] = a[i]; // 复制到排序数组
}
Arrays.sort(p, 1, n + 1); // 排序数组
tot = (1 + n) * n / 2; // 计算子数组总数
int l = 1, r = n; // 二分查找的左右边界
int m, id = 1; // m 为中间值,id 用于记录符合条件的下标
while (l <= r) {
m = (l + r) / 2; // 计算中间值
if (check(p[m])) { // 检查中位数是否符合条件
id = m; // 更新符合条件的下标
r = m - 1; // 继续向左查找
} else {
l = m + 1; // 向右查找
}
}
System.out.println(p[id]); // 输出最终结果
}
public static void main(String[] args) {
solve(); // 调用求解函数
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 94ms
memory: 117676kb
input:
4 1 3 1 7
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 100ms
memory: 123620kb
input:
8 3 3 8 4 5 3 8 5
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 559ms
memory: 122136kb
input:
1883 935804604 209383625 842052635 830082014 365721046 29571412 503828250 261878653 304868479 615753663 149387882 137293208 553441715 659054561 809401479 786598486 257715598 738987349 749751119 675212261 214984147 816730618 204108936 529505526 670681192 375128179 445679706 531625791 954119640 739969...
output:
484006473
result:
ok 1 number(s): "484006473"
Test #4:
score: 0
Accepted
time: 589ms
memory: 121800kb
input:
1957 872909724 707949349 490997221 189511043 696381097 482433184 174043836 548201426 788208141 757294560 536411878 361961 65102705 599648900 120639869 620050728 789988994 932102606 540785122 931710164 286821745 583586084 633444407 849735317 780584308 266550416 98570723 722783990 500921667 819344705 ...
output:
516436748
result:
ok 1 number(s): "516436748"
Test #5:
score: 0
Accepted
time: 117ms
memory: 118388kb
input:
178 692096263 317825236 931010188 434850173 52089022 326215531 68436615 315432237 122955263 641096948 348783853 876201150 853561741 370774198 822929299 828427226 546309487 178339038 549855187 410459983 953700209 566391036 649762057 955693969 976755106 277332611 380022551 211888435 42883285 988396877...
output:
593518959
result:
ok 1 number(s): "593518959"
Test #6:
score: 0
Accepted
time: 114ms
memory: 120500kb
input:
173 69392871 816390960 874922071 794279201 87781777 334366903 738652201 601755009 166103436 192703253 735807848 589526799 219998539 16401241 279391882 366912172 228325988 371454295 781080678 111668287 170762000 333246502 638906040 570891055 791690926 463722145 178137760 257822443 589685312 67772391 ...
output:
408439899
result:
ok 1 number(s): "408439899"
Test #7:
score: 0
Accepted
time: 575ms
memory: 121540kb
input:
2000 844787697 717725741 297174490 620713157 869654860 100305714 252182391 350740797 989692676 884515006 283929742 228386094 243395665 931009658 504278816 417368345 996825244 840349536 30435490 464970260 925567493 320798785 588704893 225104969 963755486 262212671 155822732 272322421 979407661 217426...
output:
493853563
result:
ok 1 number(s): "493853563"
Test #8:
score: 0
Accepted
time: 577ms
memory: 123160kb
input:
2000 125514461 95022349 795740214 269657744 524051185 725933061 555301058 20956383 570982744 927663180 130503343 910377386 251688610 442670648 444873155 283896336 535310191 667590228 223550748 256004263 626775796 392636383 500784551 509216248 283985276 517339979 342212266 70437630 465533157 91397139...
output:
477878585
result:
ok 1 number(s): "477878585"
Extra Test:
score: 0
Extra Test Passed