HDU 5857 Median

因为原序列是排列好了的,那么只要看一下给出的两个区间相交的情况,然后分类讨论一下,O(1)输出。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar();  }
}

const int maxn=100010;
int T,n,m,a[maxn];

int get(int l1,int r1,int l2,int r2 ,int x)
{
    if(l2>r1)
    {
        if(r1-l1+1>=x) return a[l1+x-1];
        else return a[l2+x-(r1-l1+1)-1];
    }

    else if(r2<=r1)
    {
        if(l2-l1>=x) return a[l1+x-1];
        else if(2*(r2-l2+1)+l2-l1>=x) return a[l2+(x-(l2-l1)-1)/2];
        else return a[r2+1+x-(2*(r2-l2+1)+l2-l1)-1];
    }

    else
    {
        if(l2-l1>=x) return a[l1+x-1];
        else if(2*(r1-l2+1)+l2-l1>=x) return a[l2+(x-(l2-l1)-1)/2];
        else return a[r1+1+x-(2*(r1-l2+1)+l2-l1)-1];
    }
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)
        {
            int l1,r1,l2,r2;
            scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
            if(l1>l2) { swap(l1,l2); swap(r1,r2); }
            if((r1-l1+1+r2-l2+1)%2==1)
            {
                printf("%.1lf\n",1.0*get(l1,r1,l2,r2,(r1-l1+1+r2-l2+1)/2+1));
            }
            else
            {
                double num1=1.0*get(l1,r1,l2,r2,(r1-l1+1+r2-l2+1)/2);
                double num2=1.0*get(l1,r1,l2,r2,(r1-l1+1+r2-l2+1)/2+1);
                printf("%.1lf\n",1.0*(num1+num2)/2.0);
            }
        }
    }
    return 0;
}
时间: 2024-10-11 01:42:07

HDU 5857 Median的相关文章

HDU 5857 Median (2016 多校训#10 1001)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5857 题意:给出一个已排好的序列,再给出两个范围(l1,r1,l2,r2),求由着两个子序列 组成的新序列的中位数,结果保留一位小数. 官方题解: 一个数组上的两个区间求中位数,可以通过分类讨论直接找到中位数,复杂度O(1).不过本题数据较小,优美的log(n)也可过. 分析:我用的是前者的方法.弄一个函数求新序列排第X在原序列的下标,如果长度是奇数,直接求排在第len/2+1位上的,偶

(模拟)HDU - 5857 Median

原题链接:HDU5857 题意: 分析:先挂代码,分析等会写 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int maxn=100010; 9 long long num[maxn]; 10 11 int main() { 12 int t; 13 scanf

HDU 4981 Goffi and Median(水)

HDU 4981 Goffi and Median 思路:排序就可以得到中间数,然后总和和中间数*n比较一下即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 1005; int n, a[N], sum; int main() { while (~scanf("%d&

hdu 3282 Running Median

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of th

HDU 3282 Running Median 动态中位数,可惜数据范围太小

Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Description For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read

[BestCoder Round #6] hdu 4981 Goffi and Median (水题)

Goffi and Median Problem Description A median in a sequence with the length of n is an element which occupies position number ?n+12? after we sort the elements in the non-decreasing order (the elements are numbered starting with 1). A median of an ar

HDU 4981 Goffi and Median

题解:排序取中位数,然后与平均数比较即可. #include <cstdio> #include <algorithm> using namespace std; double a[1005],ave,med,sum; int n; int main(){ while(~scanf("%d",&n)){ sum=0; for(int i=1;i<=n;i++){scanf("%lf",&a[i]);sum+=a[i];}

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

hdu 4908(思路题)

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1614    Accepted Submission(s): 566 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing