九度_1097(取中值)

题目链接:http://ac.jobdu.com/problem.php?pid=1097


这道题其实特别简单,注意不要自认为是排好序了,这道题仅仅只是让你找下标为中间值的那个点,需要判断一下是否有一个还是俩个中值点!判断一下是在左边取还是在右边取即可!

ac代码如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 1111111;
int a[maxn], b[maxn], aa[maxn], bb[maxn];

int main(void)
{
	//freopen("in.txt", "r", stdin);
	int t;

	scanf("%d", &t);
	while (t--)
	{
		int len_a, len_b;
		int aaa, bbb, c, d;
		scanf("%d%d", &len_a, &len_b);
		int i, j;
		for (i = 1; i <= len_a; i++)
			scanf("%d", &a[i]);
		for (i = 1; i <= len_b; i++)
			scanf("%d", &b[i]);
		scanf("%d%d", &aaa, &bbb);                 //采取的办法是将这些范围的值单独放到另外数组中进行排序,我取中间的值,不能合并数组,只能进行操作
		scanf("%d%d", &c, &d);
		for (i = 0, j = aaa; j <= bbb; i++, j++)
			aa[i] = a[j];
		for (i = 0, j = c; j <= d; j++, i++)
			bb[i] = b[j];
		//sort(aa, aa + bbb - aaa + 1);                        //没有排序,只是取下标在中间的值
		//sort(bb, bb + d - c + 1);
		int mid = ((bbb - aaa) + 1 + (d - c) + 1) / 2;
		if (((bbb - aaa) + 1 + (d - c) + 1) % 2 == 1)          
			mid++;
																  //下面就是找第mid个
		if (bbb - aaa + 1 >= mid)                                 //在左边能够取到
		{
			printf("%d\n", a[aaa + mid-1]);
		}
		else                                                      //在第二个数组中取到
		{
			printf("%d\n", b[mid - (bbb - aaa + 1)+c-1]);         //这里必须是从c开始的数,容易题
		}
	}
	return 0;
}


你可能感兴趣的:(九度_1097(取中值))