• hdu-5857 Median(水题)


    题目链接:

    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('
    ');
    }
     
    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
    ",solve(num/2+1)*1.0);
                else printf("%.1lf
    ",solve(num/2)*0.5+solve(num/2+1)*0.5);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    DOCK-SWARM
    springMVC上传和下载文件
    生成任意内容任意类型的文件
    配置监听(系统启动和关闭时运行的程序)
    配置过滤器
    读取文件内容(TXT之类的文件)
    读取properties文件中的内容
    时间戳转时间字符串和时间
    启动Service的时候报错unable to install breakpoint in
    java后台调用接口并获取返回值
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5784658.html
Copyright © 2020-2023  润新知