ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#213932 | #2380. 中位数 | KXG | 30 | 827ms | 7848kb | C++11 | 2.0kb | 2024-11-14 19:13:58 | 2024-11-14 23:02:34 |
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct BIT {
int n = 600000, t[600010];
void modify(int k, int x) {
k += 300000;
for (int i = k; i <= n; i += i & (-i)) {
t[i] += x;
}
}
int query(int k) {
k += 300000;
int ans = 0;
for (int i = k; i >= 1; i -= i & (-i)) {
ans += t[i];
}
return ans;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
} bt;
int n, a[300010], b[300010], s[300010], now[600010];
int check(int mid, int id) {
for (int i = 1; i <= n; i++) {
if (a[i] < mid) {
b[i] = -1;
} else if (a[i] > mid) {
b[i] = 1;
} else {
b[i] = 0;
}
}
memset(bt.t, 0, sizeof(bt.t));
bt.modify(0, 1);
now[300000]++;
int l = 0, g = 0, e = 0;
for (int i = 1; i <= n; i++) {
b[i] += b[i - 1];
int tmp = bt.query(b[i]) - now[b[i] + 300000];
g += tmp;
e += now[b[i] + 300000];
l += i - tmp - now[b[i] + 300000];
bt.modify(b[i], 1);
now[b[i] + 300000]++;
}
now[300000]--;
for (int i = 1; i <= n; i++) {
now[b[i] + 300000]--;
}
// printf("%d %d : %d %d %d\n", mid, id, l, e, g);
if (id <= l) return -1;
if (id > l + e) return 1;
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s[i] = a[i];
}
sort(s + 1, s + n + 1);
int tot = n * (n + 1) / 2;
int l = 1, r = n, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
int ck = check(s[mid], (tot + 1) / 2);
if (ck == 0) {
ans = mid;
break;
}
if (ck == 1) {
l = mid + 1;
} else {
r = mid - 1;
}
}
printf("%d\n", s[ans]);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 2896kb
input:
200 247837946 222748444 549404903 934970141 181690491 116150147 755881953 912914845 578004877 589768...
output:
411892743
result:
ok 1 number(s): "411892743"
Test #2:
score: 10
Accepted
time: 0ms
memory: 2900kb
input:
200 504553675 321343511 781936447 172776833 779139954 643752948 106 164 303976944 828799931 11502170...
output:
451369372
result:
ok 1 number(s): "451369372"
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 2896kb
input:
200 738368650 91602659 340556191 835209882 409442204 656938221 258295858 90658347 126689369 56196859...
output:
423214040
result:
wrong answer 1st numbers differ - expected: '420852999', found: '423214040'
Test #4:
score: 0
Wrong Answer
time: 5ms
memory: 2960kb
input:
5000 300051244 685920750 122806687 315801142 33592358 339985437 262520930 380010194 687330910 550362...
output:
465222581
result:
wrong answer 1st numbers differ - expected: '465193387', found: '465222581'
Test #5:
score: 10
Accepted
time: 3ms
memory: 2960kb
input:
5000 108135249 950186870 146626753 682580494 744412491 4159 995071655 157947560 668940645 878891308 ...
output:
466892796
result:
ok 1 number(s): "466892796"
Test #6:
score: 0
Wrong Answer
time: 5ms
memory: 2956kb
input:
5000 246610103 780664789 678774940 716124165 518453378 300121008 135383319 939869983 430946660 13412...
output:
470538762
result:
wrong answer 1st numbers differ - expected: '470507384', found: '470538762'
Test #7:
score: 0
Wrong Answer
time: 198ms
memory: 7260kb
input:
300000 786856547 380253059 684225515 795392038 881780483 18437791 793783577 832330260 801624996 3466...
output:
733307663
result:
wrong answer 1st numbers differ - expected: '463539280', found: '733307663'
Test #8:
score: 0
Wrong Answer
time: 203ms
memory: 6972kb
input:
300000 951000485 930070933 3566360 157114366 150196488 611226665 123590886 950624066 472150346 57976...
output:
506086079
result:
wrong answer 1st numbers differ - expected: '462334620', found: '506086079'
Test #9:
score: 0
Wrong Answer
time: 209ms
memory: 6972kb
input:
300000 728509094 406128756 696166390 756254976 31403187 672131182 35544213 870708756 3659210 5227553...
output:
509121858
result:
wrong answer 1st numbers differ - expected: '464162969', found: '509121858'
Test #10:
score: 0
Wrong Answer
time: 204ms
memory: 7848kb
input:
300000 804874490 52247250 546638789 400214210 383796618 47995083 814777769 507157584 466189162 28296...
output:
0
result:
wrong answer 1st numbers differ - expected: '463577194', found: '0'