zoukankan      html  css  js  c++  java
  • hdu5857 Median(模拟)

    Median

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2049    Accepted Submission(s): 506


    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
     
    Author
    BUPT
     
    Source
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
    题意:给出一个有序的数列.
    求由 A[l1]~A[r1] 与 A[l2]~A[r2] 组成的新序列的中位数.
    代码:
    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    #define MAX 100005
    int l1,l2,r2,r1;
    int n,m;
    int a[MAX];
    int solve(int len)
    {
        if(r1<=l2)
        {
            if(r1-l1+1>=len) return a[l1+len-1];
            else{
                len-=r1-l1+1;
                return a[l2+len-1];
            }
        }
        else{
                if(l2-l1>=len)
                {
                    return a[l1+len-1];
                }
           else  if((l2-l1+2*(r1-l2+1))<len){
                len-=(l2-l1+2*(r1-l2+1));//注意这里的括号没加就变成len-l2+l1-2*(r1-l2+1)
                return a[r1+len];
            }
            else{
                len-=l2-l1;
                if(len&1)
                {
                    return a[l2+len/2];
                }
                else{
                    return a[l2+len/2-1];
                }
            }
        }
    }
    int main()
    {
        int T;
       scanf("%d",&T);
       while(T--)
       {
           scanf("%d %d",&n,&m);
           for(int i=1;i<=n;i++)
           {
               scanf("%d",&a[i]);
           }
           while(m--){
           scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
           if(l1>l2)swap(l1,l2);
           if(r1>r2)swap(r1,r2);
           int len=(r2-l2+1)+(r1-l1+1);
           if(len&1)
           {
               printf("%.1f
    ",1.0*solve(len/2+1));
           }
           else printf("%.1f
    ",0.5*solve(len/2)+0.5*solve(len/2+1));
           }
       }
    }
     
  • 相关阅读:
    Web前端的状态管理(State Management)
    使用iScroll实现上拉或者下拉刷新
    实现checkbox组件化(Component)
    HTML5 文件异步上传 — h5uploader.js
    利用javascript和WebGL绘制地球 【翻译】
    利用JS跨域做一个简单的页面访问统计系统
    Java JSON、XML文件/字符串与Bean对象互转解析
    JS实现星级评价
    Spring中@Component注解,@Controller注解详解
    制作滑动条菜单,如何延时处理滑动效果,避免动画卡顿
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9800683.html
Copyright © 2011-2022 走看看