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;
    }
  • 相关阅读:
    [College] C++字符串读入与进制转化-关于《实践教程》P10[程序]的一些总结
    [College] 二进制与机器数的几种形式
    [College] Hello World!
    [SinGuLaRiTy] 复习模板-数学
    ByteCTF 2020 KOP Writeup
    【题解】电子科技大学第十八届 ACM 程序设计竞赛
    【逆向】某 VR 驱动分析过程
    物联网框架 IoTivity 中间人攻击分析
    批处理工具 CAPI 逆向分析之 API Call
    DASCTF 2020 六月赛 Reverse Writeup
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9801871.html
Copyright © 2011-2022 走看看