• HDU 5857 Median (推导)


    Median

    题目链接:

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5857

    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

    Source

    2016 Multi-University Training Contest 10
    ##题意: 给出一个有序的数列. 求由 A[l1]~A[r1] 与 A[l2]~A[r2] 组成的新序列的中位数.
    ##题解: 中位数:排序后中间位置的数,偶数个时为中间两个的平均值. 由于序列是有序的,可以分情况找到新序列的中位数的下标. 注意细节的处理.
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 101000 #define mod 100000007 #define inf 0x3f3f3f3f #define mid(a,b) ((a+b)>>1) #define IN freopen("in.txt","r",stdin); using namespace std;

    int n, m;
    LL num[maxn];

    LL query1(int l1,int r1,int l2,int r2, int aim) {
    if(l1+aim-1 < l2) return num[l1+aim-1];
    else if(aim > (l2-l1)+2(r1-l2+1)) {
    int pos = aim - ((l2-l1)+2
    (r1-l2+1));
    return num[r1+pos];
    } else {
    aim -= (l2-l1);
    int pos = aim / 2;
    if(aim % 2) return num[l2+pos+1-1];
    else return num[l2+pos-1];
    }
    }

    LL query2(int l1,int r1,int l2,int r2, int aim) {
    if(l1+aim-1 <= r1) return num[l1+aim-1];
    aim -= (r1-l1+1);
    return num[l2+aim-1];
    }

    int main(int argc, char const *argv[])
    {
    //IN;

    int t; cin >> t;
    while(t--)
    {
        scanf("%d %d", &n, &m);
        for(int i=1; i<=n; i++) {
            scanf("%lld", &num[i]);
        }
    
        while(m--) {
            int L1,L2,R1,R2;
            int l1,r1; scanf("%d %d", &L1, &R1);
            int l2,r2; scanf("%d %d", &L2, &R2);
            l1 = min(L1,L2); l2 = max(L1,L2);
            r1 = min(R1,R2); r2 = max(R1,R2);
    
            int tol = (r1-l1+1) + (r2-l2+1);
    
            if(r1 < l2) {
                if(tol & 1) {
                    printf("%lld.0
    ", query2(l1,r1,l2,r2, (tol+1)/2));
                } else {
                    LL ans = query2(l1,r1,l2,r2, (tol+1)/2) + query2(l1,r1,l2,r2, (tol+1)/2+1);
                    printf("%lld", ans/2);
                    if(ans % 2) printf(".5
    ");
                    else printf(".0
    ");
                }
            }
            else {
                if(tol & 1) {
                    printf("%lld.0
    ", query1(l1,r1,l2,r2, (tol+1)/2));
                } else {
                    LL ans = query1(l1,r1,l2,r2, (tol+1)/2) + query1(l1,r1,l2,r2, (tol+1)/2+1);
                    printf("%lld", ans/2);
                    if(ans % 2) printf(".5
    ");
                    else printf(".0
    ");
                }
            }
        }
    }
    
    return 0;
    

    }

  • 相关阅读:
    IOS开发中Xcode常用插件安装与管理(转)
    IOS开发中摇一摇是怎么实现的
    IOS中APP开发常用的一些接口
    数据结构——不相交集(并查集)
    数据结构——(最小)堆(完全二叉树)
    JDK1.7 中的HashMap源码分析
    Java中hashCode()方法以及HashMap()中hash()方法
    《Java多线程核心技术》读书摘要
    Java for LeetCode 237 Delete Node in a Linked List
    Java类变量、实例变量的初始化顺序
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5784770.html
Copyright © 2020-2023  润新知