HDU 5857 Median【模拟】

Median

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


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
 

Author
BUPT
 

Source
 

主要讨论三种情况:

1.l2>=r1(1 2 3 4),这种最简单,直接找;

2.有重叠部分;

3.第二串是第一串的子串,这种和第二种是一样的,于是直接转化成第二种。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)  memset(a,b,sizeof(a))
const int M=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int i,j,k,n,m;
int r1,r2,l1,l2;
int a[M];
double ans(int x)
{
    if(r1<=l2){
        if(x<=r1-l1+1){
            return a[l1+x-1];
        }
        else{
            return a[l2+x-(r1-l1+1)-1];
        }
    }
    else if(r1<=r2){
        if(x<=l2-l1){
            return a[l1+x-1];
        }
        else if(x>(r1-l1+1)+(r1-l2+1)){
            return a[l2+(x-(r1-l1+1))-1];
        }
        else{
            return a[l2+(x-l2+l1+1)/2-1];
        }
    }
    else{
        swap(r1,r2);
        if(x<=l2-l1){
            return a[l1+x-1];
        }
        else if(x>(r1-l1+1)+(r1-l2+1)){
            return a[l2+x-r1+l1-1-1];
        }
        else{
            return a[l2+(x-l2+l1+1)/2-1];
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        while(m--){
            scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
            if(l1>l2){
                swap(l1,l2);
                swap(r1,r2);
            }
            int sum=r2-l2+r1-l1+2;
            if(sum%2){
                printf("%.1f\n",ans(sum/2+1));
            }
            else {
                printf("%.1f\n",(ans(sum/2)+ans(sum/2+1))/2.0);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值