Median

Median

这里写图片描述
.
.
题目挺水的。注意给定的序列是有序的,所以其实只有两种情况,要么给定的两段序列无关,要么出现重叠,这两个情况分开弄一下就好了。
.
.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 100010;
long long a[maxn];
int l, r, l1, r1, l2, r2, mergel, merger, sum, n, m;

long long find1(int t) {
    if (l1+t-1 <= r1) return a[l1+t-1];
    t = t-(r1-l1+1);
    return a[l2+t-1];
}

long long find2(int t) {
    l = l1;
    r = max(r1, r2);
    mergel = l2;
    merger = min(r1, r2);
    if (l+t-1 < mergel) return a[l+t-1];
    t = t-(mergel-l);
    if (mergel+(t-1)/2 <= merger) return a[mergel+(t-1)/2];
    t = t-(merger-mergel+1)*2;
    return a[merger+t];
}


int main() {
    int tt;
    scanf("%d", &tt);
    while (tt--) {
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        while (m--) {
            scanf("%d %d", &l1, &r1);
            scanf("%d %d", &l2, &r2);
            if (l1 > l2) {
                swap(l1, l2);
                swap(r1, r2);
            }
            sum = (r1-l1+1)+(r2-l2+1);
            if (r1 < l2) {
                double ans = find1((sum+1)/2)+find1((sum+2)/2);
                printf("%.1lf\n", ans/2.0);
            } else {
                double ans = find2((sum+1)/2)+find2((sum+2)/2);
                printf("%.1lf\n", ans/2.0);
            }
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzh823046544/article/details/52350649
个人分类: acm训练 hdu
上一篇Less Time, More profit
下一篇Counting Intersections
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭