zoukankan      html  css  js  c++  java
  • 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
    ",1.0*la(cd/2+1));
                }
                else
                {
                    printf("%.1f
    ",0.5*la(cd/2+1)+0.5*la(cd/2));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    前端小白之每天学习记录php(2)数据库操作软件
    前端小白之每天学习记录简单的原生js路由
    Dualembedded LSTM for QA match: 双embedding的LSTM聊天匹配式模型
    歌词爬虫
    Web第六周作业:css单位
    一篇无厘头的Roadmap
    web第二周作业:Register Form
    Web第六周作业:position.fixed vs sticky
    Web第六周作业:css颜色表
    Web第三周作业:grid layout
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9801871.html
Copyright © 2011-2022 走看看