HDU5857 Median 模拟

Median

 HDU - 5857 

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.

InputFirst 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. 
OutputFor 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

给你一个有序序列,让你再两个区间内找到构造成新序列的其中位数

这个题,我们可以把它转换为两个平衡的区间,即确保第一个l和r都比第二个的小

然后就有是否相交,相交了在左边,在中间,在右边

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int l1,r1,l2,r2;
int a[N];
int la(int s)
{
    if(r1<=l2)
    {
        if(r1-l1+1>=s)return a[l1+s-1];
        s-=r1-l1+1;
        return a[l2+s-1];
    }
    if(l2-l1>=s)return a[l1+s-1];
    if(l2-l1>=s-(r1-l2+1)*2)
    {
        s-=l2-l1+1;
        return a[l2+s/2];
    }
    s-=r1-l1+1;
    return a[l2+s-1];

}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        int n,q;
        cin>>n>>q;
        for(int i=1; i<=n; i++)cin>>a[i];
        for(int i=0; i<q; i++)
        {
            cin>>l1>>r1>>l2>>r2;
            if(l2<l1)swap(l1,l2);
            if(r2<r1)swap(r1,r2);
            int cd=r2+r1-l1-l2+2;
            if(cd&1)
            {
                printf("%.1f\n",1.0*la(cd/2+1));
            }
            else
            {
                printf("%.1f\n",0.5*la(cd/2+1)+0.5*la(cd/2));
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/9801871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值