1029 Median (25 point(s))

1029 Median (25 point(s))

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤2×10​5​​) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

Output Specification:

For each test case you should output the median of the two given sequences in a line.

Sample Input:

4 11 12 13 14
5 9 10 15 16 17

Sample Output:

13

 

思路:

1.原本思路:开两个数组,然后采用归并排序的方式查找median元素(设置两个游标i,j)。结果:内存超限(22)。

2.正解思路:只开一个数组,然后对于每一个新输入的元素进行判断。

3.核心:归并排序

(1)注意判断median元素(即cnt==0时结束);

(2)注意有可能第二个序列输入完以后,还没有找到median(即在数组a[]还没遍历的部分)。

#include<iostream>
using namespace std;
const int MAX = 200007;
int a[MAX];
int main(void)
{
	int n,m;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	scanf("%d",&m);
	int temp;
	int index=0;
	int cnt = (n+m+1)/2;//目标计数 
	int res;
	for(int i=0;i<m;i++){
		scanf("%d",&temp);
		while(temp>a[index]&&index<n&&cnt){//如果新输入的数大于当前index指向的数 
			index++;cnt--;//index移动,计数减少1 
		}
		if(cnt==0){//如果此时计数结束,则说明中位数已经产生 
			res=a[--index];//注意必须要回溯1个元素 
			break;
		}
		cnt--;//没有到达中位数位置(即cnt不是0),则计数再减少1个(即temp) 
		if(cnt==0){//如果此时计数结束,原理同上 
			res=temp;
			break;
		}
	}
	if(cnt){//如果所有的数输入完毕以后,还没有出现中位数,说明中位数在序列a[]中 
		while(cnt--){
			index++;
		}
		res=a[--index];
		//res=a[index+cnt-1]; 
	}
	printf("%d\n",res);
	return 0;
}

附:原本思路代码

#include<iostream>
using namespace std;
const int MAX = 200007;
int a[MAX],b[MAX];
int main(void)
{
	int n,m;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	scanf("%d",&m);
	for(int i=0;i<m;i++) scanf("%d",&b[i]);
	int count=0,i=0,j=0;
	int pre;
	while(count<(n+m+1)/2){
		if(i<n&&j<m){
			if(a[i]<b[j]){
				pre = a[i];
				i++;
			} 
			else{
				pre = b[j];
				j++;
			}
		}
		else if(i>=n){
			pre = b[j];
			j++;
		}
		else{
			pre = a[i];
			i++;
		}
		count++;
	}
	printf("%d\n",pre);
	return 0;
}

https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968

 

如下: m1 <- glm(num_awards ~ prog + math, family="poisson", data=p) summary(m1) Call: glm(formula = num_awards ~ prog + math, family = "poisson", data = p) Deviance Residuals: Min 1Q Median 3Q Max -2.2043 -0.8436 -0.5106 0.2558 2.6796 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -5.24712 0.65845 -7.969 1.60e-15 *** progAcademic 1.08386 0.35825 3.025 0.00248 ** progVocational 0.36981 0.44107 0.838 0.40179 math 0.07015 0.01060 6.619 3.63e-11 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 287.67 on 199 degrees of freedom Residual deviance: 189.45 on 196 degrees of freedom AIC: 373.5 Number of Fisher Scoring iterations: 6 cov.m1 <- vcovHC(m1, type="HC0") cov.m1 (Intercept) progAcademic progVocational math (Intercept) 0.417313917 -0.0501798114 -9.053756e-02 -5.996339e-03 progAcademic -0.050179811 0.1030719195 8.710937e-02 -6.688459e-04 progVocational -0.090537557 0.0871093744 1.603340e-01 5.649134e-05 math -0.005996339 -0.0006688459 5.649134e-05 1.088927e-04 std.err <- sqrt(diag(cov.m1)) r.est <- cbind(Estimate= coef(m1), "Robust SE" = std.err, "Pr(>|z|)" = 2 * pnorm(abs(coef(m1)/std.err), lower.tail=FALSE), LL = coef(m1) - 1.96 * std.err, UL = coef(m1) + 1.96 * std.err) r.est with(m1, cbind(res.deviance = deviance, df = df.residual, p = pchisq(deviance, df.residual, lower.tail=FALSE))) Estimate Robust SE Pr(>|z|) LL UL (Intercept) -5.2471244 0.64599839 4.566630e-16 -6.51328124 -3.98096756 progAcademic 1.0838591 0.32104816 7.354745e-04 0.45460476 1.71311353 progVocational 0.3698092 0.40041731 3.557157e-01 -0.41500870 1.15462716 math 0.0701524 0.01043516 1.783975e-11 0.04969947 0.09060532 ## update m1 model dropping prog m2 <- update(m1, . ~ . - prog) ## test model differences with chi square test anova(m2, m1, test="Chisq") s <- deltamethod(list(~ exp(x1), ~ exp(x2), ~ exp(x3), ~ exp(x4)), coef(m1), cov.m1) ## exponentiate old estimates dropping the p values rexp.est <- exp(r.est[, -3]) ## replace SEs with estimates for exponentiated coefficients rexp.est[, "Robust SE"] <- s rexp.est (s1 <- data.frame(math = mean(p$math), prog = factor(1:3, levels = 1:3, labels = levels(p$prog)))) predict(m1, s1, type="response", se.fit=TRUE) ## calculate and store predicted values p$phat <- predict(m1, type="response") ## order by program and then by math p <- p[with(p, order(prog, math)), ] ## create the plot ggplot(p, aes(x = math, y = phat, colour = prog)) + geom_point(aes(y = num_awards), alpha=.5, position=position_jitter(h=.2)) + geom_line(size = 1) +   labs(x = "Math Score", y = "Expected number of awards")+theme_bw()
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值