UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214008#2380. 中位数STASISZHY501149ms12992kbC++111.6kb2024-11-14 22:22:492024-11-14 23:10:38

answer

// Problem: A. 中位数
// Contest: undefined - NOIP2024训练赛 05
// URL: http://119.28.3.174/contest/1157/problem/2380
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PII pair<int, int>

using namespace std;

const int N = 3e5 + 10, M = 1e6 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;

int n, m, q, ans, len;
int s[N], dp[N], cy[N], tr[(N << 1) + 1000];

inline int lowbit(int x) {return x & -x;}
inline void add(int x) {while(x <= 2 * n + 4) tr[x] ++, x += lowbit(x);}
inline int query(int x) {int res = 0; while(x) res += tr[x], x -= lowbit(x); return res;}

inline void init()
{
	sort(cy + 1, cy + n + 1);
	len = unique(cy + 1, cy + n + 1) - cy - 1;
	for(int i = 1; i <= n; i ++) s[i] = lower_bound(cy + 1, cy + len + 1, s[i]) - cy;
}

inline bool check(int x)
{
	// for(int i = 1; i <= 2 * n; i ++) tr[i] = 0; 
	memset(tr, 0, sizeof tr);
	int res = 0; add(dp[0]);
	for(int i = 1; i <= n; i ++) 
	{
		dp[i] = dp[i - 1] + (s[i] > x ? 1 : -1);
		res += query(dp[i]);
		add(dp[i]);
	}
	return res >= n * (n + 1) / 2 / 2;
}

signed main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i ++) {cin >> s[i]; cy[i] = s[i];}
	init();
	int l = 1, r = len, res = 0; dp[0] = len + 1; 
	while(l <= r)
	{
		int mid = l + r >> 1;
		if(check(mid)) l = mid + 1, res = mid;
		else r = mid - 1;
	}
	// cout << "res = " << res << '\n';
	cout << cy[res] << '\n';
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 5976kb

input:

200
247837946 222748444 549404903 934970141 181690491 116150147 755881953 912914845 578004877 589768...

output:

411892743

result:

ok 1 number(s): "411892743"

Test #2:

score: 0
Wrong Answer
time: 4ms
memory: 5972kb

input:

200
504553675 321343511 781936447 172776833 779139954 643752948 106 164 303976944 828799931 11502170...

output:

460454728

result:

wrong answer 1st numbers differ - expected: '451369372', found: '460454728'

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 5976kb

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: 3ms
memory: 6088kb

input:

5000
300051244 685920750 122806687 315801142 33592358 339985437 262520930 380010194 687330910 550362...

output:

465452979

result:

wrong answer 1st numbers differ - expected: '465193387', found: '465452979'

Test #5:

score: 10
Accepted
time: 0ms
memory: 6084kb

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: 3ms
memory: 6084kb

input:

5000
246610103 780664789 678774940 716124165 518453378 300121008 135383319 939869983 430946660 13412...

output:

470628854

result:

wrong answer 1st numbers differ - expected: '470507384', found: '470628854'

Test #7:

score: 10
Accepted
time: 276ms
memory: 12992kb

input:

300000
786856547 380253059 684225515 795392038 881780483 18437791 793783577 832330260 801624996 3466...

output:

463539280

result:

ok 1 number(s): "463539280"

Test #8:

score: 0
Wrong Answer
time: 286ms
memory: 12992kb

input:

300000
951000485 930070933 3566360 157114366 150196488 611226665 123590886 950624066 472150346 57976...

output:

462340870

result:

wrong answer 1st numbers differ - expected: '462334620', found: '462340870'

Test #9:

score: 10
Accepted
time: 293ms
memory: 12992kb

input:

300000
728509094 406128756 696166390 756254976 31403187 672131182 35544213 870708756 3659210 5227553...

output:

464162969

result:

ok 1 number(s): "464162969"

Test #10:

score: 10
Accepted
time: 280ms
memory: 12988kb

input:

300000
804874490 52247250 546638789 400214210 383796618 47995083 814777769 507157584 466189162 28296...

output:

463577194

result:

ok 1 number(s): "463577194"