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;
    }
  • 相关阅读:
    6. svg学习笔记-路径
    5. svg学习笔记-坐标系变换
    4. svg学习笔记-文档结构元素和样式的使用
    2. svg学习笔记-svg中的坐标系统和viewbox
    3. svg学习笔记-基本形状和画笔属性
    多项式:从门都没入到刚迈过门槛
    排列组合与二项式基础
    单调队列入门
    多项式:从什么都不知道到门都没入
    动态规划之四边形不等式优化
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9801871.html
Copyright © 2011-2022 走看看