hdu-5857 Median(水题)

本文介绍了一种解决区间中位数查询问题的方法。对于给定的有序序列和多个查询,每个查询包含两个区间,需要找到由这两个区间组成的序列的中位数。文章提供了一个高效的AC代码实现,并详细解释了解决思路。

题目链接:

Median

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 21    Accepted Submission(s): 4


Problem Description
There is a sorted sequence A of length n. Give you m queries, each one contains four integers, l1, r1, l2, r2. You should use the elements A[l1], A[l1+1] ... A[r1-1], A[r1] and A[l2], A[l2+1] ... A[r2-1], A[r2] to form a new sequence, and you need to find the median of the new sequence.
 

 

Input
First line contains a integer T, means the number of test cases. Each case begin with two integers n, m, means the length of the sequence and the number of queries. Each query contains two lines, first two integers l1, r1, next line two integers l2, r2, l1<=r1 and l2<=r2.
T is about 200.
For 90% of the data, n, m <= 100
For 10% of the data, n, m <= 100000
A[i] fits signed 32-bits int.
 

 

Output
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.
 

 

Sample Input
1
4 2
1 2 3 4
1 2
2 4
1 1
2 2
 

 

Sample Output
2.0
1.5
 
题意:
 
给以一个排好序的序列,m个询问,每个询问给两个区间,问用这两个区间的这些数组成的新序列的中位数是多少;
 
思路:
 
把区间分成三部分,其中一个是相交部分,然后找中位数就好了;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=(1<<20)+14;
const double eps=1e-12;

int n,m,a[N],l1,r1,l2,r2,l3,r3;

int solve(int pos)
{
    if(r1>=l1)
    {
        if(r1-l1+1>=pos)return a[l1+pos-1];
        else pos-=(r1-l1+1);
    }
    if(r3>=l3)
    {
        if(2*(r3-l3+1)>=pos)
        {
            if(pos&1)pos=pos/2+1;
            else pos=pos/2;
            return a[l3+pos-1];
        }
        else pos-=2*(r3-l3+1);
    }
    if(r2>=l2)
    {
        if(r2-l2+1>=pos)return a[l2+pos-1];
        else pos-=(r2-l2+1);
    }
}

int main()
{
    int t;
    read(t);
    while(t--)
    {
        read(n);read(m);
        For(i,1,n)read(a[i]);
        while(m--)
        {
            read(l1);read(r1);
            read(l2);read(r2);
            int num=(r1-l1+1)+(r2-l2+1);
            if(l1>l2)swap(l1,l2);
            if(r1>r2)swap(r1,r2);
            if(r1>=l2)
            {
                l3=l2,r3=r1;
                r1=l3-1;
                l2=r3+1;
            }
            else r3=-1,l3=0;
            if(num&1)printf("%.1lf\n",solve(num/2+1)*1.0);
            else printf("%.1lf\n",solve(num/2)*0.5+solve(num/2+1)*0.5);
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5784658.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值