zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    MySQL------Navicat安装与激活
    MySQL------如何将SQLServer文件数据迁移到MySQL
    WinForm------如何跳转另一个窗口,同时关闭当前窗口
    C#------如何判断输入的是否为纯数字
    WinForm------GridControl显示每行的Indicator中的行号
    WinForm------给GridControl添加搜索功能
    WinForm------分页控件dll下载地址
    WinForm------ToolTipController与GridControl的连用
    利用IE/FF的不同识别CSS来使用浏览器兼容问题
    互换两条记录中的字段值方法(有待测试,,)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5784658.html
Copyright © 2011-2022 走看看