POJ2623 Sequence Median【中位数+排序】

本文介绍了一个经典的计算中位数问题SequenceMedian,通过排序算法解决序列中位数的寻找。问题来源于POJ2623,涉及大数据输入处理,推荐使用scanf进行快速读取。文章提供了一段C++代码示例,展示了如何使用std::sort和double类型精确计算中位数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sequence Median
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17909 Accepted: 4964
Description

Given a sequence of N nonnegative integers. Let’s define the median of such sequence. If N is odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+1)/2 in sorted sequence if sequence elements are numbered starting with 1. If N is even then the median is the semi-sum of the two “middle” elements of sorted sequence. I.e. semi-sum of the elements in positions N/2 and (N/2)+1 of sorted sequence. But original sequence might be unsorted.

Your task is to write program to find the median of given sequence.
Input

The first line of input contains the only integer number N - the length of the sequence. Sequence itself follows in subsequent lines, one number in a line. The length of the sequence lies in the range from 1 to 250000. Each element of the sequence is a positive integer not greater than 2^32 - 1 inclusive.
Output

You should print the value of the median with exactly one digit after decimal point.
Sample Input

4
3
6
4
5
Sample Output

4.5
Hint

Huge input,scanf is recommended.
Source

Ural Collegiate Programming Contest 1998

问题链接:POJ2623 Sequence Median。

问题简述:参见上文。

问题分析:

这是一个计算中位数问题,排序计算即可。

程序说明:

计算平均值需要使用double类型,不然会出问题。

因为要求提示会有大数据,所以用long long

且数组要开大,不然会 RE

题记:

程序员要处处小心谨慎。

AC的C++语言程序如下:

#include<bits/stdc++.h> 

int main()
{
	long long  a[250000];
	long long  n;
	double mid;
	scanf("%lld",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	std::sort(a,a+n);
	if(n%2==0){
		mid=((double)a[n/2]+(double)a[n/2-1])/2;
	}
	else mid=(double)a[n/2];
	printf("%.1lf",mid);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值